Android: NotificationListenerService was not created again

After the first application crash due to some errors the NLService was created and linked again, but after the second or third time ..., the NLService was no longer created or linked, not even the checkbox in

Settings > Security > Notification access

How can i do this?

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>

      

+3


source to share


1 answer


Still a problem?

I'm in the same situation and this NLService doesn't seem to cooperate ...

As a "fake" workaround , I noticed that if you check and uncheck the box Settings > Security > Notification access

during a debug session and put a breakpoint in onCreate (), you can intercept when the service starts, but it's not clear why sometimes it stops ...

Another possibility is to reboot the device ...

try it, maybe you can find a way to solve, I am still trying ...


UPDATE

Perhaps something has changed in the service. This is my test that works on multiple devices (I've tried it for a few days).

After installing the app, you can enable notification access in settings and 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'
}

      



The AndroidManifest.xml file . Here we declare and add a service with a NotificationListener (the name of the class to be created).

<?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 adds every notification posted or removed that happens. Now that the service is working properly, we can do what we want.

Try to tell if it works.

+2


source







All Articles