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

The following are Jave code examples for showing how to use checkPermission() of the android.content.pm.PackageManager class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: BaseCore   File: PhoneUtils.java   View Source Code Vote up 6 votes
public static boolean checkPermission(Context context, String permission) {
    boolean result = false;
    if (Build.VERSION.SDK_INT >= 23) {
        try {
            Class<?> clazz = Class.forName("android.content.Context");
            Method method = clazz.getMethod("checkSelfPermission", String.class);
            int rest = (Integer) method.invoke(context, permission);
            if (rest == PackageManager.PERMISSION_GRANTED) {
                result = true;
            } else {
                result = false;
            }
        } catch (Exception e) {
            result = false;
        }
    } else {
        PackageManager pm = context.getPackageManager();
        if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
            result = true;
        }
    }
    return result;
}
 
Example 2
Project: CSipSimple   File: RewriterPlugin.java   View Source Code Vote up 6 votes
/**
 * Retrieve rewriter available as plugin for csipsimple.
 * 
 * @param ctxt context of application
 * @return A map of package name => Fancy name of rewriter
 */
public static Map<String, String> getAvailableRewriters(Context ctxt) {

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

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

        List<ResolveInfo> availables = packageManager.queryBroadcastReceivers(it, 0);
        for (ResolveInfo resInfo : availables) {
            ActivityInfo actInfos = resInfo.activityInfo;
            Log.d(THIS_FILE, "Found rewriter " + 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_REWRITERS.put(packagedActivityName,
                        (String) resInfo.loadLabel(packageManager));
            }
        }
    }

    return AVAILABLE_REWRITERS;
}
 
Example 3
Project: CSipSimple   File: ExtraPlugins.java   View Source Code Vote up 6 votes
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 4
Project: AndroidStatisticsLib   File: DeviceUtil.java   View Source Code Vote up 6 votes
@SuppressLint("NewApi")
public static boolean checkPermission(Context context, String permission) {
    boolean result = false;
    if (Build.VERSION.SDK_INT >= 23) {
        try {
            Class<?> clazz = Class.forName("android.content.Context");
            Method method = clazz.getMethod("checkSelfPermission", String.class);
            int rest = (Integer) method.invoke(context, permission);
            result = rest == PackageManager.PERMISSION_GRANTED;
        } catch (Exception e) {
            result = false;
        }
    } else {
        PackageManager pm = context.getPackageManager();
        if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
            result = true;
        }
    }
    return result;
}
 
Example 5
Project: AndroidBasicLibs   File: AppUtil.java   View Source Code Vote up 6 votes
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 6
Project: MinimalismJotter   File: PermissionUtils.java   View Source Code Vote up 6 votes
public static Boolean isAllowUseCamera(PackageManager pm) {
    boolean permission = (PackageManager.PERMISSION_GRANTED ==
            pm.checkPermission("android.permission.CAMERA", "com.lipy.jotter"))
            && (PackageManager.PERMISSION_GRANTED ==
            pm.checkPermission("android.permission.WRITE_EXTERNAL_STORAGE", "com.lipy.jotter"))
            && (PackageManager.PERMISSION_GRANTED ==
            pm.checkPermission("android.permission.READ_EXTERNAL_STORAGE", "com.lipy.jotter"));
    return permission;
}
 
Example 7
Project: MovingGdufe-Android   File: YoumengTest.java   View Source Code Vote up 6 votes
public static boolean checkPermission(Context context, String permission) {
    boolean result = false;
    if (Build.VERSION.SDK_INT >= 23) {
        try {
            Class<?> clazz = Class.forName("android.content.Context");
            Method method = clazz.getMethod("checkSelfPermission", String.class);
            int rest = (Integer) method.invoke(context, permission);
            if (rest == PackageManager.PERMISSION_GRANTED) {
                result = true;
            } else {
                result = false;
            }
        } catch (Exception e) {
            result = false;
        }
    } else {
        PackageManager pm = context.getPackageManager();
        if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
            result = true;
        }
    }
    return result;
}
 
Example 8
Project: anyRTC-P2P-Android   File: PreCallActivity.java   View Source Code Vote up 6 votes
/**
 * 权限检测
 *
 * @param permissions 权限的名称
 * @return
 */
public boolean checkPremission(String... permissions) {
    boolean allHave = true;
    PackageManager pm = getPackageManager();
    for (String permission : permissions) {
        switch (pm.checkPermission(permission, getApplication().getPackageName())) {
            case PERMISSION_GRANTED:
                allHave = allHave && true;
                continue;
            case PERMISSION_DENIED:
                allHave = allHave && false;
                continue;
        }
    }
    return allHave;
}
 
Example 9
Project: CSipSimple   File: ExtraPlugins.java   View Source Code Vote up 5 votes
/**
 * Retrieve all dynamic codec plugins available in the platform
 * It will resolve for a given action available sip plugins
 * @param ctxt Context of the application
 * @param action Action of the plugin to be resolved
 * For example {@link SipManager#ACTION_GET_EXTRA_CODECS}, {@link SipManager.ACTION_GET_VIDEO_PLUGIN}
 * @return a map containing plugins infos and registrered component name as key
 */
public static Map<String, DynCodecInfos> getDynCodecPlugins(Context ctxt, String action){
    if(!CACHED_RESOLUTION.containsKey(action)) {
        HashMap<String, DynCodecInfos> plugins = new HashMap<String, DynCodecInfos>();
        
        PackageManager packageManager = ctxt.getPackageManager();
        Intent it = new Intent(action);
        
        List<ResolveInfo> availables = packageManager.queryBroadcastReceivers(it, 0);
        for(ResolveInfo resInfo : availables) {
            ActivityInfo actInfos = resInfo.activityInfo;
            if( packageManager.checkPermission(SipManager.PERMISSION_CONFIGURE_SIP, actInfos.packageName) == PackageManager.PERMISSION_GRANTED) {
                ComponentName cmp = new ComponentName(actInfos.packageName, actInfos.name);
                DynCodecInfos dynInfos;
                try {
                    dynInfos = new DynCodecInfos(ctxt, cmp);
                    plugins.put(cmp.flattenToString(), dynInfos);
                } catch (NameNotFoundException e) {
                    Log.e(THIS_FILE, "Error while retrieving infos from dyn codec ", e);
                }
            }
        }
        CACHED_RESOLUTION.put(action, plugins);
    }
    
    return CACHED_RESOLUTION.get(action);
}
 
Example 10
Project: MapDemo   File: MainActivity.java   View Source Code Vote up 5 votes
private boolean hasBasePhoneAuth() {
	// TODO Auto-generated method stub

	PackageManager pm = this.getPackageManager();
	for (String auth : authBaseArr) {
		if (pm.checkPermission(auth, this.getPackageName()) != PackageManager.PERMISSION_GRANTED) {
			return false;
		}
	}
	return true;
}
 
Example 11
Project: XPrivacy   File: ApplicationInfoEx.java   View Source Code Vote up 5 votes
public boolean hasInternet(Context context) {
	if (mInternet == null) {
		mInternet = false;
		PackageManager pm = context.getPackageManager();
		for (ApplicationInfo appInfo : mMapAppInfo.values())
			if (pm.checkPermission("android.permission.INTERNET", appInfo.packageName) == PackageManager.PERMISSION_GRANTED) {
				mInternet = true;
				break;
			}
	}
	return mInternet;
}
 
Example 12
Project: Lantern-sdk   File: SystemServiceData.java   View Source Code Vote up 5 votes
private boolean checkNetworkOn(Context context, int type) {
    boolean isNetworkOn = false;
    try {
        PackageManager packageManager = context.getPackageManager();
        if (packageManager.checkPermission("android.permission.ACCESS_NETWORK_STATE",
                context.getPackageName()) == 0) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            isNetworkOn = connectivityManager.getNetworkInfo(type).isConnected();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return isNetworkOn;
}
 
Example 13
Project: Lantern-sdk   File: SystemServiceData.java   View Source Code Vote up 5 votes
private boolean checkGpsOn(Context context) {
    PackageManager packageManager = context.getPackageManager();
    if(packageManager.checkPermission("android.permission.ACCESS_FINE_LOCATION", context.getPackageName()) == 0) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            return false;
        } else {
            return true;
        }
    }

    return false;
}
 
Example 14
Project: fragmentnav   File: Androids.java   View Source Code Vote up 5 votes
public static boolean checkPermissions(Context context, String... permissions) {
    String pkgName = context.getPackageName();
    PackageManager pm = context.getPackageManager();
    if (permissions != null) {
        for (String permission : permissions) {
            if (PackageManager.PERMISSION_GRANTED != pm.checkPermission(permission, pkgName)) {
                return false;
            }
        }
    }

    return true;
}
 
Example 15
Project: fragmentnav   File: Androids.java   View Source Code Vote up 5 votes
public static boolean checkPermissions(Context context, String... permissions) {
    String pkgName = context.getPackageName();
    PackageManager pm = context.getPackageManager();
    if (permissions != null) {
        for (String permission : permissions) {
            if (PackageManager.PERMISSION_GRANTED != pm.checkPermission(permission, pkgName)) {
                return false;
            }
        }
    }

    return true;
}
 
Example 16
Project: FreeStreams-TVLauncher   File: GetAppList.java   View Source Code Vote up 5 votes
public ArrayList<AppBean> 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<AppBean> localArrayList = null;
    Iterator<ResolveInfo> localIterator = null;
    if (localList != null) {
        localArrayList = new ArrayList<AppBean>();
        localIterator = localList.iterator();
    }

    while (true) {
        if (!localIterator.hasNext())
            break;
        ResolveInfo localResolveInfo = localIterator.next();
        AppBean localAppBean = new AppBean();
        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 17
Project: SimpleUILauncher   File: PackageManagerHelper.java   View Source Code Vote up 4 votes
/**
 * Returns true if {@param srcPackage} has the permission required to start the activity from
 * {@param intent}. If {@param srcPackage} is null, then the activity should not need
 * any permissions
 */
public static boolean hasPermissionForActivity(Context context, Intent intent,
        String srcPackage) {
    PackageManager pm = context.getPackageManager();
    ResolveInfo target = pm.resolveActivity(intent, 0);
    if (target == null) {
        // Not a valid target
        return false;
    }
    if (TextUtils.isEmpty(target.activityInfo.permission)) {
        // No permission is needed
        return true;
    }
    if (TextUtils.isEmpty(srcPackage)) {
        // The activity requires some permission but there is no source.
        return false;
    }

    // Source does not have sufficient permissions.
    if(pm.checkPermission(target.activityInfo.permission, srcPackage) !=
            PackageManager.PERMISSION_GRANTED) {
        return false;
    }

    if (!Utilities.ATLEAST_MARSHMALLOW) {
        // These checks are sufficient for below M devices.
        return true;
    }

    // On M and above also check AppOpsManager for compatibility mode permissions.
    if (TextUtils.isEmpty(AppOpsManager.permissionToOp(target.activityInfo.permission))) {
        // There is no app-op for this permission, which could have been disabled.
        return true;
    }

    // There is no direct way to check if the app-op is allowed for a particular app. Since
    // app-op is only enabled for apps running in compatibility mode, simply block such apps.

    try {
        return pm.getApplicationInfo(srcPackage, 0).targetSdkVersion >= Build.VERSION_CODES.M;
    } catch (NameNotFoundException e) { }

    return false;
}
 
Example 18
Project: DailyStudy   File: GetuiSdkDemoActivity.java   View Source Code Vote up 4 votes
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        context = this;
        isServiceRunning = true;

//        DemoApplication.demoActivity = this;

        initView();
        parseManifests();

        appKeyView.setText(String.format("%s", getResources().getString(R.string.appkey) + appkey));
        appSecretView.setText(String.format("%s", getResources().getString(R.string.appsecret) + appsecret));
        masterSecretView.setText(String.format("%s", getResources().getString(R.string.mastersecret) + MASTERSECRET));
        appIdView.setText(String.format("%s", getResources().getString(R.string.appid) + appid));

        Log.d(TAG, "initializing sdk...");

        PackageManager pkgManager = getPackageManager();

        // 读写 sd card 权限非常重要, android6.0默认禁止的, 建议初始化之前就弹窗让用户赋予该权限
        boolean sdCardWritePermission =
                pkgManager.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, getPackageName()) == PackageManager.PERMISSION_GRANTED;

        // read phone state用于获取 imei 设备信息
        boolean phoneSatePermission =
                pkgManager.checkPermission(Manifest.permission.READ_PHONE_STATE, getPackageName()) == PackageManager.PERMISSION_GRANTED;

        if (Build.VERSION.SDK_INT >= 23 && !sdCardWritePermission || !phoneSatePermission) {
            requestPermission();
        } else {
            PushManager.getInstance().initialize(this.getApplicationContext(), userPushService);
        }

        // 注册 intentService 后 PushDemoReceiver 无效, sdk 会使用 DemoIntentService 传递数据,
        // AndroidManifest 对应保留一个即可(如果注册 DemoIntentService, 可以去掉 PushDemoReceiver, 如果注册了
        // IntentService, 必须在 AndroidManifest 中声明)
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);

        // 应用未启动, 个推 service已经被唤醒,显示该时间段内离线消息
//        if (DemoApplication.payloadData != null) {
//            tLogView.append(DemoApplication.payloadData);
//        }

        // cpu 架构
        Log.d(TAG, "cpu arch = " + (Build.VERSION.SDK_INT < 21 ? Build.CPU_ABI : Build.SUPPORTED_ABIS[0]));

        // 检查 so 是否存在
        File file = new File(this.getApplicationInfo().nativeLibraryDir + File.separator + "libgetuiext2.so");
        Log.e(TAG, "libgetuiext2.so exist = " + file.exists());
    }
 
Example 19
Project: AndroidMuseumBleManager   File: IntentUtils.java   View Source Code Vote up 3 votes
/**
 * 是否有某个权限(某权限是否开启)
 * <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
Project: AssistantBySDK   File: HapticFeedbackController.java   View Source Code Vote up 2 votes
/**
 * 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;
}