JBA*_*JBA 15 android android-notifications
我在没有太多运气的情况下与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>
Run Code Online (Sandbox Code Playgroud)
那么服务本身:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
在主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");
// 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);
}
Run Code Online (Sandbox Code Playgroud)
显然,启用了通知的安全设置访问权限......
在Android 6上:
在NLService本身,添加onBind和onUnbind方法使它工作,我可以查看onBind日志,并且onNotificationPosted被很好地触发.但它一直持续到下一次启动应用程序,然后没有更多绑定,没有更多onNotificationPosted,只是没有任何反应,直到我回到安全设置取消选中 - 重新检查通知访问:每次启动应用程序时重复此操作是不可能的在生产环境中.
在Android 4.4上:
与Android 6相比,在MainActivity中我需要显式启动NLService,否则它本身就不会有统计信息.但是它再次适用于第一次启动,并且在取消检查安全设置之前永远不会再次运行...
我错过了一些明显的东西吗
Kon*_*ski 26
Android缓存存在问题.在设备上上传应用程序时,操作系统会将您的服务连接到Notification Manager.如果您之前运行过您的应用,Android会在缓存中创建此服务,因此不会重新连接.
解决方法:在您的设备上推送应用程序之前,重命名您的服务(Android Studio中的重构选项效果很好) - Android会将此服务识别为新服务并连接到Notification Manager.
https://developer.android.com/reference/android/service/notification/NotificationListenerService.html#onListenerConnected() - 连接到Notification Manager后将调用此方法,以便检查您的服务是否已连接.
| 归档时间: |
|
| 查看次数: |
9375 次 |
| 最近记录: |