Uri默认声音通知?

时间:2012-06-30 06:26:38

标签: android notifications uri

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

builder.setSound(Uri sound)

但Uri在默认通知中的位置是什么?

7 个答案:

答案 0 :(得分:254)

尝试使用RingtoneManager将默认通知Uri设为:

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

builder.setSound(uri);

答案 1 :(得分:45)

builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)同样适用

答案 2 :(得分:26)

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

mBuilder.setDefaults(Notification.DEFAULT_SOUND);

或使用RingtoneManager Class:

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

答案 3 :(得分:11)

所有这些方法都有效

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

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

  3. mBuilder.setDefaults(Notification.DEFAULT_SOUND);

  4. Google Documentation

答案 4 :(得分:3)

你也可以使用它:

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

答案 5 :(得分:3)

  

用于系统默认通知

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

  

自定义通知

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

  

通知声源(我改名为“旋转”并放入   res->原始文件夹)

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

  

通知构建器:

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

答案 6 :(得分:0)

如果有人仍然需要,这对于声音和振动来说效果非常好。

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

如果你想禁用例如振动变化振动到新的长[] {0,0,0,0,0}; 你可以用声音做几乎相似的事情,或者使用if else声明。