下面列出了android.content.Context#checkCallingOrSelfPermission ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@TargetApi(Build.VERSION_CODES.N)
private static boolean isConnected(Context context) {
try {
int hasPermission = context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE);
if (PackageManager.PERMISSION_GRANTED != hasPermission) {
Log.w(TAG,"android.Manifest.permission.ACCESS_NETWORK_STATE is not granted.");
} else {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (null != networkInfo && networkInfo.isConnected()) {
return true;
}
}
} catch (Exception ex) {
Log.w(TAG,"failed to detect networking status.", ex);
}
return false;
}
/** Returns the {@link NetworkStatsAccess.Level} for the given caller. */
public static @NetworkStatsAccess.Level int checkAccessLevel(
Context context, int callingUid, String callingPackage) {
final DevicePolicyManagerInternal dpmi = LocalServices.getService(
DevicePolicyManagerInternal.class);
final TelephonyManager tm = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
boolean hasCarrierPrivileges = tm != null &&
tm.checkCarrierPrivilegesForPackage(callingPackage) ==
TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
boolean isDeviceOwner = dpmi != null && dpmi.isActiveAdminWithPolicy(callingUid,
DeviceAdminInfo.USES_POLICY_DEVICE_OWNER);
if (hasCarrierPrivileges || isDeviceOwner
|| UserHandle.getAppId(callingUid) == android.os.Process.SYSTEM_UID) {
// Carrier-privileged apps and device owners, and the system can access data usage for
// all apps on the device.
return NetworkStatsAccess.Level.DEVICE;
}
boolean hasAppOpsPermission = hasAppOpsPermission(context, callingUid, callingPackage);
if (hasAppOpsPermission || context.checkCallingOrSelfPermission(
READ_NETWORK_USAGE_HISTORY) == PackageManager.PERMISSION_GRANTED) {
return NetworkStatsAccess.Level.DEVICESUMMARY;
}
boolean isProfileOwner = dpmi != null && dpmi.isActiveAdminWithPolicy(callingUid,
DeviceAdminInfo.USES_POLICY_PROFILE_OWNER);
if (isProfileOwner) {
// Apps with the AppOps permission, profile owners, and apps with the privileged
// permission can access data usage for all apps in this user/profile.
return NetworkStatsAccess.Level.USER;
}
// Everyone else gets default access (only to their own UID).
return NetworkStatsAccess.Level.DEFAULT;
}
public static boolean checkPermission(Context context, String permission) throws SKException {
if (context == null) {
throw new SKException(TAG, "Context cannot be null.", SKExceptionErrorCode.UNKNOWN_ERROR);
}
int res = context.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
private static void checkPermission(Context context) {
String permission = "android.permission.SYSTEM_ALERT_WINDOW";
int status = context.checkCallingOrSelfPermission(permission);
if (status == PackageManager.PERMISSION_DENIED) {
throw new IllegalStateException("in order to use Galgo, " +
"please add the permission " + permission + " to your AndroidManifest.xml");
}
}
public static boolean isOnline(Context context) {
int res = context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE);
if (res == PackageManager.PERMISSION_GRANTED) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
return true;
}
private boolean isPermissionGranted() {
Context context = Leanplum.getContext();
try {
return context.checkCallingOrSelfPermission(PERMISSION) == PackageManager.PERMISSION_GRANTED;
} catch (RuntimeException ignored) {
return false;
}
}
/**
* 初始化accesTokenManager等信息
*
* @param context 当前执行的上下文环境
*/
public void init(Context context) {
if (context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE)
!= PackageManager.PERMISSION_GRANTED) {
Log.w(LOG_TAG, "App miss permission android.permission.ACCESS_NETWORK_STATE! "
+ "Some mobile's WebView don't display page!");
}
this.accessTokenManager = new AccessTokenManager(context);
this.accessTokenManager.initToken();
}
private static boolean hasExternalStoragePermission(Context context) {
int perm = context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION);
return perm == PackageManager.PERMISSION_GRANTED;
}
private static boolean hasExternalStoragePermission(Context context) {
return context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION) == 0;
}
public static boolean hasExternalStoragePermission(Context context) {
int perm = context.checkCallingOrSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE");
return perm == 0;
}
void disableAnimations(Context context) {
int permStatus = context.checkCallingOrSelfPermission(android.Manifest.permission.SET_ANIMATION_SCALE);
if (permStatus == PackageManager.PERMISSION_GRANTED) {
setSystemAnimationsScale(0.0f);
}
}
private static boolean hasExternalStoragePermission(Context context) {
int perm = context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION);
return perm == PackageManager.PERMISSION_GRANTED;
}
public static boolean hasAnyLocationPermission(Context context) {
int fineLocationPermission = context.checkCallingOrSelfPermission("android.permission.ACCESS_FINE_LOCATION");
int coarseLocationPermission = context.checkCallingOrSelfPermission("android.permission.ACCESS_COARSE_LOCATION");
return fineLocationPermission == 0 || coarseLocationPermission == 0;
}
public static boolean hasInternetPermission(Context context)
{
return context == null || context.checkCallingOrSelfPermission("android.permission.INTERNET") == 0;
}
private static boolean hasExternalStoragePermission(Context context) {
int perm = context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION);
return perm == PackageManager.PERMISSION_GRANTED;
}
public static boolean isDevicePowerPermissionGranted(Context context) {
return context.checkCallingOrSelfPermission("android.permission.DEVICE_POWER") == PackageManager.PERMISSION_GRANTED;
}
private static boolean hasExternalStoragePermission(Context context) {
int perm = context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION);
return perm == PackageManager.PERMISSION_GRANTED;
}
public static boolean hasAnyLocationPermission(Context context) {
int fineLocationPermission = context.checkCallingOrSelfPermission("android.permission.ACCESS_FINE_LOCATION");
int coarseLocationPermission = context.checkCallingOrSelfPermission("android.permission.ACCESS_COARSE_LOCATION");
return fineLocationPermission == 0 || coarseLocationPermission == 0;
}
public static boolean isSecureSettingsPermissionGranted(Context context) {
if (context.checkCallingOrSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS) == PackageManager.PERMISSION_GRANTED)
return true;
else return false;
}
/**
* Check for camera permission boolean.
*
* @param context the context
* @return the boolean
*/
boolean checkForCameraPermission(Context context) {
return context.checkCallingOrSelfPermission(Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED;
}