gpt4 book ai didi

android - 显示通知时通知声音播放两次

转载 作者:行者123 更新时间:2023-11-29 23:46:27 24 4
gpt4 key购买 nike

这是扩展 BroadcastReceivermyService 类。如果我不调用 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)

或者不设置DEFAULT_ALL

其他可以使用的默认值:

  • int DEFAULT_ALL:使用所有默认值(如果适用)。
  • int DEFAULT_LIGHTS:使用默认通知灯。
  • int DEFAULT_SOUND:使用默认通知声音。
  • int DEFAULT_VIBRATE:使用默认通知振动。

更多信息在这里: Android Disable Notification Sounds

关于android - 显示通知时通知声音播放两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51329714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com