应用程序因为第一次出现一些错误而崩溃后,NLService又被创建并绑定了,但是在第二次或第三次......之后,NLService还没有被创建和绑定,甚至是
中的复选框 Settings > Security > Notification access
已经过检查,我该怎么办?
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>
答案 0 :(得分:2)
还有问题吗?
我处于相同的情况,这个NLService似乎不会合作......
作为“假”解决方法我注意到如果您在调试会话期间检查并取消选中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 file
这里我们使用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();
}
}
正如您所看到的那样,文字会添加发生的每个通知发布或已移除。现在服务正常运行,我们可以做出我们想要的东西。
尝试并判断它是否有效。