Android Uri 到默认声音通知?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11271991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:36:59  来源:igfitidea点击:

Uri to default sound notification?

androidnotificationsuri

提问by StefMa

I use the Notification.Builderto build a notification. Now I want to use the default sound notification with:

我使用Notification.Builder来构建通知。现在我想使用默认声音通知:

builder.setSound(Uri sound)

But where is the Uri to the default notification?

但是默认通知的 Uri 在哪里?

回答by ρяσ?ρ?я K

try using RingtoneManagerto get Default Notification Uri as:

尝试使用RingtoneManager获取默认通知 Uri 为:

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

builder.setSound(uri);

回答by Fortega

builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)works as well

builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)也能用

回答by Jorgesys

The two options to use the Default Notification Soundare:

使用 的两个选项Default Notification Sound是:

mBuilder.setDefaults(Notification.DEFAULT_SOUND);

or using RingtoneManagerClass:

或使用RingtoneManager类:

mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

回答by Cristiana Chavez

all of these methods work

所有这些方法都有效

  1. mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

  2. mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

  3. mBuilder.setDefaults(Notification.DEFAULT_SOUND);

  1. mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

  2. mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

  3. mBuilder.setDefaults(Notification.DEFAULT_SOUND);

Google Documentation

谷歌文档

回答by sssvrock

For system default notification

对于系统默认通知

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

For custom notification

对于自定义通知

Uri customSoundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.twirl);

Uri customSoundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.twirl);

Source of notification sound (I renamed to "twirl" and placed in res->raw folder)

通知声音的来源(我重命名为“twirl”并放置在 res->raw 文件夹中)

https://notificationsounds.com/message-tones/twirl-470

https://notificationsounds.com/message-tones/twirl-470

Notification builder:

通知生成器:

NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.notificaion_icon)
                        .setContentTitle("Title here")
                        .setContentText("Body here")
                        .setSound(defaultSoundUri)
                        .setAutoCancel(true);



NotificationManager mNotifyMgr =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

mNotifyMgr.notify(id, mBuilder.build());

回答by Leebeedev

You can use this as well:

您也可以使用它:

Uri uri = Uri.parse(PreferenceManager.getDefaultSharedPreferences(this).
            getString("pref_tone", "content://settings/system/notification_sound"));
mBuilder.setSound(uri);

回答by Jevgenij Kononov

If somebody still need, this works absolutely fine with sound and vibration.

如果有人仍然需要,这对声音和振动绝对有效。

        Context context = getApplicationContext();
    long[] vibrate = new long[] { 1000, 1000, 1000, 1000, 1000 };
    Intent notificationIntent = new Intent(context, MainActivity.class);

    PendingIntent contentIntent = PendingIntent.getActivity(context,
            0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    Resources res = context.getResources();
    Notification.Builder builder = new Notification.Builder(context);

    builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.notif)
            .setTicker("lastWarning")
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setVibrate(vibrate)
            //.setContentTitle(res.getString(R.string.notifytitle)) 
            .setContentTitle("Notification")
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            //.setContentText(res.getString(R.string.notifytext))
            .setContentText("Notification text"); 

    // Notification notification = builder.getNotification(); // until API 16
    Notification notification = builder.build();

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFY_ID, notification);

If you would like disable for example vibration change vibrate to new long[] { 0,0,0,0,0}; Almost similar thing you can do with sound or use if else statement.

如果你想禁用例如振动改变振动到新的长[] { 0,0,0,0,0}; 您可以使用声音或使用 if else 语句执行几乎类似的操作。