覆盖android中的后退按钮

发布于 2024-10-17 11:49:55 字数 2349 浏览 1 评论 0原文

我必须播放 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");
        }

    }

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

此致。

先感谢您。

I have to play a mp3 file and when click on back button on the device then automatically the song should stop. So I tried below given method. But it is not working.

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

    }

the song is not stopping and the previous page cant display. Please tell me the solution.

Best Regards.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随风而去 2024-10-24 11:49:55

我不知道这是否是您的问题,但是当您在 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);
}

I don't know if this is your problem, but when you call onBackPressed(); in your onkeydown, you are not returning, so the parent.onkeydown is also called, and the 'normal' back is just being 'executed'.

Insert a return statement there so you will not do the normal function from the parent class.

@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);
}
我是有多爱你 2024-10-24 11:49:55

用于处理所有密钥的使用

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

for Handleing All key use

@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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文