有时候一些通讯软件需要这些个功能,比如说收到短信,通知等,要求手机发出铃声,或震动,或发光以提示用户知晓。

往往手机都是有默认设置的,比如说用户开启了铃声+震动;只铃声不震动;完全静音等等...

这个时候就需要有一个规则了,起码软件的设置不能跟系统的冲突

规则:

  1. 软件应该有个自己的设置配置文件,用以保存,自己的软件的提醒规则

  2. 遵从系统的设置,比如说:系统是完全静音的,人家想睡觉啦,你软件虽然是铃声震动全开,也得乖乖闭嘴。

  3. 如果有需要提醒了,先获取系统的配置,然后做逻辑判断给予什么样的提醒。

代码:

//首先需要接收一个Notification的参数
privatevoid 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;
}elseif(!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;
}elseif(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;
}
}

  

//首先需要接收一个Notification的参数
privatevoid 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;
}elseif(!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;
}elseif(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);就是获取软件的配置信息。

Android中设置自己软件的铃声+震动的更多相关文章

  1. 【转】Android中通知的提示音、震动和LED灯效果小例子

    通知(Notification)是 Android 系统中比较有特色的一个功能,当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现.发出一条通知后,手机最上方 ...

  2. Android中设置TextView的颜色setTextColor

    tv.setTextColor(Color.parseColor("#FFFFFF")); tv.setTextColor(Color.WHITE); tv.setTextColo ...

  3. 【转】Android中设置TextView的颜色setTextColor--代码中设置字体颜色

    原文网址:http://www.cnblogs.com/myphoebe/archive/2012/01/06/2314728.html android中设置TextView的颜色有方法setText ...

  4. 【转】Android中设置TextView的颜色setTextColor

    原文网址:http://www.cnblogs.com/myphoebe/archive/2012/01/06/2314728.html android中设置TextView的颜色有方法setText ...

  5. [转]Android中设置TextView的颜色setTextColor

    [转自]http://txlong-onz.iteye.com/blog/1249609 Android中设置TextView的颜色setTextColor android中设置TextView的颜色 ...

  6. 在Eclipse Android中设置模拟器屏幕大小

    在Eclipse Android中设置模拟器屏幕大小是本文要介绍的内容,主要是来了解并学习Eclipse Android中模拟器的设置,具体关于Eclipse Android内容的详解来看本文. 方法 ...

  7. Android中设置全屏的方法

    在实际的应用程序开发中,我们有时需要把 Activity 设置成全屏显示,一般情况下,可以通过两种方式来设置全屏显示效果.其一,通过在代码中可以设置,其二,通过manifest配置文件来设置全屏. 其 ...

  8. android中设置Animation 动画效果

    在 Android 中, Animation 动画效果的实现可以通过两种方式进行实现,一种是 tweened animation 渐变动画,另一种是 frame by frame animation ...

  9. Android中设置文字大小的定义类型

    在Android中所有的组件可以设置大小,但是在设置大小的时候需要指定其单位,这些单位如下: px(pixels):像素: dip(device independent pixels):依赖于设备的像 ...

随机推荐

  1. jconsole监控JVM

    1.查找catalina.sh,使用tomcat中的catalina.sh 目录地址/opt/apache-tomcat-7.0.82/bin 2.配置JAVA_OPTS JAVA_OPTS=&quo ...

  2. 【BZOJ1997】[Hnoi2010]Planar 2-SAT

    [BZOJ1997][Hnoi2010]Planar Description Input Output Sample Input 2 6 9 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 ...

  3. EasyNVR depends on ffmpeg,yasm/nasm not found or too old. Use --disable-yasm for a crippledbuild

    安装ffmpeg过程中,执行./configure时,报yasm/nasm not found or too old. Use --disable-yasm for a crippledbuild错误 ...

  4. HTML+CSS实现简单三级菜单

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. CodedUI Test 测试WPF程序,无法获取控件属性值的解决方法

    注意注意!ItemStatus 在VS2010的CUIT里面是没有的!需要2013以上的版本才可使用. 公司新程序使用WPF制作,但使用CodedUI Test进行自动化测试的时候,很多控件抓取不到其 ...

  6. this.$apply()

    chooseVideo(e) { this.fileInfo = {} let that = this wx.chooseVideo({ sourceType: ['album', 'camera'] ...

  7. 我的Java开发学习之旅------>Eclipse 项目有红感叹号解决之道

    今天一个读者问我关于Android通过调用Webservice实现天气预报这篇文章的源码下载后出现的错误 Could not find class 'org.ksoap2.transport.Http ...

  8. python requests 使用

    快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Req ...

  9. Java基础—运算符(转载)

    转载自:Java运算符 计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量.我们可以把运算符分成以下几组: 算术运算符 关系运算符 位运算符 逻辑运 ...

  10. Centos 常用系统命令

    一.查看系统硬件信息: 1.CPU # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数 c ...