本文介绍了振动不适用于通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试设置振动和声音以进行通知。由于某种原因,它对我不起作用:(这是我正在尝试的代码
I am trying to set vibrate and sound for notification. For some reason its not working for me :( Here is the code what I am trying
NotificationManager notificationManager = getNotificationManager();
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
builder.setSound(alarmSound);
builder.setVibrate(new long[] { 1000, 1000, 1000 });
Notification notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true).setContentTitle(title).setPriority(Notification.PRIORITY_HIGH)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msgToDisply))
.setContentText(msgToDisply).build();
notificationManager.notify(NOTIFICATION, notification);
stopSelf();
和
public NotificationManager getNotificationManager() {
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
而且我的清单中已允许
<uses-permission android:name="android.permission.VIBRATE" />
有什么线索吗?
推荐答案
这是因为您没有在Notification上声明要振动的Vibrator类。在您的通知构建器中,放置此代码并根据您的选择设置振动的持续时间。
It is because you did not declare the Vibrator class to vibrate on Notification.In your notification builder put this code and set the duration of vibrate based on your choose.
Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
这篇关于振动不适用于通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!