覆盖android中的后退按钮

时间:2011-02-11 13:29:10

标签: android override back-button

我必须播放一个mp3文件,当点击设备上的后退按钮时,歌曲应自动停止。所以我尝试了以下给定的方法。但它没有用。

  public void onCreate(Bundle icicle) {

    super.onCreate(icicle);

    setContentView(R.layout.audioplaying);
        play=(ImageView)findViewById(R.id.play);
        stop=(ImageView)findViewById(R.id.stop);

        songid=(TextView)findViewById(R.id.songid);
        status=(TextView)findViewById(R.id.status);

        String s=Songs.song;

        status.setText("Please Wait....!");
        mp=new MediaPlayer();
        try{
        mp.setDataSource(s);
        mp.prepare();
        }
        catch(Exception ex){
            Log.e("Exception",ex.getMessage());
        }
        Log.e("Status","Song is going to Start");
        mp.start();
        start=true;
        Log.e("Status","Song was Started");
        status.setText("Playing...!");
        songid.setText(s);
        play.setOnClickListener(this);

        stop.setOnClickListener(this);

    }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
            && keyCode == KeyEvent.KEYCODE_BACK
            && event.getRepeatCount() == 0) {
        Log.d("CDA", "onKeyDown Called");

        onBackPressed();
    }
    return super.onKeyDown(keyCode, event);
}

public void onBackPressed() {
    Log.d("CDA", "onBackPressed Called");
    audioStreamer.stop();
    audioStreamer.getMediaPlayer().stop();
    if(start)
    {
    mp.stop();
    start=false;
    }
    else{
         Intent setIntent = new Intent(AudioPlay1.this,Songs.class);
         startActivity(setIntent); 
         finish();
    }
    Intent setIntent = new Intent(AudioPlay1.this,Songs.class);
    startActivity(setIntent); 
    finish();
    return;

}
    @Override
    public void onClick(View v) {
        if(v.equals(play)){
        try{
        mp.prepare();
        }
        catch(Exception ex){Log.e("Exception in onclick",ex.toString());}
        mp.start();
        start=true;
        Log.e("Status","Song was Started again");
        status.setText("Playing...!");

        }

        if(v.equals(stop)){

        mp.stop();
        start=false;
        Log.e("Status","Song was stopped");
        status.setText("Song was Stopped");
        }

    }

歌曲没有停止,上一页无法显示。请告诉我解决方案。

最诚挚的问候。

提前谢谢你。

2 个答案:

答案 0 :(得分:14)

我不知道这是不是你的问题,但是当你在onkeydown打电话给onBackPressed();时,你没有回来,所以parent.onkeydown也被召唤,'正常'回来了只是被“执行”。

在那里插入一个return语句,这样你就不会从父类中执行正常的操作。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
            && keyCode == KeyEvent.KEYCODE_BACK
            && event.getRepeatCount() == 0) {
        Log.d("CDA", "onKeyDown Called");

        onBackPressed();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

答案 1 :(得分:10)

处理所有关键用途

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
       //Things to Do
        return true;
    }
    return super.onKeyDown(keyCode, event);
}