Android 更改BroadcastReceiver中的输出音量,以通知非流媒体首选项
WhatsApp在后台运行时似乎总是以高音量通知我,我的一个联系人(有自定义通知声音)出现 我试图创造一个类似的效果。我想控制通知的音量 我能找到的关于堆栈溢出的唯一例子是 这里的区别在于:Android 更改BroadcastReceiver中的输出音量,以通知非流媒体首选项,android,notifications,broadcastreceiver,audio,Android,Notifications,Broadcastreceiver,Audio,WhatsApp在后台运行时似乎总是以高音量通知我,我的一个联系人(有自定义通知声音)出现 我试图创造一个类似的效果。我想控制通知的音量 我能找到的关于堆栈溢出的唯一例子是 这里的区别在于: 我的上下文是一个BroadcastReceiver 我不是在播放流媒体音乐,而是使用基于手机上默认铃声列表的通知声音(类似于WhatsApp) 通知管理器基于getSystemService(Context.notification\u SERVICE),因此一些通常可用于流式处理的方法似乎不可用 谁能给我
BroadcastReceiver
getSystemService(Context.notification\u 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);
}
}
}嗯,你试过这个吗?(是的,它使用音频流,但应该可以工作)
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);