我在NotificationListenerService周围闲逛,可以在这里阅读和测试大量的示例,提示和技巧......
但是可靠的NotificationListenerService的实现似乎是真实的东西:从Kitkat到Marshmallow,在各种设备上,看起来它是希望让它正常工作而不需要重新启动手机,或者询问更多而不是一次在安全设置中启用访问,即使这样,每次通知登陆时都不会触发onNotificationPosted方法!
我对设备的各种尝试反映了我能读到的所有内容:有时工作,有时不工作,使用相同的代码,相同的设备,重新启动使其在1或2个通知期间工作,然后它就消失了......等等。 我不敢相信没有可靠的方法来使用这项服务(?)
海中的瓶子:有没有人实现了一种可靠的方法来实现这个NotificationListenerService - 可靠的我是说在生产环境中是什么意思?
感谢您阅读
编辑
据我所知,我可以分享一些代码,但并不是所有代码都可以找到:
清单:
<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";
@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());
}
}
在主Activity中,服务已启动,或者我们要求用户在需要时启用该设置:
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");
Intent mServiceIntent = new Intent(this, NLService.class);
startService(mServiceIntent);
}
我得到了OnCreate,但是onNotificationPosted的工作很少。 在Android 6上,它确实永远不会触发onNotificationPosted,尽管该服务创建得很好......