我有一个简单的 NotificationListenerService 设置。
它适用于其他手机的 Android 6 / 7。它曾经在基于 MIUI 的系统中的 Android 6 中工作。但它不适用于 Android 7 的 MIUI 9。
问题是什么?
public class MyNotification extends NotificationListenerService {
Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
/*
These are the package names of the apps. for which we want to
listen the notifications
*/
private static final class ApplicationPackageNames {
public static final String FACEBOOK_PACK_NAME = "com.facebook.katana";
public static final String FACEBOOK_MESSENGER_PACK_NAME = "com.facebook.orca";
public static final String WHATSAPP_PACK_NAME = "com.whatsapp";
public static final String INSTAGRAM_PACK_NAME = "com.instagram.android";
}
/*
These are the return codes we use in the method which intercepts
the notifications, to decide whether we should do something or not
*/
public static final class InterceptedNotificationCode {
public static final int FACEBOOK_CODE = 1;
public static final int WHATSAPP_CODE = 2;
public static final int INSTAGRAM_CODE = 3;
public static final int OTHER_NOTIFICATIONS_CODE = 4; // We ignore all notification with code == 4
}
@Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn){
String packageName = sbn.getPackageName();
String ticker ="";
if(sbn.getNotification().tickerText !=null) {
ticker = sbn.getNotification().tickerText.toString();
}
Bundle extras = sbn.getNotification().extras;
String title = extras.getString("android.title");
String text = extras.getCharSequence("android.text").toString();
Log.i("Package",packageName);
Log.i("Ticker",ticker);
Log.i("Title",title);
Log.i("Text",text);
Intent intent = new Intent("com.arjun.mynotification");
intent.putExtra("packageName", packageName);
intent.putExtra("title", title);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn){
int notificationCode = matchNotificationCode(sbn);
if(notificationCode != InterceptedNotificationCode.OTHER_NOTIFICATIONS_CODE) {
StatusBarNotification[] activeNotifications = this.getActiveNotifications();
if(activeNotifications != null && activeNotifications.length > 0) {
for (int i = 0; i < activeNotifications.length; i++) {
if (notificationCode == matchNotificationCode(activeNotifications[i])) {
Intent intent = new Intent("com.arjun.mynotification");
intent.putExtra("Notification Code", notificationCode);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
break;
}
}
}
}
}
private int matchNotificationCode(StatusBarNotification sbn) {
String packageName = sbn.getPackageName();
if(packageName.equals(ApplicationPackageNames.FACEBOOK_PACK_NAME)
|| packageName.equals(ApplicationPackageNames.FACEBOOK_MESSENGER_PACK_NAME)){
return(InterceptedNotificationCode.FACEBOOK_CODE);
}
else if(packageName.equals(ApplicationPackageNames.INSTAGRAM_PACK_NAME)){
return(InterceptedNotificationCode.INSTAGRAM_CODE);
}
else if(packageName.equals(ApplicationPackageNames.WHATSAPP_PACK_NAME)){
return(InterceptedNotificationCode.WHATSAPP_CODE);
}
else{
return(InterceptedNotificationCode.OTHER_NOTIFICATIONS_CODE);
}
}
}
清单.xml
<service android:name=".MyNotification"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
ADB 日志没有返回任何提示问题的信息?我在其他地方也找不到太多可以进行调查的地方。