0

嗨,在我的应用程序中,当我单击电源按钮、主页按钮、后退按钮时,我需要停止音乐,所以我尝试使用下面的代码,它的工作 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;
    }
}
  }
4

2 回答 2

0

尝试在您的活动中使用此功能:

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if(hasFocus) 
       //play music
    else
      //pause music
}

当您的应用获得或失去焦点时,此 api 始终在活动中调用。我认为在你的情况下处理你的场景是最好的地方

于 2013-08-20T09:43:48.587 回答
0

在您的 Mainfest 文件中放置这些文件以允许您的应用在解锁屏幕发生任何操作时接收广播消息

<receiver android:name=".ScreenReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>

然后在广播接收上这样做:

public class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
     if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
     {
        Intent intent = new Intent();  
        intent.setClass(context, ScreenLockActivity.class);
        startActivity(intent);          
     }
     else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
     {
      //Perform a specific action if you need
     }


}
}

我希望这有帮助

于 2013-08-20T09:50:26.407 回答