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

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

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

规则:

  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. poj 2125(最小割)

    题目链接:http://poj.org/problem?id=2125 思路:将最小点权覆盖转化为最小割模型,于是拆点建图,将点i拆成i,i+n,其中vs与i相连,边容量为w[i]-,i+n与vt相连 ...

  2. Android Studio使用心得 - 常见问题集锦

    FBI Warning:欢迎转载,但请标明出处:http://blog.csdn.net/codezjx/article/details/38669939,未经本人允许请勿用于商业用途,感谢支持! 整 ...

  3. Codeforces Beta Round #25 (Div. 2)--A. IQ test

    IQ test time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  4. okhttp 通过网关请求服务端返回数据

    1.启动类代码 package com.tycoon.service; import org.springframework.boot.SpringApplication; import org.sp ...

  5. 关于new HashMap<>(1)中1的理解(hashMap的加载因子)

    新入公司,阅读代码的时候发现了一行代码,为 Map<String, String> map=new HashMap<>(1); 对于这个括号里面的1不能理解,于是查了资料,大概 ...

  6. [网络通信] OSI七层模型思维导图

    ISO:国际标准化组织:OSI:开放系统互联 (部分描述不准确和不详细)

  7. 【BZOJ4898】[Apio2017]商旅 分数规划+SPFA

    [BZOJ4898][Apio2017]商旅 Description 在广阔的澳大利亚内陆地区长途跋涉后,你孤身一人带着一个背包来到了科巴.你被这个城市发达而美丽的市场所深深吸引,决定定居于此,做一个 ...

  8. Null Coalescing Operator

    w Parse error: syntax error, unexpected '?'

  9. <2013 08 27> 雅思阅读相关

    1.雅思阅读的总体难度不大,但是时间较紧,三段较长的阅读材料和40个题目,总耗时60min.基本上前两个材料可以花15~20min,最后一个材料至少花20min完成. 2.阅读的技巧在于三点:其一,先 ...

  10. Bean\Entity\Model\POJO\Dto\EJB简单解析

    一.Bean 对于Bean而言,只要是Java的类的就可以称为一个Bean, 更用在Spring上,被Spring管理的对象就可以将其称作为Bean. 它不仅仅可以包括对象的属性以及get,set方法 ...