振动不适用于通知

时间:2014-12-28 22:43:54

标签: android android-notifications android-vibration

我正在尝试设置振动和声音以进行通知。由于某些原因它不适合我:(这是我正在尝试的代码

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" />

有什么想法发生了什么?

2 个答案:

答案 0 :(得分:5)

添加此

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

答案 1 :(得分:4)

这是因为您没有声明Vibrator类在Notification上振动。在您的通知构建器中输入此代码并根据您的选择设置振动的持续时间。

    Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
 // Vibrate for 500 milliseconds
 v.vibrate(500);