Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 显示通知时,通知声音播放两次_Android_Notifications_Broadcastreceiver - Fatal编程技术网

Android 显示通知时,通知声音播放两次

Android 显示通知时,通知声音播放两次,android,notifications,broadcastreceiver,Android,Notifications,Broadcastreceiver,这是myService类,它扩展了BroadcastReceiver。如果我不调用showNotif,则只会播放一次通知声音。当我这样做时,声音会播放两次。有人能帮我告诉我这个代码出了什么问题吗?谢谢 public class myService extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { showNoti

这是
myService
类,它扩展了
BroadcastReceiver
。如果我不调用showNotif,则只会播放一次通知声音。当我这样做时,声音会播放两次。有人能帮我告诉我这个代码出了什么问题吗?谢谢

public class myService extends BroadcastReceiver
{    
    @Override
    public void onReceive(Context context, Intent intent)
    {
        showNotif(context, id, name);
        try {
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone r = RingtoneManager.getRingtone(context, notification);
            r.play();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void showNotif(Context context, Integer id, String name) {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, id.toString());
        notificationBuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.alert)
                .setTicker("bla")
                .setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
                .setContentTitle(name)
                .setContentText("blabla")
                .setContentInfo("blablabla");
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent mIntent = new Intent(context, newClass.class);   
        mIntent .putExtra("ID", id);
        mIntent .putExtra("NAME", name);
        PendingIntent mPending = PendingIntent.getActivity(context, id, mIntent , PendingIntent.FLAG_UPDATE_CURRENT);  
        notificationBuilder.setContentIntent(mPending);
        notificationManager.notify(id, notificationBuilder.build());
    }

}

通知上的声音可以按如下方式关闭:

.setDefaults(0)
或者不设置默认值

可使用的其他默认值:

  • int DEFAULT_ALL:使用所有默认值(如果适用)
  • int DEFAULT_LIGHTS:使用默认通知灯
  • int DEFAULT_声音:使用默认通知声音
  • int DEFAULT_振动:使用默认通知振动
更多信息请点击此处:

DEFAULT\u全部包括DEFAULT\u声音,因此除了正在播放的铃声外,通知还会播放一种声音:哦,我明白了。。。我刚刚删除了“.setDefaults(Notification.DEFAULT\u ALL)”,现在一切都很正常。非常感谢。NP很高兴它起作用了:):)