我已经实现了NotificationListenerService,但它无法正常工作。这是服务:
public class NotificationListener extends NotificationListenerService {
@Override
public void onCreate() {
Log.d("MYAPP", "Created");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.d("MYAPP", "Notification");
}
}
我已在我的清单文件中实现了这个:
<service android:name="com.rodrigopontes.whatsappbubbles.NotificationListener"
android:label="Test"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
这是我从MainActivity初始化它的方式:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
startService(new Intent(this, NotificationListener.class));
}
感谢您的帮助!
答案 0 :(得分:7)
经过多次浪费,我找到了答案。这是Android的一个问题,请参阅here 欢迎使用Android:)
答案 1 :(得分:2)
我有同样的问题。原来,我的应用没有权限,因此无法接收通知。因此,我通过遵循-
启用了通知权限#include "file.h"
int main()
{
int k = HELPERS::GetInteger();
return 0;
}
以上代码将打开通知权限页面,然后选择您的应用并允许。
答案 2 :(得分:1)
也许您尚未向您的应用提供通知访问权限。尝试这样做,它应该可以工作
答案 3 :(得分:0)
这里的一些评论提到了其他解决方案,其中一些解决了我:
https://gist.github.com/xinghui/b2ddd8cffe55c4b62f5d8846d5545bf9
问题是,如果我理解正确的话,服务进程正在运行,但是完全与任何东西断开连接。
gist是一个服务类,它检查您的NotificationListenerService进程是否实际正在运行并且&#34;已连接&#34;无论如何处理操作系统内部的通知,如果不是,重新启动它(或者更确切地说是启用组件,因为以通常的方式启动它正是打破了)。
我已经在Android 7.1.1上尝试过这个解决方案,我无法使用NotificationListenerService,但它确实有效。
答案 4 :(得分:0)
就我而言,NotificationListner
在重新启动后无法正常工作。我正在使用android 10,我已经找到了解决方案。
@Override
public void onListenerConnected() {
super.onListenerConnected();
Log.i("tag","Listner conneted");
tryReconnectService();
}
public void tryReconnectService() {
toggleNotificationListenerService();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
ComponentName componentName =
new ComponentName(getApplicationContext(), Whatsapp_recorder.class);
//It say to Notification Manager RE-BIND your service to listen notifications again inmediatelly!
requestRebind(componentName);
}
}
private void toggleNotificationListenerService() {
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(this, Whatsapp_recorder.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(new ComponentName(this, Whatsapp_recorder.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
尝试一下,它对我有用