I am working on an application where I have a service that should always work in the background. This service is responsible for the notification bar, which should always be visible. There are 2 buttons in the notification panel, where the 1st one is for capturing some data and storing it, and the second one should open an operation that will show all the data. I ran into a problem when, when I close the application, and then press the notification button, which launches the action after starting this action, my notification buttons stop responding. Please note that both buttons work perfectly until the second button starts the action.
Here is the template code for my notification service and for the notification bar button handler
which 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");
Intent buttonsIntent2 = new Intent(this, NotificationBarButtonActivityHandler.class);
buttonsIntent2.putExtra(PENDING_ACTION, SHOW_BOOKMARKS);
contentView.setOnClickPendingIntent(R.id.notificationBarShowBookmarksButton, PendingIntent.getActivity(this, 0, buttonsIntent2, 0));
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);
}
}
,
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!!!");
}
else if(action.equals(NotificationBarService.SHOW_BOOKMARKS)){
CustomLogger.log("---------------- BUTTON FOR SHOW DATA WAS PRESSSED!!!");
Intent intent2;
intent2 = new Intent(this, BookmarkDisplayActivity.class);
startActivity(intent2);
}
}
finish();
}
}
, , , , , , .