android - Android:重启应用后 NotificationListenerService 不起作用
问题描述
我正在尝试读取通知,但 NotificationListenerService 在我杀死并重新启动应用程序后不会读取通知。
在启用断点时,我看到断点仅在 onNotificationPosted 中出现一次(在首次启动时)。在重新启动应用程序 NotificationListenerService 不起作用
我正在使用安卓派
有什么解决办法吗?
NotificationListenerExampleService
public class NotificationListenerExampleService extends NotificationListenerService {
@Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
if ((sbn.getNotification().flags & Notification.FLAG_GROUP_SUMMARY) != 0) {
//Ignore the notification
return;
}
int notificationCode = matchNotificationCode(sbn);
if (notificationCode != InterceptedNotificationCode.OTHER_NOTIFICATIONS_CODE) {
String pack = sbn.getPackageName();
String ticker = "";
if (sbn.getNotification().tickerText != null) {
ticker = sbn.getNotification().tickerText.toString();
}
Bundle extras = null;
extras = sbn.getNotification().extras;
String title = extras.getString("android.title");
String text = extras.getCharSequence("android.text").toString();
int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
Bitmap id = sbn.getNotification().largeIcon;
Log.i("Package", pack);
Log.i("Ticker", ticker);
Log.i("Title", title);
Log.i("Text", text);
Intent intent = new Intent("com.github.chagall.notificationlistenerexample");
intent.putExtra("Notification Code", notificationCode);
intent.putExtra("package", pack);
intent.putExtra("ticker", ticker);
intent.putExtra("title", title);
intent.putExtra("text", text);
if (id != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
id.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
intent.putExtra("icon", byteArray);
}
sendBroadcast(intent);
}
}
}
AndroidManifest.xml
<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>
解决方案
推荐阅读
- java - ResourceConfig 实例不包含任何根资源类(eclipse Tomcat)
- mysql - 如何显示没有评论的表格的百分比?
- php - 需要帮助将 SQL 查询转换为 Laravel eloquent
- sonata-admin - 在一个奏鸣曲管理表单的选择中一次显示所有选择选项
- python - 时间序列数据python的Lowess平滑
- amazon-web-services - 如何列出具有名称标签值的 EBS 卷?
- sparql - 显然简单的 Wikidata SPARQL 过滤器查询很慢
- python - 如何用二维而不是一维来表示欧几里得距离?
- javascript - 用于(贝叶斯?)时间序列变化点检测的 Javascript 库
- r - 数值向量到日期的转换