0

I have a background music which is in Service, it works fine but when I pressed the HOME Button the music won't stop. Help me.

Service:

public class BackgroundSoundService extends Service {

private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {

    return null;
}
@Override
public void onCreate() {
    super.onCreate();
    player = MediaPlayer.create(this, R.raw.haha);
    player.setLooping(true); // Set looping
    player.setVolume(100,100);

}
public int onStartCommand(Intent intent, int flags, int startId) {
    player.start();
    return 1;
}

public void onStart(Intent intent, int startId) {
    // TO DO
}
public IBinder onUnBind(Intent arg0) {
    // TO DO Auto-generated method
    return null;
}

public void onStop() {

}
public void onPause() {

}
@Override
public void onDestroy() {
    player.stop();
    player.release();
}

@Override
public void onLowMemory() {

}

This is my Activity where have the intent:

public class MainActivity extends Activity implements OnClickListener {


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


   Intent svc=new Intent(this, BackgroundSoundService.class);//This is the intent
    startService(svc);  

    View adventuretime1 = this.findViewById(R.id.button1);
    adventuretime1.setOnClickListener(this);

    View advanced2 = this.findViewById(R.id.button2);
    advanced2.setOnClickListener(this);


}

            @Override
            public void onBackPressed(){

              new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
              .setMessage("Are you sure you want to exit?")
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                       @Override
                                       public void onClick(DialogInterface dialog, int which) {
                                          Intent intent = new Intent(Intent.ACTION_MAIN);
                                          intent.addCategory(Intent.CATEGORY_HOME);
                                          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                          startActivity(intent);
                                          System.exit(0);
                                       }
             }).setNegativeButton("No", null).show();
            }           


public void onClick(View v) {

    switch(v.getId()){
    case R.id.button1:
        Intent button1 = new Intent(this, AdventureTime.class);
        startActivity(button1);
        break;

    case R.id.button2:
        Intent button2 = new Intent(this, AdvancedKids.class);
        startActivity(button2);
        break;

}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

3 Answers3

0

Well it is a Service and Services are supposed to have a longer life time that your UI activity or fragments. From your code I don't see the case where you are handling the HOME button click in your app and stopping the service or likewise. You should probably be doing that.Its just another H/W button which triggers events on which you can piggyback.

By the way whats System.exit(0); doing in your android code?

Nazgul
  • 1,892
  • 1
  • 11
  • 15
0

Because when you click the HOME_BUTTON,the service will not call onDestory().

You can register a broadcastReceiver for the HOME_BUTTON,like this:

context.registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            //destory the service if you want
        }
    }, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
Chen
  • 1
  • 3
0

If you just want to stop the Service, then you can call this method.