通知提醒对很多需要推送的应用来说是必不可少的,但是有的推送需要声音或者震动,也有的因为开会的话想要一个静音的,那么我们应该如何设置的,于是我就研究了一下,首先我们就要获取到手机系统声音的当前设置,代码如下
AudioManager am = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
final int ringerMode = am.getRingerMode();
ringerMode为手机的系统声音设置的状态值,0位静音,1为震动,2为响铃,下面就是通知的设置
mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotification = new Notification();
Intent intent = new Intent(context, SplashActivity.class);
intent.putExtra("notification_main_flag", 3);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent mPendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
mNotification.icon = R.drawable.ic_launcher1;
mNotification.contentIntent = mPendingIntent;
CharSequence contentTitle = getTime();
mNotification.setLatestEventInfo(context, contentTitle, data,
mPendingIntent);
AudioManager am = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
final int ringerMode = am.getRingerMode();
if (ringerMode == MUTE) {
//
}
if (ringerMode == VIBRATE) {
mNotification.defaults |= Notification.DEFAULT_VIBRATE;// 震动
long v1[] = { 0, 100, 200, 300 }; // 震动频率
mNotification.vibrate = v1;
}
if (ringerMode == SOUND) {
mNotification.defaults |= Notification.DEFAULT_SOUND;// 声音
}
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;// 点击消息后,该消息自动退出
mNotificationManager.cancel(NOTIFICATION_ID);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
如果还有什么不懂得地方,欢迎留言,或者加Android技术交流群 50208422或Android交流群 470707794为你解决