Saik
Saik

Reputation: 1065

Android Notification bar buttons become not responsive

I am working on an app where I have a service that should always run in the background. This service is responsible for a Notification bar that should always be visible. The notification bar has 2 buttons, where the 1st one is for grabbing some data and storing it and the 2nd one should open an activity that will show all the data. I encountered a problem where when I close the application and then press the notification button that starts an activity after that activity starts, my notification buttons stop responding. Note that both buttons work fine before the point where the 2nd button click starts the activity.

Here is a template code for my notification service and for notification bar button handler

service that handles the Notification bar

   public class NotificationBarService extends Service {

        private int notificationID;


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

        @Override
        public int onStartCommand(Intent intent, int flags, int startId){


            notificationID = new Random().nextInt();

            RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
            contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
            contentView.setTextViewText(R.id.title, "Custom notification");
            contentView.setTextViewText(R.id.text, "This is a custom layout");


            //Handle the button for showing bookmarks on custom notification
            Intent buttonsIntent2 = new Intent(this, NotificationBarButtonActivityHandler.class);
            buttonsIntent2.putExtra(PENDING_ACTION, SHOW_BOOKMARKS);
            contentView.setOnClickPendingIntent(R.id.notificationBarShowBookmarksButton, PendingIntent.getActivity(this, 0, buttonsIntent2, 0));


            //Handle the button for adding bookmark on custom notification
            Intent buttonsIntent = new Intent(this, NotificationBarButtonActivityHandler.class);
            buttonsIntent.putExtra(PENDING_ACTION, REGISTER_BOOKMARK);
            contentView.setOnClickPendingIntent(R.id.notificationBarAddBookmarkFromChromeButton, PendingIntent.getActivity(this, 1, buttonsIntent, 0));


            RemoteViews notificationView = new RemoteViews(getPackageName(),
                    R.layout.custom_notification);


            Intent switchIntent = new Intent(this, NotificationBarService.class);
            PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0,
                    switchIntent, 0);

            notificationView.setOnClickPendingIntent(R.id.notificationBarShowBookmarksButton,
                    pendingSwitchIntent);


            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                    .setContent(contentView)
                    .setSmallIcon(R.drawable.notification_small_icon)
                    .setOngoing(true);

            Notification notification = mBuilder.build();

            startForeground(notificationID, notification);

            return START_STICKY;
        }


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

            stopForeground(true);

        }
    }

Class that handles the button press on Notification bar

public class NotificationBarButtonActivityHandler extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String action = (String) getIntent().getExtras().get(NotificationBarService.PENDING_ACTION);

        if (action != null) {
            if (action.equals(NotificationBarService.REGISTER_BOOKMARK)){
                CustomLogger.log("---------------- BUTTON FOR COLLECT DATA WAS PRESSED!!!");

                //Does something here
            }
            else if(action.equals(NotificationBarService.SHOW_BOOKMARKS)){
                CustomLogger.log("---------------- BUTTON FOR SHOW DATA WAS PRESSSED!!!");


                //Notification bar buttons start not responding right after
                //this is executed. Note that this problem only occurs if I close the app
                //and press the notification button to execute this code.
                //Otherwise this works just fine.
                Intent intent2;
                intent2 = new Intent(this, BookmarkDisplayActivity.class);
                startActivity(intent2);
            }
        }


        finish();
    }
}

So basically if I close the application and remove the code that starts the activity, both buttons work as expected but as soon as I start the activity, both buttons stop working.

Upvotes: 4

Views: 392

Answers (1)

Saik
Saik

Reputation: 1065

Ok, I finally solved the issue that I was having with changing the way that I handle button presses. This is what I got now and it works as expected.

In NotificationBarService this is how I handle the listeners for the buttons

Intent addBookmarkIntent = new Intent(this, NotificationBarButtonListener.class);
            addBookmarkIntent.setAction(ADD_BOOKMARK_ACTION);
            PendingIntent pendingAddBookmarkIntent = PendingIntent.getBroadcast(this, 0, addBookmarkIntent, 0);
            contentView.setOnClickPendingIntent(R.id.notificationBarAddBookmarkFromChromeButton, pendingAddBookmarkIntent);

            Intent showBookmarkIntent = new Intent(this, NotificationBarButtonListener.class);
            showBookmarkIntent.setAction(SHOW_BOOKMARK_ACTION);
            PendingIntent pendingShowBookmarkIntent = PendingIntent.getBroadcast(this, 0, showBookmarkIntent, 0);
            contentView.setOnClickPendingIntent(R.id.notificationBarShowBookmarksButton, pendingShowBookmarkIntent);

and then I receive a broadcast even and handle it like this

public static class NotificationBarButtonListener extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            final String action = intent.getAction();
            if(action.equals(ADD_BOOKMARK_ACTION)){
                CustomLogger.log("---------------- BUTTON FOR REGISTER BOOKMARK WAS PRESSED!!! ");


            }
            else if(action.equals(SHOW_BOOKMARK_ACTION)){
                CustomLogger.log("---------------- BUTTON FOR SHOW BOOKMARK WAS PRESSSED!!!");

            }

        }
    }

Note that this required me to add the following line to my manifest

<receiver android:name=".NotificationBarService$NotificationBarButtonListener"/>

Upvotes: 3

Related Questions