整理下安卓跳轉通知設置頁面的代碼,如下:
1、android check通知是否開啓代碼
public static boolean isNotificationEnabled(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return isEnableV26(context);
} else {
return isEnableV19(context);
}
}
/**
* Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
* 19及以上
*
* @param context
* @return
*/
public static boolean isEnableV19(Context context) {
AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
ApplicationInfo appInfo = context.getApplicationInfo();
String pkg = context.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Class appOpsClass;
try {
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, pkg) == AppOpsManager.MODE_ALLOWED);
} catch (ClassNotFoundException e) {
MyLog.e(sTAG, e);
} catch (NoSuchMethodException e) {
MyLog.e(sTAG, e);
} catch (NoSuchFieldException e) {
MyLog.e(sTAG, e);
} catch (InvocationTargetException e) {
MyLog.e(sTAG, e);
} catch (IllegalAccessException e) {
MyLog.e(sTAG, e);
}
return false;
}
/**
* Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
* 針對8.0及以上設備
*
* @param context
* @return
*/
public static boolean isEnableV26(Context context) {
try {
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Method sServiceField = notificationManager.getClass().getDeclaredMethod("getService");
sServiceField.setAccessible(true);
Object sService = sServiceField.invoke(notificationManager);
ApplicationInfo appInfo = context.getApplicationInfo();
String pkg = context.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Method method = sService.getClass().getDeclaredMethod("areNotificationsEnabledForPackage"
, String.class, Integer.TYPE);
method.setAccessible(true);
return (boolean) method.invoke(sService, pkg, uid);
} catch (Exception e) {
MyLog.e(sTAG, e);
}
return false;
}
2、跳轉到通知頁面
public static void gotoNotificationSetting(Activity activity) {
ApplicationInfo appInfo = activity.getApplicationInfo();
String pkg = activity.getApplicationContext().getPackageName();
int uid = appInfo.uid;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
//這種方案適用於 API 26, 即8.0(含8.0)以上可以用
intent.putExtra(Settings.EXTRA_APP_PACKAGE, pkg);
intent.putExtra(Settings.EXTRA_CHANNEL_ID, uid);
//這種方案適用於 API21——25,即 5.0——7.1 之間的版本可以使用
intent.putExtra("app_package", pkg);
intent.putExtra("app_uid", uid);
activity.startActivityForResult(intent, AppConst.REQUEST_SETTING_NOTIFICATION);
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + GlobalData.app().getPackageName()));
activity.startActivityForResult(intent, AppConst.REQUEST_SETTING_NOTIFICATION);
} else {
Intent intent = new Intent(Settings.ACTION_SETTINGS);
activity.startActivityForResult(intent, AppConst.REQUEST_SETTING_NOTIFICATION);
}
} catch (Exception e) {
Intent intent = new Intent(Settings.ACTION_SETTINGS);
activity.startActivityForResult(intent, AppConst.REQUEST_SETTING_NOTIFICATION);
MyLog.e(sTAG, e);
}
}
3、但是目前存在兩個已知問題。
(1)優購手機(UOOGOU T10 Android 5.1.1) check通知權限,關閉的通知權限, 也會isNotificationEnabled函數也會返回true,check權限不準,目前在排查,有結論了再在博客添加。
(2)小米手機部分系統跳轉到通知設置頁了,但是卻屏蔽了開啓按鈕的顯示。 很詭異。
我測試的問題手機是 米五(MIUI 9 8.4.27 開發板 Android 8.0.0) 和 小米6x (MIUI 10.0 穩定版 Android 8.1.0)
截圖如下:
如果大家說有更好地解決方案麻煩留言評論,謝謝。