Android通知,震动但无声音 [英] Android notification, vibrates but no sound

查看:448
本文介绍了Android通知,震动但无声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关此主题的其他一些帖子,我认为我的代码应该听起来不错 警报,但不是.它确实振动,但是没有声音.关于如何获得任何建议 传达声音吗?

I've read some of the other posts on this subject and I think my code should be sounding an alarm, but it's not. It does vibrate, but no sound. Any suggestions on how to get this to convey sound ?

程序的另一部分能够播放铃声,所以问题似乎出在 具体这个例程.

Another part of the program is able to play a ringtone so the problem seems to be specific this routine.

这是在扩展Service的类中

This is in a class that extends Service

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    if (sound == null) { Log.i("RECEIVER", "SOUND IS NULL"); }

    NotificationManager myNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    Intent intentMain = new Intent(this.getApplicationContext(), MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intentMain, 0);

    long[] pattern = {500,500,500,500,500,500,500,500,500};

    Notification myNote = new Notification.Builder(this)
            .setContentTitle("NotificationDemo")
            .setContentText("NotificatinDemo")
            .setSmallIcon(R.drawable.ic_launcher)
            .setSound(sound)
            .setVibrate(pattern)
            .setContentIntent(pIntent)
            .build();

    myNM.notify(1, myNote);
    return super.onStartCommand(intent, flags, startId);
}

推荐答案

尝试一下:

Notification.Builder mBuilder = new Notification.Builder(this)
                .setContentTitle("NotificationDemo")
                .setContentText("NotificatinDemo")
                .setSmallIcon(R.drawable.ic_launcher)
                .setVibrate(pattern)
                .setContentIntent(pIntent);

Notification myNote = mBuilder.build();

if(Build.VERSION.SDK_INT >= 21) {
    myNote.sound = sound;
    myNote.category = Notification.CATEGORY_ALARM;

    AudioAttributes.Builder attrs = new AudioAttributes.Builder();
    attrs.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION);
    attrs.setUsage(AudioAttributes.USAGE_ALARM);
    myNote.audioAttributes = attrs.build();
} else  {
    mBuilder.setSound(sound, AudioManager.STREAM_ALARM);
    myNote = mBuilder.build();
}

myNM.notify(1, myNote);

还要检查以下内容:棒棒糖通知功能在新通知系统中使用的AudioAttributes

Also check these: Lollipop notification feature, AudioAttributes class used in new notification system, and usage of Audio Attributes.

这篇关于Android通知,震动但无声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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