用户按Home时如何停止活动?

时间:2012-05-16 04:36:58

标签: android android-activity

我正在创建一个音乐应用程序。我遇到了问题:当用户按下/ home时,音乐会继续播放。

Here is my APK file

如何让用户返回主要活动并清除当前活动(播放音乐)?

5 个答案:

答案 0 :(得分:1)

您需要覆盖onPause()onStop()并停止播放音乐。

答案 1 :(得分:1)

尝试覆盖onWindowsFocusChanged(boolean hasFocus)方法。

答案 2 :(得分:0)

您可以使用活动的onPause()来停止或暂停Mediaplayer。

像这样覆盖onPause(),

@Override
protected void  onPause()
{
    super.onPause();
  if(mediaplayer!=null)
{
    mediaplayer.pause();
}
}

当您再次输入活动时,请覆盖onResume(),如下所示

@Override
            protected void onResume() {
                super.onResume();

 if(mediaplayer!=null)
{
    mediaplayer.start();
}
}

答案 3 :(得分:0)

在onpause和onstart之间切换很重要 重写onPause()方法以为此实例提供回放控制。 你也可以参考this

package org.example.audio;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class AudioDemo extends Activity implements OnClickListener {
    private static final String TAG = "AudioDemo";
    private static final String isPlaying = "Media is Playing"; 
    private static final String notPlaying = "Media has stopped Playing"; 

    MediaPlayer player;
    Button playerButton;

    public void onClick(View v) {
        Log.d(TAG, "onClick: " + v);
        if (v.getId() == R.id.play) {
            playPause();
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        player = MediaPlayer.create(this, R.raw.robotrock);
        player.setLooping(false); // Set looping

        // Get the button from the view
        playerButton = (Button) this.findViewById(R.id.play);
        playerButton.setText(R.string.stop_label);
        playerButton.setOnClickListener(this);

        // Begin playing selected media
        demoPlay();

        // Release media instance to system
        player.release();

    }

    @Override
    public void onPause() {
        super.onPause();
        player.pause();
    }

    // Initiate media player pause
    private void demoPause(){
            player.pause();
            playerButton.setText(R.string.play_label);
            Toast.makeText(this, notPlaying, Toast.LENGTH_LONG).show();
            Log.d(TAG, notPlaying);
    }

    // Initiate playing the media player
    private void demoPlay(){
            player.start();
            playerButton.setText(R.string.stop_label);
            Toast.makeText(this, isPlaying, Toast.LENGTH_LONG).show();
            Log.d(TAG, isPlaying);
    }

    // Toggle between the play and pause
    private void playPause() {
        if(player.isPlaying()) {
          demoPause();
        } else {
          demoPlay();
        }   
    }
}

答案 4 :(得分:0)

当用户单击后退按钮时,控件进入以下方法(在代码中插入此方法)

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    super.onKeyDown(keyCode, event);
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                    // code to pause or stop the music!!!!
            }
            **return false;**
        }