private void toSetting() {
Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 9) {
localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
localIntent.setData(Uri.fromParts("package", getPackageName(), null));
}
startActivity(localIntent);
}
/**
* 啓動app應用的信息設置
*/
public static void startAppSettings(Context mContext) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + mContext.getPackageName()));
mContext.startActivity(intent);
}public static int getOsUid(Context context) {
int uid = 0;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ApplicationInfo appinfo = context.getApplicationInfo();
List<ActivityManager.RunningAppProcessInfo> run = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo runningProcess : run) {
if ((runningProcess.processName != null) && runningProcess.processName.equals(appinfo.processName)) {
uid = runningProcess.uid;
break;
}
}
return uid;
}
/**
* 獲取通知欄開關的狀態:分8.0系統之上和8.0系統之下
* des:
*/
public static boolean isNoticeEnable(Context context) {
boolean isNoticeEnable = false;
String pkg = context.getPackageName();
int uid = 0x011111;//本身獲取
if (Build.VERSION.SDK_INT >= 26) {//8.0系統之上
isNoticeEnable=isNoticeEnableV26(context, uid);
} else {
isNoticeEnable=isNoticeEnableV19(context, uid);
}
return isNoticeEnable;
}
/**
* des:8.0以後,提供給APP適用的的NotificationManager 類中有INotificationManager對象,最終咱們經過反射獲取通知欄開關
*/
private static boolean isNoticeEnableV26(Context context, int uid) {
try {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Method sServiceField = notificationManager.getClass().getDeclaredMethod("getService");
sServiceField.setAccessible(true);
Object sService = sServiceField.invoke(notificationManager);
Method method = sService.getClass().getDeclaredMethod("areNotificationsEnabledForPackage", String.class, Integer.TYPE);
method.setAccessible(true);
return (boolean) method.invoke(sService, context.getPackageName(), uid);
} catch (Exception e) {
return true;
}
}
/**
* des:針對8.0以前設備,經過AppOpsManager的checkOpNoThrow方法獲取。
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
private static boolean isNoticeEnableV19(Context context, int uid) {
try {
String CHECK_OP_NO_THROW = "checkOpNoThrow";
String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
Class appOpsClass = null;
AppOpsManager mAppOps = null;
mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
appOpsClass = Class.forName(AppOpsManager.class.getName());
Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE,
Integer.TYPE, String.class);
Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (Integer) opPostNotificationValue.get(Integer.class);
return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, context.getPackageName()) == AppOpsManager.MODE_ALLOWED);
} catch (Exception e) {
return true;
}
}
/**
* 針對8.0及以上設備,發現上述方式不生效。查詢系統設置源碼,在NotificationBackend類中發現獲取通知欄狀態改到INotificationManager中了
*/
private boolean getNotificationsBanned(String pkg, int uid) {
try {
final boolean enabled = sINM.areNotificationsEnabledForPackage(pkg, uid);
return !enabled;
} catch (Exception e) {
Log.w(TAG, "Error calling NoMan", e);
return false;
}
}
/**
* 打開容許通知的設置頁(跳到的是所有通知的頁面,不是很理想)
*/
private void goToNotificationSetting(Context context) {
Intent intent = new Intent();
if (Build.VERSION.SDK_INT >= 26) { // android 8.0引導
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName());
} else if (Build.VERSION.SDK_INT >= 21) { // android 5.0-7.0
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("app_package", context.getPackageName());
intent.putExtra("app_uid", context.getApplicationInfo().uid);
} else { // 其餘
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
======== android
6. ACTION_APPLICATION_DEVELOPMENT_SETTINGS : // 跳轉開發人員選項界面