为什么此NotificationListenerService不起作用
我在与NotificationListenerService作战时运气不佳. 我尝试了很多在这里和那里发现的食谱,尤其是在这样的食谱上.但是它的工作方式并不一致.
I'm fighting against the NotificationListenerService without much luck. I tried a lot of recipes found here and there, especially on so... But it works quite inconsistently.
首先查看代码:
清单:
<service
android:name=".services.NLService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
然后是服务本身:
public class NLService extends NotificationListenerService {
private String TAG = "NLService";
// bind and unbind seems to make it work with Android 6...
// but is never called with Android 4.4...
@Override
public IBinder onBind(Intent mIntent) {
IBinder mIBinder = super.onBind(mIntent);
Log.i(TAG, "onBind");
return mIBinder;
}
@Override
public boolean onUnbind(Intent mIntent) {
boolean mOnUnbind = super.onUnbind(mIntent);
Log.i(TAG, "onUnbind");
isNotificationAccessEnabled = false;
try {
} catch (Exception e) {
Log.e(TAG, "Error during unbind", e);
}
return mOnUnbind;
}
// onCreate is called with Android 4.4
// because the service is explicitly started from the MainActivity.
// Not on Android 6 where the system binds this service itself...
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "********** onCreate");
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i(TAG, "********** onNotificationPosted");
Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i(TAG, "********** onNOtificationRemoved");
Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());
}
}
然后在主活动中启动服务,或者如果需要,我们要求用户启用设置:
And in the main Activity the service is started or we ask the user to enable the setting if needed :
if (!Utilities.hasNotificationAccess(this))
{
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(intent);
Log.i(TAG,"hasNotificationAccess NO");
}
else
{
Log.i(TAG,"hasNotificationAccess YES");
// without this startService, it never works with Android 4.4...
// but this is not needed in Android 6...
Intent mServiceIntent = new Intent(this, NLService.class);
startService(mServiceIntent);
}
显然,已启用通知的安全设置访问权限...
Obviously, the secure setting access for notifications is enabled...
在Android 6上:
On Android 6 :
在NLService本身上,添加了onBind和onUnbind方法使其可以工作,我可以查看onBind日志,并且可以很好地触发onNotificationPosted.但这一直持续到应用程序下次启动时,然后不再绑定,不再出现onNotificationPosted,仅发生什么都没有,直到我返回安全设置以取消选中-重新检查通知访问权限:每次在生产环境中无法启动应用.
On the NLService itself, the addition of the onBind and onUnbind methods make it work, I can se the onBind log, and the onNotificationPosted is triggered nicely. But it lasts until the next launch of the app, then no more binding, no more onNotificationPosted, just nothing happens until I go back to security settings to uncheck-recheck the notifications access: repeating this each time the App is started is not something possible in a production environment.
在Android 4.4上:
On Android 4.4 :
与Android 6相比,在MainActivity中,我需要显式启动NLService,否则它永远不会自行统计.但是,它再次适用于首次启动,直到取消选中-重新检查安全设置之后,它才能再次使用...
In contrast to Android 6, in the MainActivity I need to explicitly start the NLService otherwise it never stats by itself. But once again it works for the first launch and never works again until uncheck-recheck the security setting...
我想念一些明显的东西吗?
Did I miss something obvious ?
Android缓存存在问题.当您在设备上上传应用程序时,操作系统会将您的服务连接到Notification Manager.如果您之前运行过应用,Android会在缓存中找到此服务,因此不会重新连接.
There is a problem with Android caching. When you upload app on the device, OS connects your service to Notification Manager. If you ran your app before, Android founds this service in cache so it's not reconnected.
解决方法:在您的设备上推送该应用之前,重命名您的服务(Android Studio中的重构"选项效果很好)-Android会将该服务识别为新服务并连接到Notification Manager.
Workaround: before pushing the app on your device, rename your service (refactor option in Android Studio works well) - Android will recognize this service as new one and connect to Notification Manager.
https://developer.android.com/reference/android/service/notification/NotificationListenerService.html#onListenerConnected()-连接到Notification Manager后将调用此方法,以便您可以检查服务是否已连接.
https://developer.android.com/reference/android/service/notification/NotificationListenerService.html#onListenerConnected() - this method will be invoked after connecting to Notification Manager so you can check if your service is connected or not.