2013-07-01
Android 应用开发实例
1. 情景模式
使用TabHost来实现主界面的布局。
设置一组RadioButton来切换不同的情景模式。
对比普通情景模式,定时情景模式需要加上一个TimPicker来显示并设置时间。
自定义情景模式需要自己定义声音的大小。
调节声音的大小、静音、振动等。
// 获得一个AudioManager对象
AudioManager audioMgr = Context.getSystemService(Context.AUDIO_SERVICE);
// AudioManager常量
AUDIO_LOWER
AUDIO_RAISE
AUDIO_SAME
EXTRA_RINGER_MODE
EXTRA_VIBRATE_SETTING
EXTRA_VIBRATE_TYPE
FLAG_PLAY_SOUND
FLAG_VIBRATE
MODE_INVALID
MODE_IN_CALL
MODE_NORMAL
MODE_RINGTONE
// AudioManager方法
adjustVolume
getMode
getRingerMode
getStreamMaxVolume
isSpeakerPhoneOn
loadSoundEffects
playSoundEffect
setMicroPhoneMute
setMode
setRingerMode
setSpeakerPhoneOn
setVibrateSetting
// 设置铃声+振动
audioMgr.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioMgr.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
audioMgr.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON);
// 设置静音模式
audioMgr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
audioMgr.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
audioMgr.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);
// 注册一个定时器
AlarmManager.set(int type, long triggerAtTime, PendingIntent operation);
// 得到一个PendingIntent
PendingIntent.getActivity(Context, int, Intent, int);
PendingIntent.getBroadcast(Context, int, Intent, int);
PendingIntent.getService(Context, int, Intent, int);
// 创建并注册一个定时器
public void ring() {
Intent it = new Intent(RingBroadcastReceiver.RING_CHANGED);
it.putExtra(“checkedId”, R.id.ring02);
PendingIntent pIt = PendingIntent.getBroadcast(this, RingBroadcastReceiver.REQUEST_CODE, it, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP, getTime(), pIt);
}
// 创建广播接收器
public class RingBroadcastReceiver extends BroadcastReceiver {
public void onReceiver(Context context, Intent it) {
}
}
// 处理UI事件
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup02);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangedListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
});
// 在Manifest文件中注册广播接收器
<receiver android:name=”RingBroadcastReceiver”>
<intent-filter>
<action android:name=”com.example.ring.RingProfile.RV_CHANGED” />
<action android:name=”com.example.ring.RingProfile.RING_CHANGED” />
<action android:name=”com.example.ring.RingProfile.VIBRATE_CHANGED” />
<action android:name=”com.example.ring.RingProfile.SILENT_CHANGED” />
</intent-filter>
</receiver>