2012-05-04 65 views
7

我正在使用NotificationManager构建器在我的应用程序中显示警报。我知道notify方法的第一个参数是一个id,如果它已经可见,框架将更新通知,但是如果我将警报设置为播放铃声或振动,那么铃声/振动也会在警报发生时触发更新?更新通知振动/铃声

NotificationCompat.Builder nb = new NotificationCompat.Builder(this); 
    nb.setContentTitle("title"); 
    nb.setContentText("message"); 
    nb.setSmallIcon(getResources().getIdentifier("drawable/alert", null, packageName)); 
    nb.setWhen(System.currentTimeMillis()); 
    nb.setAutoCancel(true); 
    nb.setTicker("message"); 

    final Uri ringtone = Uri.parse(PreferenceManager.getDefaultSharedPreferences(this).getString("ringtone", getString(R.string.settings_default_ringtone))); 

    nb.setDefaults(Notification.DEFAULT_VIBRATE); 
    nb.setSound(ringtone);  
    nb.setDefaults(Notification.DEFAULT_LIGHTS); 

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

    final Intent notificationIntent = new Intent(this, Main.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 

    final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
    nb.setContentIntent(contentIntent); 

    Notification notification = nb.getNotification(); 

    nm.notify(0, notification); 
+0

请参阅

回答

6

刚刚测试此我自己和振动/铃声也火过,甚至在更新。

UPDATE: 只是一个更新,如果您正在使用NotificationCompat.BuilderNotification.Builder可以设置setOnlyAlertOnce到只有声音的铃声/振动一次。

0

NotificationCompat.Builder notificationBuilder =新NotificationCompat.Builder(MainActivity.this)

长[] V = {500,1000}; notificationBuilder.setVibrate(v);

Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); notificationBuilder.setSound(uri);