有時候一些通信軟件須要這些個功能,好比說收到短信,通知等,要求手機發出鈴聲,或震動,或發光以提示用戶知曉。每每手機都是有默認設置的,好比說用戶開啓了鈴聲+震動;只鈴聲不震動;徹底靜音等等... html
推薦百搜技術網:http://www.baisoujs.com android
這個時候就須要有一個規則了,起碼軟件的設置不能跟系統的衝突吧,中間的一些邏輯是要處理好的!以前作過的軟件中有這麼個需求,並且代碼是我負責的,因此總結一下。 ide
思路: spa
1. 軟件應該有個本身的設置配置文件,用以保存,本身的軟件的提醒規則 htm
2. 聽從系統的設置,好比說:系統是徹底靜音的,人家想睡覺啦,你軟件雖然是鈴聲震動全開,也得乖乖閉嘴。 ci
3. 若是有須要提醒了,先獲取系統的配置,而後作邏輯判斷給予什麼樣的提醒。 get
http://www.baisoujs.com/list_android_andarticle.html it
代碼: io
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
//首先須要接收一個Notification的參數 table //http://www.baisoujs.com/detail_137571050099728.html
private void setAlarmParams(Notification notification) {
//AudioManager provides access to volume and ringer mode control.
AudioManager volMgr = (AudioManager) mAppContext.getSystemService(Context.AUDIO_SERVICE);
switch (volMgr.getRingerMode()) { //獲取系統設置的鈴聲模式
case AudioManager.RINGER_MODE_SILENT: //靜音模式,值爲0,這時候不震動,不響鈴
notification.sound = null ;
notification.vibrate = null ;
break ;
case AudioManager.RINGER_MODE_VIBRATE: //震動模式,值爲1,這時候震動,不響鈴
notification.sound = null ;
notification.defaults |= Notification.DEFAULT_VIBRATE;
break ;
case AudioManager.RINGER_MODE_NORMAL: //常規模式,值爲2,分兩種狀況:1_響鈴但不震動,2_響鈴+震動
Uri ringTone = null ;
//獲取軟件的設置
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mAppContext);
if (!sp.contains(SystemUtil.KEY_RING_TONE)){ //若是沒有生成配置文件,那麼既有鈴聲又有震動
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
} else {
String ringFile = sp.getString(SystemUtil.KEY_RING_TONE, null );
if (ringFile== null ){ //無值,爲空,不播放鈴聲
ringTone= null ;
} else if (!TextUtils.isEmpty(ringFile)){ //有鈴聲:1,默認2自定義,都返回一個uri
ringTone=Uri.parse(ringFile);
}
notification.sound = ringTone;
boolean vibrate = sp.getBoolean(SystemUtil.KEY_NEW_MAIL_VIBRATE, true );
if (vibrate == false ){ //若是軟件設置不震動,那麼就不震動了
notification.vibrate = null ;
} else { //不然就是須要震動,這時候要看系統是怎麼設置的:不震動=0;震動=1;僅在靜音模式下震動=2;
if (volMgr.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) == AudioManager.VIBRATE_SETTING_OFF){
//不震動
notification.vibrate = null ;
} else if (volMgr.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) == AudioManager.VIBRATE_SETTING_ONLY_SILENT){
//只在靜音時震動
notification.vibrate = null ;
} else {
//震動
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
}
}
notification.flags |= Notification.FLAG_SHOW_LIGHTS; //都給開燈
break ;
default :
break ;
}
}
|
具體的實現就如代碼那樣子了,註釋也很清楚了,其中SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mAppContext);
這個很少作解釋,就是獲取軟件的配置信息。
固然這個類徹底能夠封裝成一個靜態類來使用,寫的時候是比較噁心的,可是一次痛苦,終身快樂啊,哈哈!
有時候一些通信軟件須要這些個功能,好比說收到短信,通知等,要求手機發出鈴聲,或震動,或發光以提示用戶知曉。每每手機都是有默認設置的,好比說用戶開啓了鈴聲+震動;只鈴聲不震動;徹底靜音等等...
這個時候就須要有一個規則了,起碼軟件的設置不能跟系統的衝突吧,中間的一些邏輯是要處理好的!以前作過的軟件中有這麼個需求,並且代碼是我負責的,因此總結一下。
思路:
1. 軟件應該有個本身的設置配置文件,用以保存,本身的軟件的提醒規則
2. 聽從系統的設置,好比說:系統是徹底靜音的,人家想睡覺啦,你軟件雖然是鈴聲震動全開,也得乖乖閉嘴。
3. 若是有須要提醒了,先獲取系統的配置,而後作邏輯判斷給予什麼樣的提醒。
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
//首先須要接收一個Notification的參數
private void setAlarmParams(Notification notification) {
//AudioManager provides access to volume and ringer mode control.
AudioManager volMgr = (AudioManager) mAppContext.getSystemService(Context.AUDIO_SERVICE);
switch (volMgr.getRingerMode()) { //獲取系統設置的鈴聲模式
case AudioManager.RINGER_MODE_SILENT: //靜音模式,值爲0,這時候不震動,不響鈴
notification.sound = null ;
notification.vibrate = null ;
break ;
case AudioManager.RINGER_MODE_VIBRATE: //震動模式,值爲1,這時候震動,不響鈴
notification.sound = null ;
notification.defaults |= Notification.DEFAULT_VIBRATE;
break ;
case AudioManager.RINGER_MODE_NORMAL: //常規模式,值爲2,分兩種狀況:1_響鈴但不震動,2_響鈴+震動
Uri ringTone = null ;
//獲取軟件的設置
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mAppContext);
if (!sp.contains(SystemUtil.KEY_RING_TONE)){ //若是沒有生成配置文件,那麼既有鈴聲又有震動
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
} else {
String ringFile = sp.getString(SystemUtil.KEY_RING_TONE, null );
if (ringFile== null ){ //無值,爲空,不播放鈴聲
ringTone= null ;
} else if (!TextUtils.isEmpty(ringFile)){ //有鈴聲:1,默認2自定義,都返回一個uri
ringTone=Uri.parse(ringFile);
}
notification.sound = ringTone;
boolean vibrate = sp.getBoolean(SystemUtil.KEY_NEW_MAIL_VIBRATE, true );
if (vibrate == false ){ //若是軟件設置不震動,那麼就不震動了
notification.vibrate = null ;
} else { //不然就是須要震動,這時候要看系統是怎麼設置的:不震動=0;震動=1;僅在靜音模式下震動=2;
if (volMgr.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) == AudioManager.VIBRATE_SETTING_OFF){
//不震動
notification.vibrate = null ;
} else if (volMgr.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) == AudioManager.VIBRATE_SETTING_ONLY_SILENT){
//只在靜音時震動
notification.vibrate = null ;
} else {
//震動
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
}
}
notification.flags |= Notification.FLAG_SHOW_LIGHTS; //都給開燈
break ;
default :
break ;
}
}
|
具體的實現就如代碼那樣子了,註釋也很清楚了,其中SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mAppContext);
這個很少作解釋,就是獲取軟件的配置信息。
固然這個類徹底能夠封裝成一個靜態類來使用,寫的時候是比較噁心的,可是一次痛苦,終身快樂啊,哈哈!