0

在这里,当我单击电源,主页,返回按钮时,我需要暂停音乐,我尝试使用下面的代码..该代码适用于主页和返回按钮..但是在单击电源按钮后,时间声音仍在播放......可以任何人建议我..

     public class MainActivity extends Activity {

Button b1;
MediaPlayer bgmusic;
int length11;
 public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bgmusic=MediaPlayer.create(MainActivity.this,R.raw.bgmusic);
    bgmusic.setLooping(true);
    bgmusic.start(); 

     b1=(Button)findViewById(R.id.button1);
     b1.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub


            Intent i=new Intent(MainActivity.this,Activityyy2.class);
            startActivity(i);

        }
    });
}




@Override
protected void onPause() {
    // TODO Auto-generated method stub

    Log.i("-----------onPause----------", "");
    super.onPause();
        if (bgmusic != null) {
            bgmusic.pause();
           length11=bgmusic.getCurrentPosition();
    }



}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    Log.i("-----------onDestroy----------", "");

    if (bgmusic != null) {
        if (bgmusic.isPlaying()) {

            bgmusic.stop();

        }
        bgmusic.release();
        bgmusic = null;
    }

super.onDestroy();
}   



@Override
protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();

    Log.i("-----------onRestart----------", "");

}

protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

      int valuesnd=settings.getInt("silentMode1", 0);

      Log.i("-----------onResume----------", "");
       Log.i("onResume=-----------",""+valuesnd);

     if(valuesnd==2)
     {
    bgmusic=MediaPlayer.create(MainActivity.this,R.raw.bgmusic);
    bgmusic.seekTo(length11);
    bgmusic.start();
    bgmusic.setLooping(true);
     }
}
      @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
      if ((keyCode == KeyEvent.KEYCODE_HOME)) {
    System.out.println("KEYCODE_HOME");
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
 SharedPreferences.Editor editor = settings.edit();
 editor.putBoolean("silentMode", true);
 editor.putInt("silentMode1", 2);
 editor.commit();

       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);
return true;
}
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
System.out.println("KEYCODE_BACK");

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
          SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", true);
      editor.putInt("silentMode1", 3);
       editor.commit();

return true;
}

 if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        // do what you want with the power button
     System.out.println("KEYCODE_POWER");

     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
     SharedPreferences.Editor editor = settings.edit();
     editor.putBoolean("silentMode", true);
     editor.putInt("silentMode1", 4);
     editor.commit();
        return true;
    }
 return false;
 }

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); 
}
  }
4

0 回答 0