【问题标题】:Home Button Service Background Music主页按钮服务背景音乐
【发布时间】:2014-12-02 14:40:10
【问题描述】:

我有一个正在服务中的背景音乐,它工作正常,但是当我按下 HOME 按钮时,音乐不会停止。帮帮我。

服务:

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() {

}

这是我有意图的活动:

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;

}

【问题讨论】:

    标签: android audio android-service android-homebutton


    【解决方案1】:

    嗯,它是一个服务,服务的生命周期应该比你的 UI 活动或片段更长。从您的代码中,我看不到您正在处理应用程序中的 HOME 按钮单击并停止服务或类似的情况。您可能应该这样做。它只是另一个 H/W 按钮,它触发您可以搭载的事件。

    顺便问一下 System.exit(0); 在你的 android 代码中做什么?

    【讨论】:

    • 当我点击“是”到 AlertDialog 时声音停止
    • 在那里杀死/解除绑定或停止您的服务。 HOME按钮与此有什么关系?按下后退按钮时会显示该警报。 Back 和 Home 是不同的按钮,请记住 Service 不会随着您的活动而消失。
    • 我尝试将其替换为 Intent svc= new Intent();停止服务(svc);但它并没有停止。
    • 使用陈在下面发布的内容。单击主页按钮时的广播。
    • 我不知道该放在哪里。我是新手对不起。 :(
    【解决方案2】:

    因为当你点击HOME_BUTTON时,服务不会调用onDestory()

    您可以为 HOME_BUTTON 注册一个广播接收器,如下所示:

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

    【讨论】:

    • 我会把它放在哪里?
    • 对不起,我是新手。你能告诉我,我在服务类中试过,但你发送的第一行代码的“上下文”有错误。
    【解决方案3】:

    如果你只是想停止服务,那么你可以调用this方法。

    【讨论】:

    • 我试过了,但是每当我去其他活动时它就会停止。我需要 bgmusic 不断。请帮帮我。
    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多