2012-08-13 15 views
0

WhatsApp似乎總是在後臺運行時以高音量通知我,並且其中一位聯繫人(擁有自定義通知聲音)出現。更改BroadcastReceiver for Notification中的輸出音量以適應非流式首選項

我試圖創建一個類似的效果。我想控制通知的音量。

我能找到的堆棧溢出的唯一例子是在這裏this.

的不同之處在於:

  1. 我的背景是一個BroadcastReceiver
  2. 我不是流媒體音樂,但使用基於通知聲音在手機上可用的默認鈴聲列表(類似於WhatsApp)。

通知管理器基於getSystemService(Context.NOTIFICATION_SERVICE),因此通常用於流式傳輸的一些方法似乎不可用。

任何人都可以點亮一下。代碼:

public class OnAlarmReceiver extends BroadcastReceiver { 
private static final int NOTIFY_ME_ID=1337; 

private static final String TAG = "OnAlarmReceiver"; 

@Override 
public void onReceive(Context ctxt, Intent intent) {  
    Bundle bundle = intent.getExtras(); 
    int itemId = bundle.getInt("itemId"); 
    String itemTitle = bundle.getString("itemTitle"); 
    int priority = bundle.getInt("priority"); 
    long listId = bundle.getLong("listId"); 
    String listTitle = bundle.getString("listTitle"); 

    Toast.makeText(ctxt, "itemId: " + Integer.toString(itemId), Toast.LENGTH_LONG).show(); 

    SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(ctxt); 
    boolean useNotification=prefs.getBoolean("use_notification", true); 

    if (useNotification) { 
     NotificationManager mgr = (NotificationManager)ctxt.getSystemService(Context.NOTIFICATION_SERVICE);         
     Notification notification = new Notification();   
     if (priority == 1) { // Display red icon 
      notification=new Notification(R.drawable.nuvola_apps_kwrite, itemTitle, System.currentTimeMillis());  
     } else { // Display blue icon 
      notification=new Notification(R.drawable.nuvola_apps_package_editors, itemTitle, System.currentTimeMillis());    
     }   
     Intent itemEditor = new Intent(ctxt, EditItem.class); 
     long lAlarmId = (long) (int) itemId; 
     itemEditor.putExtra(DbAdapter.KEY_ITEMS_ITEM_ID, lAlarmId); 
     itemEditor.putExtra("listId", listId); 
     itemEditor.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

     PendingIntent i=PendingIntent.getActivity(ctxt, 0, itemEditor, PendingIntent.FLAG_UPDATE_CURRENT);   
     notification.setLatestEventInfo(ctxt, listTitle, itemTitle, i);   
     String notifyPreference = prefs.getString("notification_sound", "DEFAULT_RINGTONE_URI");       
     notification.sound = Uri.parse(notifyPreference); 

     if (priority == 1) { 
      notification.defaults |= Notification.DEFAULT_VIBRATE; 
     }   
     mgr.notify(itemId + NOTIFY_ME_ID, notification); 
    } 
    else { 
     Intent i=new Intent(ctxt, AlarmActivity.class); 
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     ctxt.startActivity(i); 
    } 
} 

}

回答

0

嗯,你試試這個? (是它使用音頻流,但它應該工作)

private AudioManager mAudioManager; 
private Context ctxt; 

mAudioManager = (AudioManager)ctxt.getSystemService(Context.AUDIO_SERVICE); 

int streamVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION); 

mAudioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, streamVolume, 0); 
+0

謝謝你的工作! – 2012-08-13 18:37:55

+0

不客氣:) – 2012-08-13 18:47:36