NotificationListenerService中的OnListenerCOnnected没有被调用 [英] OnListenerCOnnected in NotificationListenerService not being called

查看:204
本文介绍了NotificationListenerService中的OnListenerCOnnected没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序仅在华为和其他手机上不起作用.为什么??? 我的MainActivity中有该代码: 软件包pl.ct8.wieprzco.wieprzwatch;

My app only does not work on Huawei and on other phones it does. Why??? I have that code in my MainActivity: package 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类中的简单代码:

And that simple code in NotificationGetService class:

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或其他.请帮助我尝试做那么多时间. 非常感谢约翰!

And it logs onCreate and onStartCommand, but no onListenerConnected or other. Please help i try to do that so much time. Very thanks John!!

推荐答案

与我一起,第一次在手机上安装该应用程序无法正常工作.我必须去设置->通知访问->打开notificationListener.

With me, the first time the app is installed on the phone it didn't work. I had to go to setting --> notification access --> turn on the notificationListener.

完成此操作后,它就起作用了.

After I did this, it worked.

进行一些更改后,它停止工作.事实证明,Android存在缓存问题,因为您可以在此处阅读.您应该做的是重命名(重构)notificationListenerService,它应该可以工作.

When I made some changes, it stopped working. Turned out Android has a caching problem, as you can read here. What you should do is rename (refactor) the notificationListenerService and it should work.

这篇关于NotificationListenerService中的OnListenerCOnnected没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆