Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android foreground service which can be bound

What is the right way of doing foreground service which I can later bind to it? I have followed Android API demo which includes an example how to create foreground service. There is no example about start service in bind to it at same time.

I want to see one good example of music player service with activity "bound" to it.

Is there any?

I want to do something like:

  • When a program is first (first means that service isn't started yet) started I want to start foreground service which do all the job. User Interface (activity) is just to control that job
  • If user presses home button service must stay up and running (and notification in bar must be present)
  • Now if user click on notification in notification bar activity should start and bind to service (or something like that, the right way) and user takes control over the job.
  • If activity is active and user presses back button activity should be destroyed and also service should be destroyed.

What methods do I have to override to accomplish this task? What is the Android way of doing this?

like image 792
zmeda Avatar asked Mar 21 '11 20:03

zmeda


People also ask

How do you bind a foreground service?

You can also call bindService() from MainActivity. kt to create a Service. Alternatively, you can call bindService() after startForeground() and then bind the Service by calling bindService() . (You can also create a Service with bindService() and then call startForeground() .

What are bound services in Android?

A bound service is the server in a client-server interface. It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC).

What is bound and unbound service in Android?

Bounded services are bounded to an activity which binds it and will work only till bounded activity is alive. while a unbounded service will work till the completion even after activity is destroyed.

What are foreground services Android?

Foreground services perform operations that are noticeable to the user. Foreground services show a status bar notification, so that users are actively aware that your app is performing a task in the foreground and is consuming system resources.


1 Answers

before froyo there was setForeground(true) in Service which was easy, but also easy to abuse.

Now there is startForeGround services which requires a notification to be activated (so the user can see there is a foregroundservice running).

i made this class to control it:

public class NotificationUpdater {
    public static void turnOnForeground(Service srv,int notifID,NotificationManager mNotificationManager,Notification notif) {
        try {
            Method m = Service.class.getMethod("startForeground", new Class[] {int.class, Notification.class});
            m.invoke(srv, notifID, notif);
        } catch (Exception e) {
            srv.setForeground(true);
            mNotificationManager.notify(notifID, notif);
        }
    } 

    public static void turnOffForeground(Service srv,int notifID,NotificationManager mNotificationManager) {
        try {
            Method m = Service.class.getMethod("stopForeground", new Class[] {boolean.class});
            m.invoke(srv, true);
        } catch (Exception e) {
            srv.setForeground(false);
            mNotificationManager.cancel(notifID);
        }
    }
}

then for my media player this update the notification - note the foreground service is only required while media is playing and should be left on after it stops, it a bad practice.

private void updateNotification(){
    boolean playing = ((mFGPlayerBean.getState()==MediaPlayerWrapper.STATE_PLAYING) || 
                        (mBGPlayerBean.getState()==MediaPlayerWrapper.STATE_PLAYING));
    if (playing) {
        Notification notification = getNotification();
        NotificationUpdater.turnOnForeground(this,Globals.NOTIFICATION_ID_MP,mNotificationManager,notification);
    } else {
        NotificationUpdater.turnOffForeground(this,Globals.NOTIFICATION_ID_MP,mNotificationManager);
    }
}

as for binding - you just bind in the normal way in your activity onStart you just make a bindService call as you would bind to any service (it doesnt matter weather it foreground or not)

MediaPlayerService mpService=null;
@Override
protected void onEWCreate(Bundle savedInstanceState) {
     Intent intent = new Intent(this, MediaPlayerService.class);
     startService(intent);
}

@Override
protected void onStart() {
    // assume startService has been called already
    if (mpService==null) {
        Intent intentBind = new Intent(this, MediaPlayerService.class);
        bindService(intentBind, mConnection, 0);
    }
}

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        mpService = ((MediaPlayerService.MediaBinder)service).getService();
    }

    public void onServiceDisconnected(ComponentName className) {
        mpService = null;
    }
};
like image 175
siliconeagle Avatar answered Sep 30 '22 22:09

siliconeagle