即使授予权限,NotificationListenerService也无法正常工作

时间:2014-12-13 09:53:55

标签: android android-notifications

以下是我捕获通知的代码。我不明白为什么 onNotificationPosted 没有被解雇。

我从“设置”>中提供我的应用通知访问权限安全性和MyNotifService中的 onCreate 也会被解雇,但 onNotificationPosted 不会被解雇。

我错过了什么或做错了什么?

来自我的 MainActivity' onCreate()功能:

    Intent intent = new Intent(this, MyNotifService.class);
    this.startService(intent);

我的服务代码扩展 NotificationListenerService

@SuppressLint("NewApi")

公共类MyNotifService扩展了NotificationListenerService {

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    super.onNotificationPosted(sbn);        
    Log.v("focus", "in onNotificationPosted() of MyNotifService");      
}

@Override
public void onCreate() {
    super.onCreate();       
    Log.v("focus", "in onCreate() of MyNotifService");      
}

}

清单文件中,我有:

    <service
        android:name="com.mavdev.focusoutfacebook.notifications.MyNotifService"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>

2 个答案:

答案 0 :(得分:2)

它似乎有点不稳定,但这是这种情况下的实际修复:

@Override
public IBinder onBind(Intent intent) {
    return super.onBind(intent);
}

答案 1 :(得分:0)

我一直试图让它在第二天工作,但我成功了。尝试在onStartCommand方法的末尾添加这些行。

而且,它几乎没有作用,但是请尝试在清单文件中的活动之后声明您的服务。

这个带有示例代码的注释对我真的很有帮助:https://stackoverflow.com/a/41521664/10652152

相关问题