I have a Google Music app with package name is com.google.android.music
I can control play, pause, next options by send the command
public static final String SERVICECMD = "com.android.music.musicservicecommand";
public static final String CMDNAME = "command";
AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if(mAudioManager.isMusicActive()) {
Intent i = new Intent(SERVICECMD);
i.putExtra(CMDNAME , CMDSTOP );
YourApplicationClass.this.sendBroadcast(i);
}
I want to exit the Google music (close the Google music app and remove it in notification bar). Which command should I used? I tried the command public static final String CMDSTOP = "stop"; but it only worked in default media player such as com.sec.android.app.music
Is anyone able to tell me how they have done this They probably call stopForeground() when one of their activities returns to the foreground, and startForeground() when they think the user has left (e.g., onUserLeaveHint()).
I am interested in what is considered correct behavior. I do not think there is a definitive "correct behavior" in this area. It is easier to do what the other apps do and leave the Notification around, until the user indicates that they no longer want the background work to run.
Note that while you may want "to keep running when the user is outside the app", the user might not. Certain types of apps (e.g., music players) can safely assume that the user wants the audio to keep going; a metronome, IMHO, does not rise to that level. Please allow the user to configure this behavior, such as through a SharedPreference.