gpt4 book ai didi

Android:NotificationListenerService 未再次创建

转载 作者:太空狗 更新时间:2023-10-29 13:21:43 26 4
gpt4 key购买 nike

应用程序第一次因为一些错误而崩溃后,再次创建并绑定(bind)了 NLService,但是在第 2 次或第 3 次之后......,不再创建和绑定(bind) NLService,即使是复选框

设置 > 安全 > 通知访问

勾选了,这种情况怎么办?

网络服务:

import android.content.Intent;
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

public class NLService extends NotificationListenerService {
private String TAG = this.getClass().getSimpleName();
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "==onCreate==");
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "==onDestroy==");
}

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i(TAG, "********** onNotificationPosted");
Log.i(TAG, "ID :"
+ sbn.getId()
+ "\t"
+ sbn.getNotification().tickerText
+ "\t"
+ sbn.getPackageName());
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i(TAG, "********** onNOtificationRemoved");
Log.i(TAG, "ID :"
+ sbn.getId()
+ "\t"
+ sbn.getNotification().tickerText
+ "\t"
+ sbn.getPackageName());
}

@Override public int onStartCommand(Intent intent, int flags, int startId){
Log.d(TAG, "==onStartCommand==");
return super.onStartCommand(intent, flags, startId);
}

@Override public IBinder onBind(Intent intent) {
Log.d(TAG, "==onBind==");
return super.onBind(intent);
}

@Override public void onRebind(Intent intent) {
super.onRebind(intent);
Log.d(TAG, "==onRebind==");
}

@Override public boolean onUnbind(Intent intent) {
Log.d(TAG, "==onUnbind==");
return super.onUnbind(intent);
}
}

并在manifest.xml中注册

<service android:name="com.kpbird.nlsexample.NLService"
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>

最佳答案

还有问题吗?

我处于同样的情况,这个 NLService 似乎不会协作......

作为一个“伪”解决方法,我注意到如果您在调试 session 期间选中和取消选中 Settings > Security > Notification access 并在 onCreate 中放置一个断点() 您可以在服务启动时进行拦截,但不太清楚为什么有时会停止...

另一种可能性是重启设备...

试试吧,也许你会找到解决的方法,我还在努力...


更新

也许服务中发生了一些变化。这是我在多个设备上运行的测试(我已经尝试了好几天)。

安装应用程序后,您可以在设置中启用通知访问权限,然后您可以尝试使用代码。

build.gradle 文件

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "your_application_id"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
}

AndroidManifest.xml文件
这里我们声明并添加带有NotificationListener的服务(我们创建的类名)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="insert_your_package">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".AMain"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<service
android:name=".NotificationListener"
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>
</application>

</manifest>

AMain.java文件

package insert_your_package;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class AMain extends AppCompatActivity {

protected TextView textView;
protected NotificationReceiver notificationReceiver;

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

textView = (TextView) findViewById(R.id.textview);

notificationReceiver = new NotificationReceiver();

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.dev.name.test");

registerReceiver(notificationReceiver, intentFilter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

class NotificationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

String temp = intent.getStringExtra("notification_event") + "\n" + textView.getText();

textView.setText(temp);
}
}
}

NotificationListener.java文件

package insert_your_package;

import android.content.Intent;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

public class NotificationListener extends NotificationListenerService {

private String TAG = this.getClass().getSimpleName();

@Override
public void onNotificationPosted(StatusBarNotification sbn) {

Log.i(TAG, "********** onNotificationPosted");
Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());

Intent i = new Intent("com.dev.name.test");
i.putExtra("notification_event", sbn.getPackageName() + " posted");

sendBroadcast(i);
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {

Log.i(TAG, "********** onNotificationPosted");
Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());

Intent i = new Intent("com.dev.name.test");
i.putExtra("notification_event", sbn.getPackageName() + " removed");

sendBroadcast(i);
}

@Override
public void onDestroy() {

super.onDestroy();
}
}

如您所见,文本添加了发生的每个已发布已删除的通知。现在服务可以正常工作了,我们可以做我们想要的了。

尝试并判断它是否有效。

关于Android:NotificationListenerService 未再次创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27778015/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com