NotationListenerService 中的 OnListenerCOnected 未被调用

2023-12-24

我的应用程序仅不适用于华为,但在其他手机上可以。为什么??? 我的 MainActivity 中有该代码:package pl.ct8.wieprz.wieprz watch;

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 有一个缓存问题,正如你所读到的here https://stackoverflow.com/a/40825389/7262915。您应该做的是重命名(重构)notificationListenerService,它应该可以工作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NotationListenerService 中的 OnListenerCOnected 未被调用 的相关文章

随机推荐