Java Code Examples for android.content.pm.PackageManager#checkPermission()

The following examples show how to use android.content.pm.PackageManager#checkPermission() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExternalStorage.java    From NIM_Android_UIKit with MIT License 6 votes vote down vote up
/**
 * SD卡存储权限检查
 */
private boolean checkPermission(Context context) {
    if (context == null) {
        Log.e(TAG, "checkMPermission context null");
        return false;
    }

    // 写权限有了默认就赋予了读权限
    PackageManager pm = context.getPackageManager();
    if (PackageManager.PERMISSION_GRANTED !=
            pm.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, context.getApplicationInfo().packageName)) {
        Log.e(TAG, "without permission to access storage");
        return false;
    }

    return true;
}
 
Example 2
Source File: GPSLocationProvider.java    From mappwidget with Apache License 2.0 6 votes vote down vote up
public GPSLocationProvider(Context context)
{
	locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
	
	started = false;
	passiveMode = false;
	
	gpsStatusListener = new MyGPSListener();
	
	PackageManager mgr = context.getPackageManager();
	if (mgr.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
		permGranted = true;
	} else {
		permGranted = false;
	}
}
 
Example 3
Source File: CallHandlerPlugin.java    From CSipSimple with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Retrieve outgoing call handlers available as plugin for csipsimple Also
 * contains stock call handler if available
 * 
 * @param ctxt context of application
 * @return A map of package name => Fancy name of call handler
 */
public static Map<String, String> getAvailableCallHandlers(Context ctxt) {

    if (AVAILABLE_HANDLERS == null) {
        AVAILABLE_HANDLERS = new HashMap<String, String>();

        PackageManager packageManager = ctxt.getPackageManager();
        Intent it = new Intent(SipManager.ACTION_GET_PHONE_HANDLERS);

        List<ResolveInfo> availables = packageManager.queryBroadcastReceivers(it, 0);
        for (ResolveInfo resInfo : availables) {
            ActivityInfo actInfos = resInfo.activityInfo;
            Log.d(THIS_FILE, "Found call handler " + actInfos.packageName + " " + actInfos.name);
            if (packageManager.checkPermission(permission.PROCESS_OUTGOING_CALLS,
                    actInfos.packageName) == PackageManager.PERMISSION_GRANTED) {
                String packagedActivityName = (new ComponentName(actInfos.packageName,
                        actInfos.name)).flattenToString();
                AVAILABLE_HANDLERS.put(packagedActivityName,
                        (String) resInfo.loadLabel(packageManager));
            }
        }
    }

    return AVAILABLE_HANDLERS;
}
 
Example 4
Source File: SystemUtils.java    From ArgusAPM with Apache License 2.0 6 votes vote down vote up
public static boolean checkPermission(String permission) {
    if (TextUtils.isEmpty(permission)) return false;

    Context context = Manager.getContext();
    if (null == context) return false;

    String packageName = context.getPackageName();
    if (TextUtils.isEmpty(packageName)) return false;

    PackageManager pm = context.getPackageManager();
    if (null == pm) return false;

    int permissionState = PackageManager.PERMISSION_DENIED;

    try {
        permissionState = pm.checkPermission(permission, packageName);
    } catch (Exception e) {
        LogX.e(TAG, SUB_TAG, e.toString());
    }

    return PackageManager.PERMISSION_GRANTED == permissionState ? true : false;
}
 
Example 5
Source File: MainActivity.java    From tinker-manager with Apache License 2.0 6 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();
    PackageManager pm = getPackageManager();
    boolean hasPermission = (PackageManager.PERMISSION_GRANTED ==
            pm.checkPermission("android.permission.WRITE_EXTERNAL_STORAGE", getPackageName()))
            && (PackageManager.PERMISSION_GRANTED ==
            pm.checkPermission("android.permission.READ_EXTERNAL_STORAGE", getPackageName()));
    if (!hasPermission) {
        String error = "PatchTool需要存储读写权限";
        showDialog(error);
        mTvContent.setText(error);
        mBtnScan.setEnabled(false);
        mBtnClear.setEnabled(false);
        return;
    }
    mBtnScan.setEnabled(true);
    mBtnClear.setEnabled(true);
    updateContent();
}
 
Example 6
Source File: PermissionCheck.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * パーミッションを確認
 * @param context
 * @param permissionName
 * @return
 */
@SuppressLint("NewApi")
public static int checkSelfPermission(@Nullable final Context context, final String permissionName) {
	if (context == null) return PackageManager.PERMISSION_DENIED;
	int result = PackageManager.PERMISSION_DENIED;
	try {
		if (BuildCheck.isMarshmallow()) {
			result = ContextCompat.checkSelfPermission(context, permissionName);
		} else {
			final PackageManager pm = context.getPackageManager();
			result = pm.checkPermission(permissionName, context.getPackageName());
		}
	} catch (final Exception e) {
		Log.w("", e);
	}
	return result;
}
 
Example 7
Source File: ExtraPlugins.java    From CSipSimple with GNU General Public License v3.0 6 votes vote down vote up
public static Map<String, DynActivityPlugin> getDynActivityPlugins(Context ctxt, String action){
    if(!CACHED_ACTIVITY_RESOLUTION.containsKey(action)) {
        HashMap<String, DynActivityPlugin> plugins = new HashMap<String, DynActivityPlugin>();
        
        PackageManager packageManager = ctxt.getPackageManager();
        Intent it = new Intent(action);
        
        List<ResolveInfo> availables = packageManager.queryIntentActivities(it, 0);
        for(ResolveInfo resInfo : availables) {
            ActivityInfo actInfos = resInfo.activityInfo;
            if( packageManager.checkPermission(SipManager.PERMISSION_USE_SIP, actInfos.packageName) == PackageManager.PERMISSION_GRANTED) {
                ComponentName cmp = new ComponentName(actInfos.packageName, actInfos.name);
                DynActivityPlugin dynInfos;
                dynInfos = new DynActivityPlugin(actInfos.loadLabel(packageManager).toString(), action,
                        cmp, actInfos.metaData);
                plugins.put(cmp.flattenToString(), dynInfos);
            }
        }
        CACHED_ACTIVITY_RESOLUTION.put(action, plugins);
    }
    
    return CACHED_ACTIVITY_RESOLUTION.get(action);
}
 
Example 8
Source File: PackageUtils.java    From SprintNBA with Apache License 2.0 5 votes vote down vote up
/**
 * 检查包名所在的程序是否有某项权限
 *
 * @param permName 权限名称
 * @param pkgName  程序所在的包名
 * @return
 */
public static boolean checkPermission(String permName, String pkgName) {
    PackageManager pm = AppUtils.getAppContext().getPackageManager();
    try {
        boolean isHave = (PackageManager.PERMISSION_GRANTED == pm.checkPermission(permName, pkgName));
        return isHave;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 9
Source File: NetworkUtils.java    From HeaderAndFooterRecyclerView with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前网络状态 返回2代表wifi,1代表2G/3G
 *
 * @param paramContext
 * @return
 */
public static String[] getNetType(Context paramContext) {
    String[] arrayOfString = {"Unknown", "Unknown"};
    PackageManager localPackageManager = paramContext.getPackageManager();
    if (localPackageManager.checkPermission("android.permission.ACCESS_NETWORK_STATE", paramContext.getPackageName()) != 0) {
        arrayOfString[0] = "Unknown";
        return arrayOfString;
    }

    ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext.getSystemService("connectivity");
    if (localConnectivityManager == null) {
        arrayOfString[0] = "Unknown";
        return arrayOfString;
    }

    NetworkInfo localNetworkInfo1 = localConnectivityManager.getNetworkInfo(1);
    if (localNetworkInfo1 != null && localNetworkInfo1.getState() == NetworkInfo.State.CONNECTED) {
        arrayOfString[0] = "2";
        return arrayOfString;
    }

    NetworkInfo localNetworkInfo2 = localConnectivityManager.getNetworkInfo(0);
    if (localNetworkInfo2 != null && localNetworkInfo2.getState() == NetworkInfo.State.CONNECTED) {
        arrayOfString[0] = "1";
        arrayOfString[1] = localNetworkInfo2.getSubtypeName();
        return arrayOfString;
    }

    return arrayOfString;
}
 
Example 10
Source File: ScreenRecordByCodecActivity.java    From ScreenCapture with MIT License 5 votes vote down vote up
private boolean hasPermissions() {
    PackageManager pm = getPackageManager();
    String packageName = getPackageName();
    int granted = (pm.checkPermission(RECORD_AUDIO, packageName) | pm.checkPermission(WRITE_EXTERNAL_STORAGE,
            packageName));
    return granted == PackageManager.PERMISSION_GRANTED;
}
 
Example 11
Source File: Utils.java    From Android-Tech with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
public static boolean checkPermission(Context context, String permission) {
    boolean result = false;
    if (Build.VERSION.SDK_INT >= 23) {
        if (context.checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
            result = true;
        }
    } else {
        PackageManager pm = context.getPackageManager();
        if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
            result = true;
        }
    }
    return result;
}
 
Example 12
Source File: AppDataManage.java    From AndroidTVLauncher with GNU General Public License v3.0 5 votes vote down vote up
public ArrayList<AppModel> getAutoRunAppList() {
    PackageManager localPackageManager = mContext.getPackageManager();
    Intent localIntent = new Intent("android.intent.action.MAIN");
    localIntent.addCategory("android.intent.category.LAUNCHER");
    List<ResolveInfo> localList = localPackageManager.queryIntentActivities(localIntent, 0);
    ArrayList<AppModel> localArrayList = null;
    Iterator<ResolveInfo> localIterator = null;
    if (localList != null) {
        localArrayList = new ArrayList<>();
        localIterator = localList.iterator();
    }

    while (true) {
        if (!localIterator.hasNext())
            break;
        ResolveInfo localResolveInfo = localIterator.next();
        AppModel localAppBean = new AppModel();
        localAppBean.setIcon(localResolveInfo.activityInfo.loadIcon(localPackageManager));
        localAppBean.setName(localResolveInfo.activityInfo.loadLabel(localPackageManager).toString());
        localAppBean.setPackageName(localResolveInfo.activityInfo.packageName);
        localAppBean.setDataDir(localResolveInfo.activityInfo.applicationInfo.publicSourceDir);
        String pkgName = localResolveInfo.activityInfo.packageName;
        String permission = "android.permission.RECEIVE_BOOT_COMPLETED";
        try {
            PackageInfo mPackageInfo = mContext.getPackageManager().getPackageInfo(pkgName, 0);
            if ((PackageManager.PERMISSION_GRANTED == localPackageManager.checkPermission(permission, pkgName))
                    && !((mPackageInfo.applicationInfo.flags & mPackageInfo.applicationInfo.FLAG_SYSTEM) > 0)) {
                localArrayList.add(localAppBean);
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }
    return localArrayList;
}
 
Example 13
Source File: Utils.java    From paystack-android with Apache License 2.0 5 votes vote down vote up
/**
 * To check for internet permission
 *
 * @param context - Application context for current run
 */
public static void hasInternetPermission(Context context) {
  validateNotNull(context, "context");
  PackageManager pm = context.getPackageManager();
  int hasPermission = pm.checkPermission(Manifest.permission.INTERNET, context.getPackageName());//context.checkCallingOrSelfPermission(Manifest.permission.INTERNET);
  if (hasPermission ==
      PackageManager.PERMISSION_DENIED) {
    throw new IllegalStateException("Paystack requires internet permission. " +
        "Please add the intenet permission to your AndroidManifest.xml");
  }
}
 
Example 14
Source File: LoginDialog.java    From android-auth with Apache License 2.0 4 votes vote down vote up
private boolean internetPermissionGranted() {
    PackageManager pm = getContext().getPackageManager();
    String packageName = getContext().getPackageName();
    return pm.checkPermission(Manifest.permission.INTERNET, packageName) == PackageManager.PERMISSION_GRANTED;
}
 
Example 15
Source File: LoginDialog.java    From blade-player with GNU General Public License v3.0 4 votes vote down vote up
private boolean internetPermissionGranted() {
    PackageManager pm = getContext().getPackageManager();
    String packageName = getContext().getPackageName();
    return pm.checkPermission(Manifest.permission.INTERNET, packageName) == PackageManager.PERMISSION_GRANTED;
}
 
Example 16
Source File: RootUtils.java    From lsiem with Apache License 2.0 4 votes vote down vote up
public static boolean haveReadLogsPermission(Context context) {
    PackageManager pm = context.getPackageManager();
    return pm.checkPermission(android.Manifest.permission.READ_LOGS, context.getPackageName()) == 0;
}
 
Example 17
Source File: AppApplicationMgr.java    From AndroidWallet with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 是否有权限
 *
 * @param context
 * @param permission
 * @return
 */
public static boolean hasPermission(Context context, String permission) {
    if (context != null && !TextUtils.isEmpty(permission)) {
        try {
            PackageManager packageManager = context.getPackageManager();
            if (packageManager != null) {
                if (PackageManager.PERMISSION_GRANTED == packageManager.checkPermission(permission, context
                        .getPackageName())) {
                    return true;
                }
                Log.d("AppUtils", "Have you  declared permission " + permission + " in AndroidManifest.xml ?");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    return false;
}
 
Example 18
Source File: Util.java    From tracker-control-android with GNU General Public License v3.0 4 votes vote down vote up
public static boolean hasInternet(String packageName, Context context) {
    PackageManager pm = context.getPackageManager();
    return (pm.checkPermission("android.permission.INTERNET", packageName) == PackageManager.PERMISSION_GRANTED);
}
 
Example 19
Source File: IntentUtils.java    From AndroidBleManager with Apache License 2.0 3 votes vote down vote up
/**
 * 是否有某个权限(某权限是否开启)
 * <p><b>注: </b>结果不一定准确,对于第三方厂商会更改<p/>
 * @param context
 * @param permissionString 权限字符串,例如android.permission.RECORD_AUDIO
 * @return
 */
public static boolean isHavePermission(Context context, String permissionString){
    PackageManager pm = context.getPackageManager();
    boolean isHave = (PackageManager.PERMISSION_GRANTED ==
            pm.checkPermission(permissionString, context.getPackageName()));
    return isHave;
}
 
Example 20
Source File: HapticFeedbackController.java    From date_picker_converter with Apache License 2.0 2 votes vote down vote up
/**
 * Method to verify that vibrate permission has been granted.
 *
 * Allows users of the library to disabled vibrate support if desired.
 * @return true if Vibrate permission has been granted
 */
private boolean hasVibratePermission(Context context) {
    PackageManager pm = context.getPackageManager();
    int hasPerm = pm.checkPermission(android.Manifest.permission.VIBRATE, context.getPackageName());
    return hasPerm == PackageManager.PERMISSION_GRANTED;
}