2012-11-26 30 views
0

我加入一個通知我的Android項目:notificationcompat setDefaults和設置

mBuilder.setContentTitle("title"); 
mBuilder.setContentText("Text"); 
mBuilder.setSmallIcon(R.drawable.icon); 
mBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE); 
mBuilder.setSound(Uri.parse("android.resource://com.my.package/" + R.raw.sound)); 
mBuilder.setOnlyAlertOnce(true); 

現在,我想一個「設置視圖」裏,如果他/她想要的振動或聲音,用戶可以決定。

我應該如何存儲此設置並更新setDefaults和setSound?

回答

0

從@dsandler,我給你舉個例子作爲多一點點的細節:

builder = new NotificationCompat.Builder(this).setContentTitle(contentTitle).setContentText(contentText).setSmallIcon(R.drawable.ic_launcher) 
      .setAutoCancel(true).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(contentText)); 

    int notificationDefault = NotificationCompat.DEFAULT_SOUND; 

    // Check vibrate 
    boolean isVibrate = mPreferences.getBoolean(mKeyPrefNotifications[1], true); 
    if (isVibrate) { 
     notificationDefault = notificationDefault | NotificationCompat.DEFAULT_VIBRATE; 
    } 

    // Check light 
    boolean isLight = mPreferences.getBoolean(mKeyPrefNotifications[2], true); 
    if (isLight) { 
     notificationDefault = notificationDefault | NotificationCompat.DEFAULT_LIGHTS; 
    } 

    // Check sound 
    Uri sound = null; 
    if (!mPreferences.contains(mKeyPrefNotifications[3])) { 
     sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    } else { 
     String soundPref = mPreferences.getString(mKeyPrefNotifications[3], ""); 
     if (soundPref.isEmpty()) { 
      sound = null; 
     } else { 
      sound = Uri.parse(soundPref); 
     } 
    } 

    builder.setDefaults(notificationDefault); 
    builder.setSound(sound);