Android:根据"声音设置">"常规"中选中的选项进行振动

Bry*_*eld 5 notifications android vibration

我怎样才能做到这一点?我目前的代码如下所示:

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);
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 2

请参阅Notification#DEFAULT_ALL及其DEFAULT_VIBRATE下面的文档。您当前并未表明您想要该DEFAULT_VIBRATE配置(您当前的代码仅选择DEFAULT_SOUND.

notification.defaults |= Notification.DEFAULT_VIBRATE;
Run Code Online (Sandbox Code Playgroud)

如果您想对声音和振动使用设备默认设置,可以使用按位或来实现:

notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
Run Code Online (Sandbox Code Playgroud)

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

notification.defaults |= Notification.DEFAULT_ALL;
Run Code Online (Sandbox Code Playgroud)

除此之外,您还需要确保您拥有文件中指定的VIBRATE权限AndroidManifest.xml

<uses-permission android:name="android.permission.VIBRATE" />
Run Code Online (Sandbox Code Playgroud)