80,354
社区成员
发帖
与我相关
我的任务
分享
import android.app.Activity;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
public class Activity01 extends Activity {
private MIDIPlayer m_MIDIPlayer=null;
private boolean state_Music=false;
private TextView m_TextView=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_TextView=(TextView)findViewById(R.id.TextView01);
m_MIDIPlayer=new MIDIPlayer(this);
//Loading data
//Obtain the object of Preferences
SharedPreferences settings=getPreferences(Activity.MODE_PRIVATE);
state_Music=settings.getBoolean("bmusic", false);
if (state_Music){
m_TextView.setText("The station of music:START");
state_Music=true;
m_MIDIPlayer.PlayMusic();
}
else{
m_TextView.setText("The station of music:CLOSE");
}
}
public boolean onKeyUp(int keyCode,KeyEvent event){
switch (keyCode){
case keyCode=KeyEvent.KEYCODE_DPAD_UP:
m_TextView.setText("The station of music:START");
state_Music=true;
m_MIDIPlayer.PlayMusic();
break;
case keyCode=KeyEvent.KEYCODE_DPAD_DOWN:
m_TextView.setText("The station of music:CLOSE");
state_Music=false;
m_MIDIPlayer.FreeMusic();
break;
}
return true;
}
public boolean onKeyDown(int keyCode,KeyEvent event){
if ( keyCode == KeyEvent.KEYCODE_BACK){
//Store data before out of the Application
//Obtain the Object:Preferences
SharedPreferences uiState=getPreferences(0);
//Obtain the object of Editor
SharedPreferences.Editor editor=uiState.edit();
//Add values
editor.putBoolean("bmusic", state_Music);
//commit
editor.commit();
if (state_Music){
m_MIDIPlayer.FreeMusic();
}
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}