Reputation: 1759
After the app crashed because of some bugs for the first time, the NLService was created and bound again, but after 2nd time or 3rd time..., the NLService wasn't created and bound any more, even the checkbox in
Settings > Security > Notification access
is checked, how can I do with this situation?
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);
}
}
and registered in 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>
Upvotes: 4
Views: 2511
Reputation: 312
Still got the problem?
I am in the same situation and this NLService seems won't to be collaborative...
As a "fake" workaround I have notice that if you check and uncheck the Settings > Security > Notification access
during a debug session and put a breakpoint in the onCreate() you can intercept when the service starts but is not so clear why sometimes stops...
Another possibility is to reboot the device...
try maybe you'll find a way to solve, I'm still trying...
UPDATE
Maybe something is changed in the service. This is my test that is working on multiple devices (I've try it for days).
After install the app you can enabling the notification access in settings and than you can try the code.
build.gradle file
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
Here we declare and add the service with NotificationListener (name of the class that we create)
<?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 file
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 file
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();
}
}
as you can see the text add every notification posted or removed that happens. Now that the service is working properly we can make what we want.
Try and tell if it works.
Upvotes: 2