android系统 中使应用默认获取通知使用权

关于这个通知使用权,之前写过一篇文章android 如何去控制第三方音乐播放app之控制QQ音乐。介绍了如何去通过监听通知去拿到QQ音乐的音乐名和歌手名。但是要拿到这个之前,应用必须得首先拿到系统的通知使用权。在上篇文章中的做法是通过主动申请去获取的系统通知使用权。这里再回顾一下:

if (!isNotificationServiceEnabled()) { 初次使用时 判断是否获取了通知使用权
            enableNotificationListenerAlertDialog = buildNotificationServiceAlertDialog();
            enableNotificationListenerAlertDialog.show();
        }
 private boolean isNotificationServiceEnabled() {
        String pkgName = mActivityView.getContext().getPackageName();
        final String flat = Settings.Secure.getString(mActivityView.getContext().getContentResolver(),
                ENABLED_NOTIFICATION_LISTENERS);
        if (!TextUtils.isEmpty(flat)) {
            final String[] names = flat.split(":");
            for (int i = 0; i < names.length; i++) {
                final ComponentName cn = ComponentName.unflattenFromString(names[i]);
                if (cn != null) {
                    if (TextUtils.equals(pkgName, cn.getPackageName())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

但是我转后一想,我是系统应用啊,我app中可是加了sharedUserId=“android.uid.system” 的。我应该生来就有这个权限啊。不应该再去动态申请啊,这对用户体验也不好啊。

首先我发现,我每次动态申请以及检查是否具有通知使用权,都是在系统中的secure 数据库中检查我我的包名服务名是否已经写入了其中。我们可以在串口中用settings 命令看下:
在这里插入图片描述
那接下来我们就只有一个目的,将动态写入的这个值,在系统初次初始化时就将其加入secure 数据库中。

因为是secure 数据库,原想着可以使用Settings.Secure 直接在代码中设置的,于是就找了个开机的流程,将这个设置的操作丢进去,虽然完全开机后,发现数据里有了值。但是依然还是获取不到通知使用权。最后发现可能还是权限的问题,需要android 系统的权限去设置。

接来下说说正解:

还是操作secure 数据库,但是是在一开始创建这个数据的时候,就将那行值加进去就行了。

frameworks/base / packages/SettingsProvider/res/values/defaults.xml

<resources>
frameworks\base\packages\SettingsProvider/res/values/defaults.xml
    <string name="config_default_notification_app" translatable="false">包名\服务名r</string>
</resources>
frameworks/base / packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
 private void loadSecureSettings(SQLiteDatabase db) {
        SQLiteStatement stmt = null;
        try {
            stmt = db.compileStatement("INSERT OR IGNORE INTO secure(name,value)"
                    + " VALUES(?,?);");
            loadStringSetting(stmt, Settings.Secure.ENABLED_NOTIFICATION_LISTENERS,
                    R.string.config_default_notification_app);
            // 将 此行加入
frameworks/base / core/java/android/provider/Settings.java

        private static final HashSet<String> MOVED_TO_SECURE;
        static {
            MOVED_TO_SECURE = new HashSet<>(30);
            MOVED_TO_SECURE.add(Secure.ANDROID_ID);
            MOVED_TO_SECURE.add(Secure.ENABLED_NOTIFICATION_LISTENERS); // 加入此属性

这样,这个app就完全拥有了通知使用权,就不用再去动态申请了。哇哈哈哈!

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
以下是使用 Android Studio 创建通知的示例代码: 1. 在 `AndroidManifest.xml` 文件中添加通知权限: ```xml <uses-permission android:name="android.permission.VIBRATE"/> ``` 2. 在布局文件中创建一个按钮并设置其 `onClick` 事件: ```xml <Button android:id="@+id/notification_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Notification" android:onClick="sendNotification"/> ``` 3. 在 Activity 中实现 `sendNotification` 方法,该方法创建并发送通知: ```java public void sendNotification(View view) { // 创建通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "my_channel") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT); // 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(0, builder.build()); } ``` 上述代码中,我们创建了一个包含小图标、标题和内容的通知,并将其发送到系统通知栏中。注意,我们需要指定一个通知渠道,这可以在应用启动时进行设置。 4. 在应用启动时创建通知渠道: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 创建通知渠道 CharSequence name = getString(R.string.channel_name); String description = getString(R.string.channel_description); int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel("my_channel", name, importance); channel.setDescription(description); // 注册通知渠道 NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } ``` 上述代码中,我们检查当前 Android 版本是否支持通知渠道,如果支持,则创建一个默认级别的通知渠道,并在系统中注册该渠道。 现在,当用户点击按钮时,应用将创建并发送一个通知系统通知栏中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

假装多好123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值