NotificationUtil.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.edufound.mobile.util;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.app.AppOpsManager;
  5. import android.app.NotificationManager;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.content.pm.ApplicationInfo;
  10. import android.net.Uri;
  11. import android.os.Build;
  12. import android.support.annotation.RequiresApi;
  13. import android.support.v4.app.NotificationManagerCompat;
  14. import android.view.View;
  15. import android.widget.TextView;
  16. import com.edufound.mobile.R;
  17. import java.lang.reflect.Field;
  18. import java.lang.reflect.InvocationTargetException;
  19. import java.lang.reflect.Method;
  20. public class NotificationUtil {
  21. public static void choseNotification(Activity activity) {
  22. //如果是新应用,查询权限并且询问
  23. if (SPutil.getPrefInt(activity, SPutil.notificationType, 0) == 0) {
  24. if (NotificationUtil.areNotificationsEnabled(activity)) {
  25. //有权限
  26. SPutil.setPrefInt(activity, SPutil.notificationType, SPutil.NOTIFICATION_OPEN);
  27. } else {
  28. //没权限
  29. SPutil.setPrefInt(activity, SPutil.notificationType, SPutil.NOTIFICATION_CLOSE);
  30. NotificationUtil.showDialog(activity);
  31. }
  32. } else {
  33. //不是第一次新应用,如果设置了有权限,就设置系统默认的权限,不提示
  34. if (NotificationUtil.areNotificationsEnabled(activity)) {
  35. //有权限
  36. SPutil.setPrefInt(activity, SPutil.notificationType, SPutil.NOTIFICATION_OPEN);
  37. } else {
  38. //没权限
  39. SPutil.setPrefInt(activity, SPutil.notificationType, SPutil.NOTIFICATION_CLOSE);
  40. }
  41. }
  42. }
  43. private static boolean areNotificationsEnabled(Context context) {
  44. NotificationManagerCompat.from(context).areNotificationsEnabled();
  45. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  46. return true;
  47. }
  48. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
  49. return isEnableV19(context);
  50. } else {
  51. return isEnableV26(context);
  52. }
  53. }
  54. @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  55. private static boolean isEnableV19(Context context) {
  56. final String CHECK_OP_NO_THROW = "checkOpNoThrow";
  57. final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
  58. AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
  59. ApplicationInfo appInfo = context.getApplicationInfo();
  60. String pkg = context.getApplicationContext().getPackageName();
  61. int uid = appInfo.uid;
  62. Class appOpsClass = null; /* Context.APP_OPS_MANAGER */
  63. try {
  64. appOpsClass = Class.forName(AppOpsManager.class.getName());
  65. Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
  66. Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
  67. int value = (int) opPostNotificationValue.get(Integer.class);
  68. return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
  69. } catch (ClassNotFoundException e) {
  70. } catch (NoSuchMethodException e) {
  71. } catch (NoSuchFieldException e) {
  72. } catch (InvocationTargetException e) {
  73. } catch (IllegalAccessException e) {
  74. } catch (Exception e) {
  75. }
  76. return false;
  77. }
  78. private static boolean isEnableV26(Context context) {
  79. ApplicationInfo appInfo = context.getApplicationInfo();
  80. String pkg = context.getApplicationContext().getPackageName();
  81. int uid = appInfo.uid;
  82. try {
  83. NotificationManager notificationManager = (NotificationManager)
  84. context.getSystemService(Context.NOTIFICATION_SERVICE);
  85. Method sServiceField = notificationManager.getClass().getDeclaredMethod("getService");
  86. sServiceField.setAccessible(true);
  87. Object sService = sServiceField.invoke(notificationManager);
  88. Method method = sService.getClass().getDeclaredMethod("areNotificationsEnabledForPackage"
  89. , String.class, Integer.TYPE);
  90. method.setAccessible(true);
  91. return (boolean) method.invoke(sService, pkg, uid);
  92. } catch (Exception e) {
  93. return true;
  94. }
  95. }
  96. private static void showDialog(final Activity act) {
  97. AlertDialog.Builder builder = new AlertDialog.Builder(act)
  98. // .setIcon(R.mipmap.ic_launcher)
  99. .setTitle("提示")
  100. .setMessage("检测到您没有打开通知权限,是否去打开")
  101. .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  102. @Override
  103. public void onClick(DialogInterface dialogInterface, int i) {
  104. dialogInterface.cancel();
  105. Intent localIntent = new Intent();
  106. localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  107. if (Build.VERSION.SDK_INT >= 9) {
  108. localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
  109. localIntent.setData(Uri.fromParts("package", act.getPackageName(), null));
  110. } else if (Build.VERSION.SDK_INT <= 8) {
  111. localIntent.setAction(Intent.ACTION_VIEW);
  112. localIntent.setClassName("com.android.settings",
  113. "com.android.settings.InstalledAppDetails");
  114. localIntent.putExtra("com.android.settings.ApplicationPkgName",
  115. act.getPackageName());
  116. }
  117. act.startActivity(localIntent);
  118. }
  119. }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
  120. @Override
  121. public void onClick(DialogInterface dialog, int i) {
  122. dialog.cancel();
  123. }
  124. });
  125. builder.create().show();
  126. }
  127. }