我就偷懶了,直接貼代碼了
這個我寫在MainActivity的onCreate()裏面的,相當於進入主頁面檢查一下
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//判斷是否需要開啓通知欄功能
NotificationUtil.OpenNotificationSetting(mContext, new NotificationUtil.OnNextLitener() {
@Override
public void onNext() {
Toast.makeText(mContext,"已開啓通知權限",Toast.LENGTH_SHORT).show();
}
});
}
然後是工具類
/**
* Created by Muzik
* 2018/12/20 10:17
*/
public class NotificationUtil {
//判斷是否需要打開設置界面
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static void OpenNotificationSetting(Context context, OnNextLitener mOnNextLitener) {
if (!isNotificationEnabled(context)) {
showSettingDialog(context);
} else {
if (mOnNextLitener != null) {
mOnNextLitener.onNext();
}
}
}
//判斷該app是否打開了通知
/**
* 可以通過NotificationManagerCompat 中的 areNotificationsEnabled()來判斷是否開啓通知權限。NotificationManagerCompat 在 android.support.v4.app包中,是API 22.1.0 中加入的。而 areNotificationsEnabled()則是在 API 24.1.0之後加入的。
* areNotificationsEnabled 只對 API 19 及以上版本有效,低於API 19 會一直返回true
* */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static boolean isNotificationEnabled(Context context) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
boolean areNotificationsEnabled = notificationManagerCompat.areNotificationsEnabled();
return areNotificationsEnabled;
}
String CHECK_OP_NO_THROW = "checkOpNoThrow";
String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
ApplicationInfo appInfo = context.getApplicationInfo();
String pkg = context.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Class appOpsClass = null;
/* Context.APP_OPS_MANAGER */
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 (Exception e) {
e.printStackTrace();
}
return false;
}
//打開手機設置頁面
/**
* 假設沒有開啓通知權限,點擊之後就需要跳轉到 APP的通知設置界面,對應的Action是:Settings.ACTION_APP_NOTIFICATION_SETTINGS, 這個Action是 API 26 後增加的
* 如果在部分手機中無法精確的跳轉到 APP對應的通知設置界面,那麼我們就考慮直接跳轉到 APP信息界面,對應的Action是:Settings.ACTION_APPLICATION_DETAILS_SETTINGS*/
private static void gotoSet(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);
}
/*=====================添加Listener回調================================*/
public interface OnNextLitener {
/**
* 不需要設置通知的下一步
*/
void onNext();
}
private OnNextLitener mOnNextLitener;
public void setOnNextLitener(OnNextLitener mOnNextLitener) {
this.mOnNextLitener = mOnNextLitener;
}
public static void showSettingDialog(final Context mContext) {
//提示彈窗
final CustomDialogSureCancel customDialogSureCancel = new CustomDialogSureCancel(mContext);
customDialogSureCancel.getTitleView().setVisibility(View.GONE);
customDialogSureCancel.getContentView().setText(R.string.never_be_pass);
customDialogSureCancel.getSureView().setText(R.string.i_know);
customDialogSureCancel.getCancelView().setText(R.string.go_to_set);
customDialogSureCancel.getSureView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialogSureCancel.cancel();
}
});
customDialogSureCancel.getCancelView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialogSureCancel.cancel();
gotoSet(mContext);
}
});
customDialogSureCancel.show();
}
}
這裏面有個自定義的showSettingDialog,就是個對話框,我就不貼了哈~