嗨,在我的应用程序中,当我单击电源按钮、主页按钮、后退按钮时,我需要停止音乐,所以我尝试使用下面的代码,它的工作 fn 用于主页和后退按钮..但我在电源按钮中遇到问题..使用下面的代码我当我按下主页按钮时可以停止声音,我的问题是我再次按下电源按钮时声音正在播放但我需要在解锁后恢复声音,所以我采取了另一个 falg Intent.ACTION_USER_PRESENT 当我解锁屏幕时这个 falg 正在调用但是在这个只调用 onresume() 的标志被触发之前,我无法在屏幕解锁时写出那个时间声音应该像那样播放.. 任何人都建议我谢谢你。
public class MainMenu extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenHeight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter); //register
setContentView(R.layout.mainmenu);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i("","-----------onPause----------");
if (mediaPlayer1 != null) {
if (mediaPlayer1.isPlaying()) {
mediaPlayer1.pause();
Globalclass.length=mediaPlayer1.getCurrentPosition();
}
}
if(Globalclass.power==1)
{
Globalclass.hmecount=1;
Globalclass.backcount=0;
if (mediaPlayer1 != null) {
if (mediaPlayer1.isPlaying()) {
mediaPlayer1.pause();
Globalclass.lengthscreen=mediaPlayer1.getCurrentPosition();
}
}
}
}
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(Globalclass.hmecount==1)
{
mediaPlayer1 = MediaPlayer.create(MainMenu.this, R.raw.bgmusic);
mediaPlayer1.seekTo(Globalclass.length);
mediaPlayer1.setLooping(true);
mediaPlayer1.start();
}
else if(Globalclass.backcount==1)
{
mediaPlayer1 = MediaPlayer.create(MainMenu.this, R.raw.bgmusic);
mediaPlayer1.setLooping(true);
mediaPlayer1.start();
}
else if(Globalclass.power==2)
{
mediaPlayer1 = MediaPlayer.create(MainMenu.this, R.raw.bgmusic);
mediaPlayer1.seekTo(Globalclass.lengthscreen);
mediaPlayer1.setLooping(true);
mediaPlayer1.start();
}
else if(Globalclass.power==1)
{
}
else
{
mediaPlayer1 = MediaPlayer.create(MainMenu.this, R.raw.bgmusic);
mediaPlayer1.setLooping(true);
mediaPlayer1.start();
}
Log.i("","----------length1------------"+Globalclass.length);
Log.i("","----------hmecount------------"+Globalclass.hmecount);
Log.i("","----------backcount------------"+Globalclass.backcount);
Log.i("","----------power------------"+Globalclass.power);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("-----------onDestroy----------","");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Globalclass.hmecount=1;
Globalclass.backcount=0;
System.out.println("KEYCODE_HOME");
Intent intent = new Intent(
Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
System.out.println("KEYCODE_BACK");
Globalclass.backcount=1;
Globalclass.hmecount=0;
Intent intent = new Intent(
Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
return true;
}
return false;
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
}
public class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// DO WHATEVER YOU NEED TO DO HERE
Globalclass.power=1;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// AND DO WHATEVER YOU NEED TO DO HERE
}
else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
Globalclass.power=2;
}
}
}