Android 7.0, NotificationListenerService is no longer working

Asked 2 years ago, Updated 2 years ago, 44 views

Hello
"I made my own class ""NotificationLiseter(*1)"" and it was working until recently, but before I knew it, it stopped working."For example, switching Notification access does not refer to onListenerConnected().
"Once I made a ""NLStest(*2)"" that does nothing and made sure it worked properly, I copied it to (*1), but it didn't work."
I checked the Manifest by changing it thoroughly.

Why doesn't (*1) work?

Tried

  • Rebuild the project
  • Make sure to create another NotificationListener and work
  • Toggle Notification access.The NLStest was switched properly.

I just started developing Android, so I think there are some things that I can't do, but I appreciate your cooperation.

(*1)

@RequiresApi(api=Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationListener extensions NotificationListenerService{

    private static final String TAG = "myDEBUG(reactor)";

    @ Override
    public void onCreate(){
        super.onCreate();

        final int notifId = 4;

        // set the notification to start by tapping
        Intent showTaskIntent= new Intent(getApplicationContext(), MainActivity.class);
        showTaskIntent.setAction(Intent.ACTION_MAIN);
        showTaskIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        showTaskIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent contentIntent=PendingIntent.getActivity(
                getApplicationContext(),
                0,
                showTaskIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
        );

        // Display in Status Bar
        Notification.Builder builder = new Notification.Builder(getApplicationContext());
        builder.setContentTitle("TANSU");
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.icon_tansu);
        builder.setLargeIcon(bitmap);
        builder.setSmallIcon(R.drawable.icon_tansu);
        builder.setWhen(System.currentTimeMillis());
        builder.setContentIntent(contentIntent);
        startForeground(notifId, builder.build());
    }

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

    @ Override
    public void onDestroy()
    {
        super.onDestroy();
    }

    @ Override
    public IBinder onBind (Intent)
    {
        return super.onBind(intent);
    }

    @RequiresApi(api=Build.VERSION_CODES.KITKAT)
    @ Override
    public void onNotificationPosted (StatusBarNotification sbn) {
    }

    @RequiresApi(api=Build.VERSION_CODES.KITKAT)
    @ Override
    public void onNotificationRemoved (StatusBarNotification sbn) {
        Log.i(TAG, "onRemoved() -----------------------------------");
    }

    @ Override
    public void onListenerConnected()
    {
        Log.i(TAG, "NLS is on connected!");
    }

    @ Override
    public void onListenerDisconnected()
    {
        Log.i(TAG, "NLS is on disconnected...");
    }
}

((*2)

@RequiresApi(api=Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NLStest extensions NotificationListenerService{

    private static final String TAG = "myDEBUG(reactor)";

    @ Override
    public void onCreate(){
        super.onCreate();

        final int notifId = 4;

        // set the notification to start by tapping
        Intent showTaskIntent= new Intent(getApplicationContext(), MainActivity.class);
        showTaskIntent.setAction(Intent.ACTION_MAIN);
        showTaskIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        showTaskIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent contentIntent=PendingIntent.getActivity(
                getApplicationContext(),
                0,
                showTaskIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
        );

        // Display in Status Bar
        Notification.Builder builder = new Notification.Builder(getApplicationContext());
        builder.setContentTitle("TANSU");
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.icon_tansu);
        builder.setLargeIcon(bitmap);
        builder.setSmallIcon(R.drawable.icon_tansu);
        builder.setWhen(System.currentTimeMillis());
        builder.setContentIntent(contentIntent);
        startForeground(notifId, builder.build());
    }

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

    @ Override
    public void onDestroy()
    {
        super.onDestroy();
    }

    @ Override
    public IBinder onBind (Intent)
    {
        return super.onBind(intent);
    }

    @RequiresApi(api=Build.VERSION_CODES.KITKAT)
    @ Override
    public void onNotificationPosted (StatusBarNotification sbn) {
    }

    @RequiresApi(api=Build.VERSION_CODES.KITKAT)
    @ Override
    public void onNotificationRemoved (StatusBarNotification sbn) {
        Log.i(TAG, "onRemoved() -----------------------------------");
    }

    @ Override
    public void onListenerConnected()
    {
        Log.i(TAG, "NLS is on connected!");
    }

    @ Override
    public void onListenerDisconnected()
    {
        Log.i(TAG, "NLS is on disconnected...");
    }
}

android

2022-09-30 17:20

2 Answers

Why don't you try the following solutions from your home?

https://stackoverflow.com/questions/33530807/why-is-this-notificationlistenerservice-not-working

It may not work due to cache problems.

About its NotificationLiseter on Android Studio
From the menu, under Refactor->Rename, click
Change to the appropriate name (for example, NotificationLiseter2).I'm sorry)
See if it works.


2022-09-30 17:20

I have also confirmed that it works with rename.


2022-09-30 17:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.