改变输出量的BroadcastReceiver的通知对非流preference [英] Change output volume in BroadcastReceiver for Notification for non-streaming preference

查看:123
本文介绍了改变输出量的BroadcastReceiver的通知对非流preference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WhatsApp的,似乎总是与高容量通知我的背景和我交往中的一个(谁拥有自定义通知声音)运行时,出现了。

WhatsApp seems to always notify me with high volume when running in the background and one of my contacts (who has a custom notification sound) comes up.

我想创建一个类似的效果。我想控制通知的音量。

I'm trying to create a similar effect. I want to control the volume of the notification.

我能找到的堆栈溢出的唯一例子是<一个href=\"http://stackoverflow.com/questions/7337481/how-do-you-change-the-output-volume-of-a-sound\">this.

The only example I could find on Stack Overflow is this.

这里的不同之处在于:


  1. 我的背景是一个广播接收器

  2. 我不是流媒体音乐,但使用基于手机(类似WhatsApp的)上提供的默认铃声列表上的通知声音。

通知管理器是基于 getSystemService(Context.NOTIFICATION_SERVICE)所以一些方法可用通常用于流媒体似乎并不可用。

The notification manager is based on getSystemService(Context.NOTIFICATION_SERVICE) so some of the method available normally for streaming does not seems to be available.

任何人都可以提供一些线索。 code:

Could anyone shed some light. Code:

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);
    }
}

}

推荐答案

嗯,你试试这个? (是其使用的音频流,但它应该工作)

Hmm, did you try this? (yes its using an audio stream but it should work)

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);

这篇关于改变输出量的BroadcastReceiver的通知对非流preference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆