【问题标题】:OnListenerCOnnected in NotificationListenerService not being called未调用 NotificationListenerService 中的 OnListenerCONnected
【发布时间】:2016-11-06 10:54:48
【问题描述】:

我的应用程序仅在华为和其他手机上不起作用。为什么??? 我的 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 或其他。请帮助我尝试做这么多时间。 非常感谢约翰!!

【问题讨论】:

    标签: android service android-notifications notification-listener


    【解决方案1】:

    对我来说,第一次在手机上安装该应用时,它不起作用。我必须去设置 --> 通知访问 --> 打开通知监听器。

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

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

    【讨论】:

    • 您实际上用“您应该做的是重命名(重构)notificationListenerService,它应该可以工作。”救了我。我尝试了很多方法,但在更改侦听器名称之前没有任何效果。非常感谢
    • 我尝试重构 - 没有运气。只卸载我的应用程序,重新启动设备,然后安装对我有用的应用程序。这是一场噩梦般的调试,呃。
    【解决方案2】:

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

    在你的 sn-p 中:

    @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() 方法不应被覆盖...覆盖它将阻止 onListenerConnected() 被调用...

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        <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>
        

        【讨论】:

        • 是的,我知道了
        • 同样的问题
        【解决方案5】:

        您在 Android 中检查过这个错误吗? https://issuetracker.google.com/issues/36984668

        问题报告如下:

        我在开发中没有看到这个问题,但从 android 4.4 开始 出来了,我从用户那里收到了很多关于我的应用通知的报告 当他们更新应用程序时,监控就会停止。好像
        NotificationListenerService 变得完全不活动。

        通知侦听器在他们的手机中仍然显示为开启,并且 我检查监听器是否仍然启用也表明它是 启用。

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

        我什至想不出任何方法来以编程方式做任何事情 我猜没有办法停止并重新启动通知。

        【讨论】:

          【解决方案6】:

          您可能正在调试您的应用程序。 在调试模式下使用 NotificationListenerService 时存在一些问题。 您应该运行您的应用程序,授予应用程序收听通知的权限。 如果您已经授予许可,请撤销并再次授予它。 (如果需要,请重启手机) 然后调试器会命中。

          【讨论】:

            【解决方案7】:

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-07-03
              • 2021-06-13
              • 1970-01-01
              相关资源
              最近更新 更多