我有一个简单的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);
}
}
}
Manifest.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日志未返回任何内容来提示问题是什么? 我也找不到其他地方可以进行调查。
答案 0 :(得分:0)
您是否为应用启用了通知服务?
private boolean isNotificationServiceEnabled(){
String pkgName = getPackageName();
final String flat = Settings.Secure.getString(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;
}
private AlertDialog buildNotificationServiceAlertDialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle(R.string.notification_listener_service);
alertDialogBuilder.setMessage(R.string.notification_listener_service_explanation);
alertDialogBuilder.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS));
}
});
alertDialogBuilder.setNegativeButton(R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// If you choose to not enable the notification listener
// the app. will not work as expected
}
});
return(alertDialogBuilder.create());
}
}