共享我的方法,以防有人需要它们:
/** Determines if the context calling has the required permission
* @param context - the IPC context
* @param permissions - The permissions to check
* @return true if the IPC has the granted permission
*/
public static boolean hasPermission(Context context, String permission) {
int res = context.checkCallingOrSelfPermission(permission);
Log.v(TAG, "permission: " + permission + " = \t\t" +
(res == PackageManager.PERMISSION_GRANTED ? "GRANTED" : "DENIED"));
return res == PackageManager.PERMISSION_GRANTED;
}
/** Determines if the context calling has the required permissions
* @param context - the IPC context
* @param permissions - The permissions to check
* @return true if the IPC has the granted permission
*/
public static boolean hasPermissions(Context context, String... permissions) {
boolean hasAllPermissions = true;
for(String permission : permissions) {
//you can return false instead of assigning, but by assigning you can log all permission values
if (! hasPermission(context, permission)) {hasAllPermissions = false; }
}
return hasAllPermissions;
}
并称之为:
boolean hasAndroidPermissions = SystemUtils.hasPermissions(mContext, new String[] {
android.Manifest.permission.ACCESS_WIFI_STATE,
android.Manifest.permission.READ_PHONE_STATE,
android.Manifest.permission.ACCESS_NETWORK_STATE,
android.Manifest.permission.INTERNET,
});