首先要把android的键盘的上下键开启方法如下:寻找.android\avd\2,2avd\config.ini (用记事本打开)寻找hw.dpad,如果他的值为no,则改为yea。这样android模拟器中的上下键就能使用了。
设计布局文件,在布局文件中添加一个TextView:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
主要的Java代码如下所示:
MainActivity.java
package wang.md.shared_preferences;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private MIDIPlayer mMIDIPlayer=null;
private boolean mbMusic=false;
private TextView mTextView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView=(TextView) this.findViewById(R.id.textview1);
mMIDIPlayer=new MIDIPlayer(this);
SharedPreferences settings=getPreferences(Activity.MODE_PRIVATE);
mbMusic=settings.getBoolean("bmusic",false);
if(mbMusic)
{
mTextView.setText("当前音乐状态:开");
mbMusic=true;
mMIDIPlayer.playMusic();
}
else
{
mTextView.setText("当前音乐状态:关");
}
}
public boolean onKeyUp(int keyCode,KeyEvent event)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_DPAD_UP:
mTextView.setText("当前音乐状态:开");
mbMusic=true;
mMIDIPlayer.playMusic();
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
mTextView.setText("当前音乐状态:关");
mbMusic=false;
mMIDIPlayer.freeMusic();
break;
}
return true;
}
public boolean onKeyDown(int keyCode,KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
{
SharedPreferences uiState=getPreferences(0);
SharedPreferences.Editor editor=uiState.edit();
editor.commit();
if(mbMusic)
{
mMIDIPlayer.freeMusic();
}
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
另外还要写一个音乐播放类:
MIDIPlayer.java
package wang.md.shared_preferences;
import java.io.IOException;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
public class MIDIPlayer {
public MediaPlayer playerMusic = null;
private Context mContext = null;
public MIDIPlayer(Context context) {
mContext = context;
}
/* 播放音乐 */
public void playMusic() {
/* 装载资源中的音乐 */
playerMusic = MediaPlayer.create(mContext, R.raw.start);
/* 设置是否循环 */
playerMusic.setLooping(true);
try {
playerMusic.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
playerMusic.start();
}
/* 停止并释放音乐 */
public void freeMusic() {
if (playerMusic != null) {
playerMusic.stop();
playerMusic.release();
}
}
}
另外在res目录下创建一个raw文件在里面添加你需要播放的音乐文件