Guest User

Untitled

a guest
Jul 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. import android.app.AppOpsManager
  2. import android.app.NotificationManager
  3. import android.content.Context
  4. import android.content.Intent
  5. import android.net.Uri
  6. import android.os.Build
  7. import java.lang.reflect.InvocationTargetException
  8.  
  9. // NotificationUtils is a class that could judge notification status or not , and could enableNotification
  10. object NotificationUtils {
  11.  
  12. private const val CHECK_OP_NO_THROW = "checkOpNoThrow"
  13. private const val OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION"
  14.  
  15. @JvmStatic
  16. fun enableNotification(context: Context) =
  17. Intent().apply {
  18. when {
  19. Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
  20. //android 8.0引导
  21. action = "android.settings.APP_NOTIFICATION_SETTINGS"
  22. putExtra("android.provider.extra.APP_PACKAGE", context.packageName)
  23. }
  24. Build.VERSION.SDK_INT in Build.VERSION_CODES.LOLLIPOP..Build.VERSION_CODES.N_MR1 -> {
  25. //android 5.0-7.0
  26. action = "android.settings.APP_NOTIFICATION_SETTINGS"
  27. putExtra("app_package", context.packageName)
  28. putExtra("app_uid", context.applicationInfo.uid)
  29. }
  30. Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP -> {
  31. action = "android.settings.APPLICATION_DETAILS_SETTINGS"
  32. data = Uri.fromParts("package", context.packageName, null)
  33. }
  34. }
  35. }
  36.  
  37. @JvmStatic
  38. fun areNotificationsEnabled(context: Context) = with(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager) {
  39. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  40. areNotificationsEnabled()
  41. } else {
  42. isNotificationEnabled(context)
  43. }
  44. }
  45.  
  46. private fun isNotificationEnabled(context: Context): Boolean {
  47.  
  48. val mAppOps = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
  49. val appInfo = context.applicationInfo
  50. val pkg = context.applicationContext.packageName
  51. val uid = appInfo.uid
  52.  
  53. val appOpsClass: Class<*>? /* Context.APP_OPS_MANAGER */
  54.  
  55. try {
  56. appOpsClass = Class.forName(AppOpsManager::class.java.name)
  57. val checkOpNoThrowMethod = appOpsClass!!.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String::class.java)
  58. val opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION)
  59. val value = opPostNotificationValue.get(Int::class.java) as Int
  60.  
  61. return checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) as Int == AppOpsManager.MODE_ALLOWED
  62. } catch (e: ClassNotFoundException) {
  63. e.printStackTrace()
  64. } catch (e: NoSuchMethodException) {
  65. e.printStackTrace()
  66. } catch (e: NoSuchFieldException) {
  67. e.printStackTrace()
  68. } catch (e: InvocationTargetException) {
  69. e.printStackTrace()
  70. } catch (e: IllegalAccessException) {
  71. e.printStackTrace()
  72. }
  73.  
  74. return false
  75. }
  76. }
Add Comment
Please, Sign In to add comment