转载请注明我的链接,尊重原创 !!!!!!!!!!!!!
在Android系统订制Settings模块的时候需要添加许多自定义的功能,本文就讲一下如何在Settings模块实现通知震动和通知音乐的开关功能实现。
主要思路是在
1:Settings DB中添加两个Switch开关,一个是通知音乐 一个是通知震动 ,关闭的时候所有的Notifiction都没法震动和播放提示音。
2:在framework的notificationManager修改代码添加变量控制
首先了解Setting的音量,亮度许多变量都是存储在framework的settingDB中,现在我们要在这个DB添加两个bool类型的变量
(1) base/core/java/android/provider/Settings.java
/**
* System settings, containing miscellaneous system preferences. This
* table holds simple name/value pairs. There are convenience public static final String[] SETTINGS_TO_BACKUP = {
STAY_ON_WHILE_PLUGGED_IN, // moved to global
WIFI_USE_STATIC_IP,
WIFI_STATIC_IP,
WIFI_STATIC_GATEWAY,
WIFI_STATIC_NETMASK,
WIFI_STATIC_DNS1,
WIFI_STATIC_DNS2,
BLUETOOTH_DISCOVERABILITY,
BLUETOOTH_DISCOVERABILITY_TIMEOUT,
DIM_SCREEN,
SCREEN_OFF_TIMEOUT,
SCREEN_BRIGHTNESS,
SCREEN_BRIGHTNESS_MODE,
SCREEN_AUTO_BRIGHTNESS_ADJ,
VIBRATE_INPUT_DEVICES,
MODE_RINGER_STREAMS_AFFECTED,
VOLUME_VOICE,
VOLUME_SYSTEM,
VOLUME_RING,
VOLUME_MUSIC,
VOLUME_ALARM,
VOLUME_NOTIFICATION,
VOLUME_BLUETOOTH_SCO,
VOLUME_VOICE + APPEND_FOR_LAST_AUDIBLE,
VOLUME_SYSTEM + APPEND_FOR_LAST_AUDIBLE,
VOLUME_RING + APPEND_FOR_LAST_AUDIBLE,
VOLUME_MUSIC + APPEND_FOR_LAST_AUDIBLE,
VOLUME_ALARM + APPEND_FOR_LAST_AUDIBLE,
VOLUME_NOTIFICATION + APPEND_FOR_LAST_AUDIBLE,
VOLUME_BLUETOOTH_SCO + APPEND_FOR_LAST_AUDIBLE,
TEXT_AUTO_REPLACE,
TEXT_AUTO_CAPS,
TEXT_AUTO_PUNCTUATE,
TEXT_SHOW_PASSWORD,
AUTO_TIME, // moved to global
AUTO_TIME_ZONE, // moved to global
TIME_12_24,
DATE_FORMAT,
DTMF_TONE_WHEN_DIALING,
DTMF_TONE_TYPE_WHEN_DIALING,
HEARING_AID,
TTY_MODE,
SOUND_EFFECTS_ENABLED,
HAPTIC_FEEDBACK_ENABLED,
POWER_SOUNDS_ENABLED, // moved to global
DOCK_SOUNDS_ENABLED, // moved to global
LOCKSCREEN_SOUNDS_ENABLED,
SHOW_WEB_SUGGESTIONS,
NOTIFICATION_LIGHT_PULSE,
SIP_CALL_OPTIONS,
SIP_RECEIVE_CALLS,
POINTER_SPEED,
VIBRATE_WHEN_RINGING,
RINGTONE,
LOCK_TO_APP_ENABLED,
NOTIFICATION_SOUND,
ANR_DEBUGGING_MECHANISM,
ANR_DEBUGGING_MECHANISM_STATUS,
NOTIFICATION_VIBRATE, //add by zhongqi.shao on 2016-09-28 start
NOTIFICATION_MUSIC,
KEY_VIBRATE
};
}
(2)base/packages/SettingsProvider/res/values/defaults.xml
false
false
false
(3) base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
//add by zhongqi.shao on 2016-09-28 start
loadBooleanSetting(stmt, Settings.System.NOTIFICATION_VIBRATE,R.bool.def_notification_vibrate);
loadBooleanSetting(stmt, Settings.System.NOTIFICATION_MUSIC,R.bool.def_notification_music);
loadBooleanSetting(stmt, Settings.System.KEY_VIBRATE,R.bool.def_key_vibrate);
//add by zhongqi.shao on 2016-09-28 end
到这SettingDB添加两个bool类型的变量就结束了,别忘记make update-api更新framework一下
这时候我们需要更改NotificationManagerService的逻辑了
(5)base/services/core/java/com/android/server/notification/NotificationManagerService.java
if (hasValidSound) {
/** modify by zhongqi.shao on 2016-09-28 start for settings notifition sound open*/
boolean settingsOpen = (Settings.System.getInt( getContext().getContentResolver(), Settings.System.NOTIFICATION_MUSIC,0) != 0);
if (settingsOpen && hasValidSound) { /** modify by zhongqi.shao on 2016-09-28 end*/
boolean looping =
(notification.flags & Notification.FLAG_INSISTENT) != 0;
AudioAttributes audioAttributes = audioAttributesForNotification(notification);
... ...
@@ -2262,10 +2269,14 @@ public class NotificationManagerService extends SystemService {
// The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback.
final boolean useDefaultVibrate =
(notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
//modify by zhongqi.shao on 2016-09-28 start
final boolean settingsVibrationOpen = (Settings.System.getInt( getContext().getContentResolver(), Settings.System.NOTIFICATION_VIBRATE,0) != 0);
//modify by zhongqi.shao on 2016-09-28 end
if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
&& !(mAudioManager.getRingerModeInternal()
== AudioManager.RINGER_MODE_SILENT)) {
== AudioManager.RINGER_MODE_SILENT) && settingsVibrationOpen) {
mVibrateNotificationKey = record.getKey();
if (useDefaultVibrate || convertSoundToVibration) {
就是在发送通知的地方增加系统变量控制,具体逻辑就多看看源代码吧,这篇博文给定值Settings的童鞋来个思路!
Just Like!Just Do IT!