NotListenerCnnected NotificationListenerService未被调用

时间:2016-11-06 10:54:48

标签: android service android-notifications notification-listener

我的应用程序仅适用于华为及其他手机。为什么??? 我在MainActivity中有这个代码:         包pl.ct8.wieprzco.wieprzwatch;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
    startActivity(intent);
    startService(new Intent(this,NotificationGetService.class));
}

}

NotificationGetService类中的简单代码:

public class NotificationGetService extends NotificationListenerService{
SharedPreferences sharedPreferences;
SharedPreferences.Editor spEditor;

@Override
public void onCreate(){
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    spEditor = sharedPreferences.edit();
    Log.e("jej","swiat powiadomien");
    super.onCreate();
}

@Override
public void onListenerConnected(){
    super.onListenerConnected();
    Log.e("startCommand","yeah");
}

@Override
public int onStartCommand(Intent intent,int flags, int startId){
    Log.e("startCommand","yes");
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onNotificationPosted(StatusBarNotification sbn){
    super.onNotificationPosted(sbn);
    spEditor.putInt("notificationCount",sharedPreferences.getInt("notificationCount",0)+1);
    spEditor.apply();
    Log.e("ADDED","YESSSSSS");

}

@Override
public void onNotificationRemoved(StatusBarNotification sbn){
    super.onNotificationRemoved(sbn);
    spEditor.putInt("notificationCount",sharedPreferences.getInt("notificationCount",1)-1);
    spEditor.apply();
    Log.e("REMOVED","YESSSSSS");
}

}

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <service android:name=".UpdateTimeService"/>
    <service android:name=".NotificationGetService"
        android:label="@string/service_name"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
    </service>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


</application>

它会记录onCreate和onStartCommand,但没有onListenerConnected或其他。请帮助我尝试这么多时间。 非常感谢John !!

7 个答案:

答案 0 :(得分:2)

对我来说,第一次在手机上安装应用程序时它没有用。我不得不去设置 - &gt;通知访问 - &gt;打开notificationListener。

在我这样做之后,它起作用了。

当我做了一些更改时,它停止了工作。原来Android有一个缓存问题,因为你可以阅读here。你应该做的是重命名(重构)notificationListenerService它应该工作。

答案 1 :(得分:2)

也许只是因为你输入了错误的loggin标签?

在您的代码段中:

df1[df2, on = c(c1="c4", c2="c5",c3="c6"),nomatch=0]

这两个@Override public void onListenerConnected(){ super.onListenerConnected(); Log.e("startCommand","yeah"); } @Override public int onStartCommand(Intent intent,int flags, int startId){ Log.e("startCommand","yes"); return super.onStartCommand(intent, flags, startId); } 调用都有标记Log.e() ...

以防相关:"startCommand"方法不应被覆盖...覆盖它会阻止NotificationListernerService.onBind()被调用...

答案 2 :(得分:0)

您是否已在服务清单中声明了必要的权限和意图过滤器,如下所示?

<service android:name=".NotificationListener"
      android:label="@string/service_name"
      android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
 <intent-filter>
     <action android:name="android.service.notification.NotificationListenerService" />
 </intent-filter>

答案 3 :(得分:0)

根据我的经验,弄乱onBind()也会阻止它被调用。

答案 4 :(得分:0)

您是否在Android中检查过此错误? https://issuetracker.google.com/issues/36984668

问题报告显示:

  

我在开发过程中没有看到这个问题,但是从Android 4.4开始   我收到了很多来自用户的报告我的应用通知   监控只是在他们更新应用程序时停止。就像是   NotificationListenerService刚刚变为完全不活动状态。

     

通知监听器仍在手机中显示为   我检查是否仍然启用了监听器也表明它是   启用。

     

我必须让用户在他们开始获取之前重新启动   再次通知。我没有在android 4.3中看到这种行为

     

我甚至无法想到以编程方式做任何事情的任何方式   因为我猜没有办法停止并重新启动通知。

答案 5 :(得分:0)

您可能正在调试应用程序。 在调试模式下使用NotificationListenerService时会遇到一些问题。 您应该运行您的应用程序,授予该应用程序侦听通知的权限。 如果您已获得许可,则可以将其撤消并重新获得许可。 (如果需要,请重启手机) 然后,调试器将命中。

答案 6 :(得分:0)

如果您在运行 Android 4.3 或 4.4 的设备上运行也会发生这种情况,因为 onListenerConnected() 直到 Android 5.0 才被引入。

相反,在这些旧设备上收听 onBind()