Android开发获取系统通知开启状态及跳转设置

前言

在App开发中有时候需要知道app通知的系统设置是否开启,来进行相关的展示,与跳转系统设置的操作。

方案

在比较早的版本之前,系统没有相关的api进行直接的操作,需要通过反射的方式去进行操作。
但是现在已经不需要了,直接调用系统的api进行操作即可。

代码解决

  1. 判断是否开启状态,直接调用系统api,开启返回true,未开启返回false。
NotificationManagerCompat.from(this).areNotificationsEnabled();
  1. 跳转app详情页进行设置。
 Uri packageURI = Uri.parse("package:" + this.getPackageName());
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
        startActivity(intent);

源码分析

判断状态源码分三种情况

  1. api>= 24 则返回系统的判断,如下:
mNotificationManager.areNotificationsEnabled();
  1. api >=19 反射方式进行,如下:
AppOpsManager appOps =
                    (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
            ApplicationInfo appInfo = mContext.getApplicationInfo();
            String pkg = mContext.getApplicationContext().getPackageName();
            int uid = appInfo.uid;
            try {
                Class<?> 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 = (int) opPostNotificationValue.get(Integer.class);
                return ((int) checkOpNoThrowMethod.invoke(appOps, value, uid, pkg)
                        == AppOpsManager.MODE_ALLOWED);
            } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException
                    | InvocationTargetException | IllegalAccessException | RuntimeException e) {
                return true;
            }
  1. api <19 则返回true。
    完整方法源码如下:
    /**
     * Returns whether notifications from the calling package are not blocked.
     */
    public boolean areNotificationsEnabled() {
        if (Build.VERSION.SDK_INT >= 24) {
            return mNotificationManager.areNotificationsEnabled();
        } else if (Build.VERSION.SDK_INT >= 19) {
            AppOpsManager appOps =
                    (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
            ApplicationInfo appInfo = mContext.getApplicationInfo();
            String pkg = mContext.getApplicationContext().getPackageName();
            int uid = appInfo.uid;
            try {
                Class<?> 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 = (int) opPostNotificationValue.get(Integer.class);
                return ((int) checkOpNoThrowMethod.invoke(appOps, value, uid, pkg)
                        == AppOpsManager.MODE_ALLOWED);
            } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException
                    | InvocationTargetException | IllegalAccessException | RuntimeException e) {
                return true;
            }
        } else {
            return true;
       

总结

关于app通知的系统设置就介绍到此,结束。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您讲解一下 Android 开发中点击事件及页面跳转的实现方法。 点击事件: 在 Android 开发中,可以通过设置 View 的 OnClickListener 来实现点击事件。具体实现步骤如下: 1. 在 XML 中定义需要响应点击事件的 View,并设置其 id。 ```xml <Button android:id="@+id/btn_click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me!" /> ``` 2. 在 Java 代码中获取该 View,并设置其 OnClickListener。 ```java Button btnClick = findViewById(R.id.btn_click); btnClick.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 点击事件的处理逻辑 } }); ``` 页面跳转: 在 Android 开发中,可以通过 Intent 实现页面跳转。具体实现步骤如下: 1. 在 XML 中定义需要响应点击事件的 View,并设置其 id。 ```xml <Button android:id="@+id/btn_jump" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Jump to another activity" /> ``` 2. 在 Java 代码中获取该 View,并设置其 OnClickListener。 ```java Button btnJump = findViewById(R.id.btn_jump); btnJump.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 创建 Intent 对象,指定需要跳转的目标 Activity Intent intent = new Intent(MainActivity.this, AnotherActivity.class); // 启动目标 Activity startActivity(intent); } }); ``` 需要注意的是,需要在 AndroidManifest.xml 文件中注册目标 Activity。 ```xml <activity android:name=".AnotherActivity" /> ``` 以上就是 Android 开发中点击事件及页面跳转的实现方法。希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值