Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide notification of foreground service while activity is visible

Is their a way to start a service as a foreground service and hide the notification while an activity is visible?

Consider a music player, while the app is opened, you don't need the notification (with i.e. the buttons), but whenever the music player is in the background, the notification should be shown.

I know, how to do this, if I DON'T run my service in foreground... But when running in foreground, the service itself needs the notification and shows it and I can't manage the notification by myself...

How can I solve that problem?

like image 398
prom85 Avatar asked Feb 27 '14 11:02

prom85


People also ask

How do I turn off foreground notifications?

How do i remove a foreground notification in Android Lollipop? You can remove your own foreground Notification by calling stopForeground() from your Service , that called startForeground() . For instance in Google Music, if you are playing music then the the notification cannot be swiped away.

Can you start foreground without notification?

As a security feature of the Android platform, you cannot, under any circumstance, have a foregrounded service without also having a notification.

What is a foreground notification?

Foreground services are an advanced Android concept which allows you to display notifications to your users when running long lived background tasks. The notification acts like any other notification, however it cannot be removed by the user and lives for the duration of the service.

Can foreground service be killed by Android?

The Android system stops a service only when memory is low and it must recover system resources for the activity that has user focus. If the service is bound to an activity that has user focus, it's less likely to be killed; if the service is declared to run in the foreground, it's rarely killed.


2 Answers

You can do it like this. One prerequisite to this method is, that your activity must bind the service.

First you start service foreground.

private Notification mNotification;

public void onCreate() {
   ...
   startForeground(1, mNotification);
}

Then in your activity you bind and unbind the service as shown below. BIND_ADJUST_WITH_ACTIVITY is important for keeping service alive for the time it is bound to visible activity.

public void onStart() {
    ...
    Intent intent = new Intent(this, PlayerService.class);
    bindService(intent, mConnection, BIND_ADJUST_WITH_ACTIVITY);
}

public void onStop() {
    ...
    unbindService(mConnection);
}

Now here is the last past. You stop foreground, when at least one client is connected to the service, and you start foreground, when last client disconnects.

@Override
public void onRebind(Intent intent) {
    stopForeground(true); // <- remove notification
}

@Override
public IBinder onBind(Intent intent) {
    stopForeground(true); // <- remove notification
    return mBinder;
}

@Override
public boolean onUnbind(Intent intent) {
    startForeground(1, mNotification); // <- show notification again
    return true; // <- important to trigger future onRebind()
}

When binding a service, you have to consider rules applied by Android. If you bind a not started service, the service will not start automatically unless you specify BIND_AUTO_CREATE flag in addition to BIND_ADJUST_WITH_ACTIVITY flag.

    Intent intent = new Intent(this, PlayerService.class);
    bindService(intent, mConnection, BIND_AUTO_CREATE 
            | BIND_ADJUST_WITH_ACTIVITY);

If service was started with auto creation flag on, and last client unbinds then service will stop automatically. If you want to keep service running you have to start it with startService() method. Basically, your code will look like the one below.

    Intent intent = new Intent(this, PlayerService.class);
    startService(intent);
    bindService(intent, mConnection, BIND_ADJUST_WITH_ACTIVITY);

Calling startService() for already started service has no effect on it, as we do not override onCommand() method.

like image 79
sergej shafarenka Avatar answered Sep 24 '22 02:09

sergej shafarenka


Use following steps:

1.Use ActivityManager to get current package name(i.e the Activity Running on top).

2.check if its your application then do not show the notifications

3.else If it is not your application then show the notifications.

ActivityManager manager =(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
                            List<ActivityManager.RunningTaskInfo> tasks = manager.getRunningTasks(1);
                            String topActivityName = tasks.get(0).topActivity.getPackageName();
                            if(!(topActivityName.equalsIgnoreCase("your package name"))){
 //enter notification code here
}
like image 34
Android Learner Avatar answered Sep 24 '22 02:09

Android Learner