0

当我单击此按钮时,我想关闭应用程序并强制关闭。这是我到目前为止尝试过的代码。但是应用程序没有关闭。按下按钮时如何关闭应用程序?

  import java.io.IOException;
  import android.app.Activity;
  import android.content.DialogInterface;
  import android.content.res.AssetFileDescriptor;
  import android.media.MediaPlayer;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;

      public class MainActivity extends Activity implements  setOnCompletionListener{
        private static final String TAG = null;

        int [] songs;
        MediaPlayer mediaPlayer;
        int current_index = 0; 
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                    songs= new int[] {R.raw.song2,R.raw.song3,R.raw.song7};

        mediaPlayer = MediaPlayer.create(this, songs[0]);

        mediaPlayer.setOnCompletionListener(this);

        mediaPlayer.start();



Button btnstop=(Button)findViewById(R.id.button1); //Play
btnstop.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View vone) 
    {
        mediaPlayer.stop();
    }
});
4

2 回答 2

0
Button btnstop=(Button)findViewById(R.id.button1); //Play
btnstop.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View vone) 
    {
        mediaPlayer.stop();
        mediaPlayer.release ();

        finish();
    }
});
于 2013-03-29T23:38:48.170 回答
0

要调用finish方法,你应该在你的类中扩展Activity,因为finish()方法在Activity类里面......

您也可以尝试将活动作为参数传递给您的类并调用activity.finish()。虽然我不确定这是否有效......

class foo(Activity activity){
    this.activity = activity;
}

//inside onclick
activity.finish();
于 2013-03-30T00:08:30.913 回答