Android:根据Sound Settings>中选中的选项进行振动一般

时间:2010-11-08 16:03:31

标签: android notifications vibrate

我该怎么做?我目前的代码如下所示:

final NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.stat_sys_warning, System.currentTimeMillis());    
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(Intent.ACTION_MAIN, Uri.EMPTY, context, Activity....class).putExtra(...);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, text, contentIntent);
manager.notify(1, notification);

1 个答案:

答案 0 :(得分:2)

请参阅Notification#DEFAULT_ALL的文档以及其下方的DEFAULT_VIBRATE。您目前尚未表示需要DEFAULT_VIBRATE配置(您当前的代码仅选择DEFAULT_SOUND

notification.defaults |= Notification.DEFAULT_VIBRATE;

如果要对Sound Vibrate使用设备默认设置,可以使用按位OR来执行此操作:

notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;

或者,您可以指定要使用 all 默认通知设置:

notification.defaults |= Notification.DEFAULT_ALL;

除此之外,您还需要确保在AndroidManifest.xml文件中指定了VIBRATE permission

<uses-permission android:name="android.permission.VIBRATE" />