gpt4 book ai didi

android - 为什么这个 NotificationListenerService 不工作

转载 作者:可可西里 更新时间:2023-11-01 18:57:01 39 4
gpt4 key购买 nike

我在对抗 NotificationListenerService 时运气不佳。我尝试了很多在这里和那里找到的食谱,尤其是关于……但它的效果很不一致。

先看代码:

list :

<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());
}
}

并且在主 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);
}

显然,通知的安全设置访问已启用...

在安卓 6 上:

在 NLService 本身,添加 onBind 和 onUnbind 方法使其工作,我可以看到 onBind 日志,并且很好地触发了 onNotificationPosted。但它一直持续到应用程序的下一次启动,然后不再有绑定(bind),不再有 onNotificationPosted,只是没有发生,直到我返回安全设置以取消选中-重新检查通知访问:每次重复此操作应用程序启动在生产环境中是不可能的。

在 Android 4.4 上:

与 Android 6 相比,在 MainActivity 中我需要显式启动 NLService 否则它永远不会自行统计。但是它再次适用于第一次启动并且在取消选中 - 重新检查安全设置之前永远不会再次运行......

我错过了一些明显的东西吗?

最佳答案

Android 缓存存在问题。当您在设备上上传应用程序时,操作系统会将您的服务连接到通知管理器。如果您之前运行过您的应用,Android 会在缓存中找到此服务,因此它不会重新连接。

解决方法:在您的设备上推送应用之前,重命名您的服务(Android Studio 中的重构选项运行良好)- Android 会将此服务识别为新服务并连接到通知管理器。

https://developer.android.com/reference/android/service/notification/NotificationListenerService.html#onListenerConnected() - 此方法将在连接到通知管理器后调用,因此您可以检查您的服务是否已连接。

关于android - 为什么这个 NotificationListenerService 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33530807/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com