2017-08-01 166 views
0

我想在設備收到通知時不斷振動設備,如果按某個按鈕,振動會停止。Android Studio'Notification.FLAG_INSISTENT'不起作用

所以我加這兩行。

Notification notification = notificationBuilder.build(); 

notification.flags |= Notification.FLAG_INSISTENT; 

但是,當我收到通知時,設備只振動一次。

我需要做些什麼來持續振動?

private void showNotification(String title, String message) { 

     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher_plant) 
       .setLights(Color.GREEN,1,1) 
       .setContentTitle(title) 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setContentIntent(pendingIntent) 
       .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) 
       .addAction(R.drawable.ic_menu_camera,"Go CCTV",getNotificationPendingIntent()); 


     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     Notification notification = notificationBuilder.build(); 
     notification.flags |= Notification.FLAG_INSISTENT; 
     notificationManager.notify(0, notificationBuilder.build()); 
    } 

回答

2

你打電話notificationBuilder.build()兩次:)

而不是notificationManager.notify(0, notificationBuilder.build());

做:notificationManager.notify(0, notification);

+0

謝謝!有用!! –