如何在Android中屏幕解锁后恢复音乐

时间:2013-08-20 09:37:11

标签: android android-listview

嗨,在我的应用程序中,我需要停止音乐,当我点击电源按钮,homebutton,后退按钮,所以我尝试使用下面的代码其工作fn为主页和后退按钮..但我面临prblem电源按钮..使用下面的代码我能够停止声音,当我按下主页按钮,我的问题是我再次按电源按钮,时间声音正在播放但我需要恢复解锁后的声音,所以我采取了另一个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;
    }
}
  }

2 个答案:

答案 0 :(得分:0)

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

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

当您的应用获得或失去焦点时,此API随时会被激活。我认为这是处理您的方案的最佳位置

答案 1 :(得分: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
     }


}
}

我希望这会有所帮助