1

In my app I use a service for background work(send data to server) when my app is in the background.So I create a notification which tells to the user that app is running in the background.I want when the user taps on the notification my app to come to the foreground so I use an Intent like this that android use to launch my app

   NotificationCompat.Builder mBuilder =
   new NotificationCompat.Builder(this)
   .setSmallIcon(R.drawable.final_driver_notification_icon)
   .setContentTitle("...")
   .setContentText("...");

    final Intent notintent = new Intent(getApplicationContext(),EntryScreen.class);

    notintent.setAction(Intent.ACTION_MAIN);

    notintent.addCategory(Intent.CATEGORY_LAUNCHER);

    PendingIntent pendint = PendingIntent.getActivity(this, 0, notintent, 0);

    mBuilder.setContentIntent(pendint);

    int notID = 001;


    startForeground(notID, mBuilder.build());

In the emulator and some physical devices works perfect.But in some other it launches the app from the start(It puts Entry screen in the top). Any idea why is this happening?

  • It could be because the activity was maybe killed by android to increase resource. I'm not sure though. You should put a log in onDestroy and check if that ever executes. – Shivam Verma Jul 15 '14 at 15:27
  • No onDestroy isn't called!And my activity that running right before app goes to background is still active because when i press back after app launch from notification i can see it.In some devices this Intent puts EntryScreen in the top of the stack but when press app icon it works perfect!It brings app foreground without starting a new instance of EntryScreen! – Psirogiannis Dimitris Jul 15 '14 at 15:39
  • i think you should remove this line " notintent.setAction(Intent.ACTION_MAIN);". It will always start your launcher screen. – Rahul Aug 01 '17 at 04:58

1 Answers1

0

You should start your service with startForeground(). this is the way to start a service in foreground and report by a notification. Android - implementing startForeground for a service?

or try something like this:

public void initForeground() {
        Intent intentForeground = new Intent(this, Activity.class);
        intentForeground.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentForeground,0);
        pendingIntent.cancel();
        pendingIntent = PendingIntent.getActivity(this, 0, intentForeground,0);          

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.icon)
                .setContentIntent(pendingIntent)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(getString(R.string.something))
                .setTicker(getString(R.string.something))
                .setAutoCancel(false);    

        Notification notification = mBuilder.build();

        startForeground(myID, notification);
    }

Hope it helps you!

Community
  • 1
  • 1