http://hualang.iteye.com/blog/1163185
本测试主要是为了测试PreferenceActivity的使用,其中设置了播放背景音乐和开启wifi的设置,也就是本文要讲的
PreferenceActivity。
Android提供了放摆放的工具来定义所有的程序的首选项,并支持既不不许要编写代码的情况写显示这些首选项。可以在一个名为res/xml/settings.xml的资源文件中定义这些首选项:
res/xml/settings.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <PreferenceScreen
- xmlns:android="http://schemas.android.com/apk/res/android">
- <CheckBoxPreference
- android:key="music"
- android:title="@string/music"
- android:summaryOn="@string/music_summaryOn"
- android:summaryOff="@string/music_summaryOff"
- android:defaultValue="true"
- />
- <CheckBoxPreference
- android:key="wifi"
- android:title="@string/wifi"
- android:summaryOn="@string/wifi_summaryOn"
- android:summaryOff="@string/wifi_summaryOff"
- android:defaultValue="true"
- />
- </PreferenceScreen>
res/values/strings.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <resources>
- <stringname="hello">HelloWorld,NewGameActivity!</string>
- <stringname="btntext">开始游戏</string>
- <stringname="app_name">NewGame</string>
- <stringname="game_level">游戏级别</string>
- <stringname="easy_level">菜鸟级</string>
- <stringname="general_level">普通级</string>
- <stringname="medium_level">高手级</string>
- <stringname="hard_level">骨灰级</string>
- <stringname="wifiinfo">启动wifi</string>
- <stringname="music">音乐</string>
- <stringname="music_summaryOn">开启背景音乐</string>
- <stringname="music_summaryOff">关闭背景音乐</string>
- <stringname="wifi">无线</string>
- <stringname="wifi_summaryOn">开启无线网络</string>
- <stringname="wifi_summaryOff">关闭无线网络</string>
- </resources>
Settings.java
- packagecom.hualang.gotogame;
- importandroid.content.Context;
- importandroid.os.Bundle;
- importandroid.preference.PreferenceActivity;
- importandroid.preference.PreferenceManager;
- publicclassSettingsextendsPreferenceActivity{
- privatestaticfinalStringMUSIC="music";
- privatestaticfinalbooleanMUSIC_DEF=true;
- privatestaticfinalStringWIFI="wifi";
- privatestaticfinalbooleanWIFI_DEF=true;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- addPreferencesFromResource(R.xml.settings);
- }
- //获得当前音乐的值
- publicstaticbooleangetMusic(Contextcontext)
- {
- returnPreferenceManager.getDefaultSharedPreferences(context).getBoolean(MUSIC,MUSIC_DEF);
- }
- //获取当前蓝牙选项的值
- publicstaticbooleangetWifi(Contextcontext)
- {
- returnPreferenceManager.getDefaultSharedPreferences(context).getBoolean(WIFI,WIFI_DEF);
- }
- }
addPreferencesFromResource()方法从XML中读取设置定义,并将其解压缩到当前的活动试图中,所有耗时的解压缩工作都是在PreferenceActivity类中处理的
保存的Preference文件存放在data/data/项目包名/shared_prefs下,名字为“包名_preferences.xml”
这里的就是:
com.hualang.gotogame_preferences.xml
Music.java用于设置背景音乐
- packagecom.hualang.gotogame;
- importandroid.content.Context;
- importandroid.media.MediaPlayer;
- publicclassMusic{
- privatestaticMediaPlayermp=null;
- //停止旧歌曲,开始新歌曲
- publicstaticvoidplay(Contextcontext,intresource)
- {
- stop(context);
- if(Settings.getMusic(context))
- {
- mp=MediaPlayer.create(context,resource);
- mp.setLooping(true);
- mp.start();
- }
- }
- //停止音乐
- publicstaticvoidstop(Contextcontext)
- {
- if(mp!=null)
- {
- mp.stop();
- mp.release();
- mp=null;
- }
- }
- }
下面就是核心代码了,这里主要说一下如何调用背景音乐和蓝牙
背景音乐:
首先,开发者必须了解Android的生命周期,当我们运行的时候,会经历onCreate---onStart---onResume,我们可以将设置背景音乐和蓝牙的设置放在此处,代码如下:
- @Override
- protectedvoidonResume(){
- //TODOAuto-generatedmethodstub
- super.onResume();
- Music.play(this,R.raw.test);
- if(Settings.getWifi(this))
- {
- wifion();
- }else
- {
- wifioff();
- }
- Log.d("System.out","onResume-----------");
- }
这样,如果保存的CheckBoxPreferences为true,就设置背景音乐,启动wifi,否则就关闭北京音乐,关闭wifi
当我们进入设置界面的时候,进入了onPause,设置完后返回主界面,又变成了onResume,所以背景音乐部分在进入
onPause时停止掉,只有到进入主界面玩游戏的时候再开启,这样要合理些。代码如下:
- @Override
- protectedvoidonPause(){
- //TODOAuto-generatedmethodstub
- super.onPause();
- Music.stop(this);
- Log.d("System.out","onPause----------------");
- }
Wifi:
首先需要在清单文件中加入权限:
- <uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>
- <uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE"/>
- <uses-permissionandroid:name="android.permission.WAKE_LOCK"/>
我们也将wifi的操作放在onResume中
- publicvoidwifion()
- {
- if(!wifiManager.isWifiEnabled())
- wifiManager.setWifiEnabled(true);
- }
- publicvoidwifioff()
- {
- if(wifiManager.isWifiEnabled())
- wifiManager.setWifiEnabled(false);
- }
上面两个方法用户打开和关闭wifi。
下面的代码通过判断Setting.getWifi(this)的布尔值,如果真,也就是被选中的话,则打卡wifi,否则关闭wifi
- @Override
- protectedvoidonResume(){
- //TODOAuto-generatedmethodstub
- super.onResume();
- Music.play(this,R.raw.test);
- if(Settings.getWifi(this))
- {
- wifion();
- }else
- {
- wifioff();
- }
- Log.d("System.out","onResume-----------");
- }
NewGameActivity
- packagecom.hualang.gotogame;
- importandroid.app.Activity;
- importandroid.app.AlertDialog;
- importandroid.content.Context;
- importandroid.content.DialogInterface;
- importandroid.content.Intent;
- importandroid.content.SharedPreferences;
- importandroid.net.wifi.WifiManager;
- importandroid.os.Bundle;
- importandroid.util.Log;
- importandroid.view.Menu;
- importandroid.view.MenuItem;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- publicclassNewGameActivityextendsActivity{
- privateButtonclickme;
- WifiManagerwifiManager;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- wifiManager=(WifiManager)this.getSystemService(Context.WIFI_SERVICE);
- clickme=(Button)findViewById(R.id.clickme);
- clickme.setOnClickListener(newOnClickListener()
- {
- @Override
- publicvoidonClick(Viewv){
- startNewGameDialog();
- }
- });
- }
- privatevoidstartNewGameDialog()
- {
- newAlertDialog.Builder(this)
- .setTitle(R.string.game_level)
- .setItems(R.array.difficulty,
- newDialogInterface.OnClickListener(){
- @Override
- publicvoidonClick(DialogInterfacedialog,intwhich){
- startGame(which);
- }
- }).show();
- }
- privatevoidstartGame(inti)
- {
- //开始游戏
- }
- @Override
- publicbooleanonCreateOptionsMenu(Menumenu){
- menu.add(0,0,0,"设置");
- menu.add(0,1,1,"关于");
- menu.add(0,2,2,"退出");
- returnsuper.onCreateOptionsMenu(menu);
- }
- @Override
- publicbooleanonOptionsItemSelected(MenuItemitem){
- switch(item.getItemId())
- {
- case0:
- {
- Intentintent=newIntent();
- intent.setClass(NewGameActivity.this,Settings.class);
- startActivity(intent);
- }
- break;
- case1:
- break;
- case2:
- finish();
- break;
- }
- returnsuper.onOptionsItemSelected(item);
- }
- @Override
- protectedvoidonResume(){
- //TODOAuto-generatedmethodstub
- super.onResume();
- Music.play(this,R.raw.test);
- if(Settings.getWifi(this))
- {
- wifion();
- }else
- {
- wifioff();
- }
- Log.d("System.out","onResume-----------");
- }
- @Override
- protectedvoidonPause(){
- //TODOAuto-generatedmethodstub
- super.onPause();
- Music.stop(this);
- Log.d("System.out","onPause----------------");
- }
- publicvoidwifion()
- {
- if(!wifiManager.isWifiEnabled())
- wifiManager.setWifiEnabled(true);
- }
- publicvoidwifioff()
- {
- if(wifiManager.isWifiEnabled())
- wifiManager.setWifiEnabled(false);
- }
- }
运行结果如下:
源代码见附件: