Skip to content Skip to sidebar Skip to footer

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 i

Solution 1:

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:

publicclassNotificationUpdater{
    publicstaticvoid turnOnForeground(Service srv,int notifID,NotificationManager mNotificationManager,Notification notif) {
        try {
            Method m = Service.class.getMethod("startForeground", newClass[] {int.class, Notification.class});
            m.invoke(srv, notifID, notif);
        } catch (Exception e) {
            srv.setForeground(true);
            mNotificationManager.notify(notifID, notif);
        }
    } 

    publicstaticvoid turnOffForeground(Service srv,int notifID,NotificationManager mNotificationManager) {
        try {
            Method m = Service.class.getMethod("stopForeground", newClass[] {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.

privatevoidupdateNotification(){
    booleanplaying= ((mFGPlayerBean.getState()==MediaPlayerWrapper.STATE_PLAYING) || 
                        (mBGPlayerBean.getState()==MediaPlayerWrapper.STATE_PLAYING));
    if (playing) {
        Notificationnotification= 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;
@OverrideprotectedvoidonEWCreate(Bundle savedInstanceState) {
     Intent intent = newIntent(this, MediaPlayerService.class);
     startService(intent);
}

@OverrideprotectedvoidonStart() {
    // assume startService has been called alreadyif (mpService==null) {
        Intent intentBind = newIntent(this, MediaPlayerService.class);
        bindService(intentBind, mConnection, 0);
    }
}

privateServiceConnection mConnection = newServiceConnection() {
    publicvoidonServiceConnected(ComponentName className, IBinder service) {
        mpService = ((MediaPlayerService.MediaBinder)service).getService();
    }

    publicvoidonServiceDisconnected(ComponentName className) {
        mpService = null;
    }
};

Solution 2:

To accomplish asked task the only thing I have to do was add following property to AndroidManifest.xml to my activity definition

android:launchMode="singleTop"

That was it.

Regards

Post a Comment for "Android Foreground Service Which Can Be Bound"