主页按钮服务背景音乐 [英] Home Button Service Background Music

查看:136
本文介绍了主页按钮服务背景音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个背景音乐是​​在服务,它工作正常,但是当我pressed HOME键音乐不会停止。帮帮我吧。

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.

服务:

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;

}

希望你能帮助我THANKYOU:)

HOPE YOU CAN HELP ME THANKYOU :)

推荐答案

如果你只是想停止该服务,那么你可以调用<一个href=\"http://developer.android.com/reference/android/content/Context.html#stopService(android.content.Intent)\"相对=nofollow>此方法。

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

这篇关于主页按钮服务背景音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆