本测试主要是为了测试PreferenceActivity的使用,其中设置了播放背景音乐和开启wifi的设置,也就是本文要讲的
PreferenceActivity。
Android提供了放摆放的工具来定义所有的程序的首选项,并支持既不不许要编写代码的情况写显示这些首选项。可以在一个名为res/xml/settings.xml的资源文件中定义这些首选项:
res/xml/settings.xml
- <?xml version="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>
<?xml version="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
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, NewGameActivity!</string>
- <string name="btntext">开始游戏</string>
- <string name="app_name">NewGame</string>
- <string name="game_level">游戏级别</string>
- <string name="easy_level">菜鸟级</string>
- <string name="general_level">普通级</string>
- <string name="medium_level">高手级</string>
- <string name="hard_level">骨灰级</string>
- <string name="wifiinfo">启动wifi</string>
- <string name="music">音乐</string>
- <string name="music_summaryOn">开启背景音乐</string>
- <string name="music_summaryOff">关闭背景音乐</string>
- <string name="wifi">无线</string>
- <string name="wifi_summaryOn">开启无线网络</string>
- <string name="wifi_summaryOff">关闭无线网络</string>
- </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, NewGameActivity!</string> <string name="btntext">开始游戏</string> <string name="app_name">NewGame</string> <string name="game_level">游戏级别</string> <string name="easy_level">菜鸟级</string> <string name="general_level">普通级</string> <string name="medium_level">高手级</string> <string name="hard_level">骨灰级</string> <string name="wifiinfo">启动wifi</string> <string name="music">音乐</string> <string name="music_summaryOn">开启背景音乐</string> <string name="music_summaryOff">关闭背景音乐</string> <string name="wifi">无线</string> <string name="wifi_summaryOn">开启无线网络</string> <string name="wifi_summaryOff">关闭无线网络</string> </resources>
Settings.java
- package com.hualang.gotogame;
- import android.content.Context;
- import android.os.Bundle;
- import android.preference.PreferenceActivity;
- import android.preference.PreferenceManager;
- public class Settings extends PreferenceActivity {
- private static final String MUSIC = "music";
- private static final boolean MUSIC_DEF = true;
- private static final String WIFI = "wifi";
- private static final boolean WIFI_DEF = true;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- addPreferencesFromResource(R.xml.settings);
- }
- //获得当前音乐的值
- public static boolean getMusic(Context context)
- {
- return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(MUSIC, MUSIC_DEF);
- }
- //获取当前蓝牙选项的值
- public static boolean getWifi(Context context)
- {
- return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(WIFI, WIFI_DEF);
- }
- }
package com.hualang.gotogame;
import android.content.Context;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
public class Settings extends PreferenceActivity {
private static final String MUSIC = "music";
private static final boolean MUSIC_DEF = true;
private static final String WIFI = "wifi";
private static final boolean WIFI_DEF = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
//获得当前音乐的值
public static boolean getMusic(Context context)
{
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(MUSIC, MUSIC_DEF);
}
//获取当前蓝牙选项的值
public static boolean getWifi(Context context)
{
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(WIFI, WIFI_DEF);
}
}
addPreferencesFromResource()方法从XML中读取设置定义,并将其解压缩到当前的活动试图中,所有耗时的解压缩工作都是在PreferenceActivity类中处理的
保存的Preference文件存放在data/data/项目包名/shared_prefs下,名字为“包名_preferences.xml”
这里的就是:
com.hualang.gotogame_preferences.xml
Music.java 用于设置背景音乐
- package com.hualang.gotogame;
- import android.content.Context;
- import android.media.MediaPlayer;
- public class Music {
- private static MediaPlayer mp = null;
- //停止旧歌曲,开始新歌曲
- public static void play(Context context, int resource)
- {
- stop(context);
- if(Settings.getMusic(context))
- {
- mp = MediaPlayer.create(context, resource);
- mp.setLooping(true);
- mp.start();
- }
- }
- //停止音乐
- public static void stop(Context context)
- {
- if(mp!=null)
- {
- mp.stop();
- mp.release();
- mp = null;
- }
- }
- }
package com.hualang.gotogame;
import android.content.Context;
import android.media.MediaPlayer;
public class Music {
private static MediaPlayer mp = null;
//停止旧歌曲,开始新歌曲
public static void play(Context context, int resource)
{
stop(context);
if(Settings.getMusic(context))
{
mp = MediaPlayer.create(context, resource);
mp.setLooping(true);
mp.start();
}
}
//停止音乐
public static void stop(Context context)
{
if(mp!=null)
{
mp.stop();
mp.release();
mp = null;
}
}
}
下面就是核心代码了,这里主要说一下如何调用背景音乐和蓝牙
背景音乐:
首先,开发者必须了解Android的生命周期,当我们运行的时候,会经历onCreate---onStart---onResume,我们可以将设置背景音乐和蓝牙的设置放在此处,代码如下:
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- Music.play(this, R.raw.test);
- if(Settings.getWifi(this))
- {
- wifion();
- }else
- {
- wifioff();
- }
- Log.d("System.out", "onResume-----------");
- }
@Override
protected void onResume() {
// TODO Auto-generated method stub
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
- protected void onPause() {
- // TODO Auto-generated method stub
- super.onPause();
- Music.stop(this);
- Log.d("System.out", "onPause----------------");
- }
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Music.stop(this);
Log.d("System.out", "onPause----------------");
}
Wifi:
首先需要在清单文件中加入权限:
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
- <uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.WAKE_LOCK"/>
我们也将wifi的操作放在onResume中
- public void wifion()
- {
- if(!wifiManager.isWifiEnabled())
- wifiManager.setWifiEnabled(true);
- }
- public void wifioff()
- {
- if(wifiManager.isWifiEnabled())
- wifiManager.setWifiEnabled(false);
- }
public void wifion()
{
if(!wifiManager.isWifiEnabled())
wifiManager.setWifiEnabled(true);
}
public void wifioff()
{
if(wifiManager.isWifiEnabled())
wifiManager.setWifiEnabled(false);
}
上面两个方法用户打开和关闭wifi。
下面的代码通过判断Setting.getWifi(this)的布尔值,如果真,也就是被选中的话,则打卡wifi,否则关闭wifi
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- Music.play(this, R.raw.test);
- if(Settings.getWifi(this))
- {
- wifion();
- }else
- {
- wifioff();
- }
- Log.d("System.out", "onResume-----------");
- }
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Music.play(this, R.raw.test);
if(Settings.getWifi(this))
{
wifion();
}else
{
wifioff();
}
Log.d("System.out", "onResume-----------");
}
NewGameActivity
- package com.hualang.gotogame;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.net.wifi.WifiManager;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class NewGameActivity extends Activity {
- private Button clickme;
- WifiManager wifiManager;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
- clickme = (Button)findViewById(R.id.clickme);
- clickme.setOnClickListener(new OnClickListener()
- {
- @Override
- public void onClick(View v) {
- startNewGameDialog();
- }
- });
- }
- private void startNewGameDialog()
- {
- new AlertDialog.Builder(this)
- .setTitle(R.string.game_level)
- .setItems(R.array.difficulty,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- startGame(which);
- }
- }).show();
- }
- private void startGame(int i)
- {
- //开始游戏
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- menu.add(0,0,0,"设置");
- menu.add(0,1,1,"关于");
- menu.add(0,2,2,"退出");
- return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch(item.getItemId())
- {
- case 0:
- {
- Intent intent = new Intent();
- intent.setClass(NewGameActivity.this, Settings.class);
- startActivity(intent);
- }
- break;
- case 1:
- break;
- case 2:
- finish();
- break;
- }
- return super.onOptionsItemSelected(item);
- }
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- Music.play(this, R.raw.test);
- if(Settings.getWifi(this))
- {
- wifion();
- }else
- {
- wifioff();
- }
- Log.d("System.out", "onResume-----------");
- }
- @Override
- protected void onPause() {
- // TODO Auto-generated method stub
- super.onPause();
- Music.stop(this);
- Log.d("System.out", "onPause----------------");
- }
- public void wifion()
- {
- if(!wifiManager.isWifiEnabled())
- wifiManager.setWifiEnabled(true);
- }
- public void wifioff()
- {
- if(wifiManager.isWifiEnabled())
- wifiManager.setWifiEnabled(false);
- }
- }
package com.hualang.gotogame;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class NewGameActivity extends Activity {
private Button clickme;
WifiManager wifiManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
clickme = (Button)findViewById(R.id.clickme);
clickme.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
startNewGameDialog();
}
});
}
private void startNewGameDialog()
{
new AlertDialog.Builder(this)
.setTitle(R.string.game_level)
.setItems(R.array.difficulty,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startGame(which);
}
}).show();
}
private void startGame(int i)
{
//开始游戏
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,0,0,"设置");
menu.add(0,1,1,"关于");
menu.add(0,2,2,"退出");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case 0:
{
Intent intent = new Intent();
intent.setClass(NewGameActivity.this, Settings.class);
startActivity(intent);
}
break;
case 1:
break;
case 2:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Music.play(this, R.raw.test);
if(Settings.getWifi(this))
{
wifion();
}else
{
wifioff();
}
Log.d("System.out", "onResume-----------");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Music.stop(this);
Log.d("System.out", "onPause----------------");
}
public void wifion()
{
if(!wifiManager.isWifiEnabled())
wifiManager.setWifiEnabled(true);
}
public void wifioff()
{
if(wifiManager.isWifiEnabled())
wifiManager.setWifiEnabled(false);
}
}
运行结果如下:
源代码见附件: