操作栏中的后退箭头无法正常工作,我想按下后退箭头进入MainActivity,但它没有显示任何错误,也没有让我回头。 我通过在Manifest xml中调用ParentingActivity来尝试它,它在那里工作得很好,但是当我按下后退箭头时我的音乐播放器仍然播放歌曲。我想在按下后退箭头时完成当前活动。以下是代码;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.os.Handler;
public class Datadetail extends AppCompatActivity implements OnSeekBarChangeListener{
TextView txtname;
TextView txtaarti;
String[] itemname;
int position;
int resID;
private SeekBar seekbar;
ImageButton buttonPlay;
private MediaPlayer mplayer;
private Handler mHandler = new Handler();
private Utilities utils;
private TextView songCurrentDurationLabel;
private TextView songTotalDurationLabel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.data_detail);
Intent i = getIntent();
position = i.getExtras().getInt("position");
itemname = i.getStringArrayExtra("itemname");
txtname = (TextView) findViewById(R.id.textView2);
txtname.setText(itemname[position]);
txtaarti = (TextView) findViewById(R.id.textView3);
txtaarti.setText(Aarti.aartitxt[position]);
seekbar = (SeekBar) findViewById(R.id.seekBar);
seekbar.setOnSeekBarChangeListener(this);
seekbar.setEnabled(true);
songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);
utils = new Utilities();
mplayer = new MediaPlayer();
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
buttonPlay = (ImageButton) findViewById(R.id.imageButton);
playsong(position);
buttonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mplayer.isPlaying()) {
if (mplayer != null) {
mplayer.pause();
buttonPlay.setImageResource(R.mipmap.ic_play);
}
} else {
if (mplayer != null) {
mplayer.setOnCompletionListener(completionListener);
mplayer.start();
buttonPlay.setImageResource(R.mipmap.ic_pause);
}
}
}
});
}
public void playsong(int position) {
switch (position){
case 0:
resID = R.raw.ganesh_aarti;
break;
case 1:
resID = R.raw.durga_aarti;
break;
}
if (mplayer !=null){
mplayer.release();
}
mplayer = MediaPlayer.create(this, resID);
buttonPlay.setImageResource(R.mipmap.ic_play);
seekbar.setProgress(0);
seekbar.setMax(100);
updateProgressBar();
}
public void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask,100);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
int totalDuration = mplayer.getDuration();
int currentDuration = mplayer.getCurrentPosition();
// Displaying Total Duration time
songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
// Displaying time completed playing
songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));
// Updating progress bar
int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
//Log.d("Progress", ""+progress);
seekbar.setProgress(progress);
// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}
};
@Override
public void onBackPressed() {
if (mplayer.isPlaying()) {
mplayer.stop();
}
mHandler.removeCallbacks(mUpdateTimeTask);
mplayer.release();
super.onBackPressed();
}
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekbar) {
// TODO Auto-generated method stub
mHandler.removeCallbacks(mUpdateTimeTask);
}
@Override
public void onStopTrackingTouch(SeekBar seekbar) {
// TODO Auto-generated method stub
mHandler.removeCallbacks(mUpdateTimeTask);
int totalDuration = mplayer.getDuration();
int currentPosition = utils.progressToTimer(seekbar.getProgress(), totalDuration);
// forward or backward to certain seconds
mplayer.seekTo(currentPosition);
// update timer progress again
updateProgressBar();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:
Intent parentActivityIntent = new Intent(this, MainActivity.class);
parentActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(parentActivityIntent);
finish();
return true;
case R.id.action_name:
Intent intent=new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.putExtra(android.content.Intent.EXTRA_TEXT, Aarti.aartitxt[position]);
startActivity(Intent.createChooser(intent, "How do you want to share?"));
return true;
}
return super.onOptionsItemSelected(item);
}
MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mplayer.seekTo(0);
buttonPlay.setImageResource(R.mipmap.ic_play);
}
};
}
答案 0 :(得分:1)
使用case android.R.id.home:
代替case R.id.home: