您的位置:首页 > Web前端 > CSS

应用通知栏样式及声音、震动和闪光设置

2016-12-05 14:55 267 查看
通知栏样式:

endingIntent pendingIntent2 = PendingIntent.getActivity(appContext, 0,
new Intent(appContext, MainActivity.class), 0);
// 通过Notification.Builder来创建通知,注意API Level
// API11之后才支持
NotificationManager manager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify2 = new Notification.Builder(appContext)
.setSmallIcon(R.drawable.ic_launcher) // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap
// icon)
// bar上显示的提示文字
.setContentTitle("标题")// 设置在下拉status
// bar后Activity,本例子中的NotififyMessage的TextView中显示的标题
.setContentText(noContent)// TextView中显示的详细内容
//                                .setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent2) // 关联PendingIntent
.setNumber(1) // 在TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。
.getNotification(); // 需要注意build()是在API level
// 16及之后增加的,在API11中可以使用getNotificatin()来代替
notify2.flags |= Notification.FLAG_AUTO_CANCEL;
SoundAndVibrateUtil.setAlarmParams(notify2,appContext);
manager.notify(100, notify2);


声音、震动和闪光都是通过setDefaults方法设定,可以单独设置。这里我直接写了一个工具类用来控制声音和振动,都是采用默认的声音和震动:

package cn.com.bjhj.utils.systemsetting;

import android.app.Notification;
import android.content.Context;
import android.media.AudioManager;

import com.easemob.easeui.hhy.PreferenceUtils;

import cn.com.bjhj.utils.L;

/**
* Created by Jiang on 2016/12/5.
*/

public class SoundAndVibrateUtil {
private static final String TAG = "系统设置";

public static void setAlarmParams(Notification notification, Context context) {
//AudioManager provides access to volume and ringer mode control.
AudioManager volMgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
L.d(TAG,"获取系统设置的铃声模式-=="+volMgr.getRingerMode());
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_响铃+震动
boolean allowRing = PreferenceUtils.getPrefBoolean(context, "allowRing", true);//响铃
boolean allowVibrator = PreferenceUtils.getPrefBoolean(context, "allowVibrator", true);//震动
L.d(TAG,"响铃是否开启-=="+allowRing+"\n"+"震动是否开启-=="+allowVibrator);
if (allowRing){
notification.defaults |= Notification.DEFAULT_SOUND;
}else {
notification.sound = null;
}
if (allowVibrator){
notification.defaults |= Notification.DEFAULT_VIBRATE;
}else {
notification.vibrate = null;
}
notification.flags |= Notification.FLAG_SHOW_LIGHTS;//都给开灯
//                Uri ringTone = null;
//                //获取软件的设置
//                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;
//                        }
//                    }
//                }

break;
default:
break;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: