背景
我发现了一个应用程序,它以某种方式隐藏了提醒通知,甚至包括正在进行的通知(由前台服务使用),称为NCleaner。
我想知道这样的事情是如何工作的。
问题
没有太多关于如何控制其他应用程序通知的信息。只有当前的应用程序,由于某种原因,我未能取消正在进行的通知,但我已经成功取消了一个正常的通知。
我发现了什么
我发现这个示例展示了如何监控通知。在搜索有关NotificationListenerService的文档后,我还发现了如何取消其他应用程序的特定通知。
但是,它不适用于正在进行的通知。
为了测试正常通知,我只是通过我的电脑给自己发了一封电子邮件或在 PushBullet 应用程序上写了一些东西。
为了测试持续通知,我安装并运行了我的业余时间应用程序(这里),它在应用程序的开头显示一个持续通知,从 Android O 开始。事实上,它甚至可以隐藏操作系统的通知, USB 连接和调试(只需通过 USB 将设备连接到 PC)...
这是我根据示例制作的代码,用于取消它收到的所有通知:
NotificationListenerExampleService.kt
class NotificationListenerExampleService : NotificationListenerService() {
override fun onNotificationPosted(sbn: StatusBarNotification) {
Log.d("AppLog", "sbn:" + sbn.toString())
cancelNotification(sbn.key)
}
override fun onListenerConnected() {
super.onListenerConnected()
Log.d("AppLog", "onListenerConnected")
}
override fun onNotificationRemoved(sbn: StatusBarNotification) {}
}
显现:
<application
android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:name=".NotificationListenerExampleService" android:label="@string/service_label" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"/>
</intent-filter>
</service>
</application>
MainActivity.kt
class MainActivity : AppCompatActivity() {
private val isNotificationServiceEnabled: Boolean
get() {
val pkgName = packageName
val flat = Settings.Secure.getString(contentResolver, "enabled_notification_listeners"")
if (!TextUtils.isEmpty(flat)) {
val names = flat.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
for (i in names.indices) {
val cn = ComponentName.unflattenFromString(names[i])
if (cn != null) {
if (TextUtils.equals(pkgName, cn.packageName)) {
return true
}
}
}
}
return false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (!isNotificationServiceEnabled) {
startActivity(Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"))
}
}
}
问题
- 为什么我的代码无法删除正在进行的通知?
- 我找到的应用程序为什么可以做到这一点?它有什么作用?
- 这个 API 在通知方面提供了多少?查看文档,它似乎可以做很多事情:阅读通知,关闭它们,获取有关它们的各种信息,......但我找不到其他一些功能:是否可以修改通知?改变它的优先级?为了避免成为提醒通知而只是在通知抽屉中?是否可以触发通知操作?