Android foreground service that may be related

What is the right way to do the foreground work with which I can later bind it? I followed an Android API demo that includes an example of creating a front-end service. There is no example of starting a service in conjunction with it at the same time.

I want to see one good example of a music player service with activity associated with it.

Whether there is a?

I want to do something like:

  • When the program is first (first means that the service is not already running), I want to start the front-end service, which does all the work. The user interface (activity) is designed to manage this task.
  • If the user presses the home button, the service should remain operational (and there should be a notification in the bar).
  • Now, if the user clicks on the notification in the notification panel, the action should begin and bind to the service (or something like that, the right way), and the user gains control over the task.
  • If the activity is active and the user clicks the "Back" button, the action must be destroyed, and the service must also be destroyed.

What methods should I override to complete this task? What is the android way to do this?

+5
source share
2 answers

To accomplish a given task, the only thing I need to do is add the following property in AndroidManifest.xml to the definition of my activity

android:launchMode="singleTop"

That's all.

Hi

-3
source

froyo Foreground (true) Service, , .

startForeGround, ( , ).

:

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);
        }
    }
}

- , , , .

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);
    }
}

- onStart, bindService, - ( , )

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;
    }
};
+10

All Articles