Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.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
如何在notification compat android中添加自定义通知声音_Android_Notifications - Fatal编程技术网

如何在notification compat android中添加自定义通知声音

如何在notification compat android中添加自定义通知声音,android,notifications,Android,Notifications,如何在使用notification compat时添加自定义通知声音?我看到这样的事情 Uri uri = Uri.parse(PreferenceManager.getDefaultSharedPreferences(this). getString("pref_tone", "content://settings/system/notification_sound")); Builder.setSound(uri); 但就我而言。不幸的是,它不起作用:请帮帮我。顺便说一句

如何在使用notification compat时添加自定义通知声音?我看到这样的事情

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

但就我而言。不幸的是,它不起作用:请帮帮我。顺便说一句,我还在我的云功能中使用它:提前感谢。

是的,这样做非常简单。你所要做的就是

1.如果尚未创建,请创建原始资源目录

2.将要设置为通知声音的文件放在原始文件夹中

3.将原始文件设置为通知声音

您还可以查看以下代码段

public void GenerateNotification(View view) {
        NotificationManager myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "123")
                .setAutoCancel(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Test")
                .setContentText("Sound test")
                .build();
        notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test);
        if (myNotificationManager != null) {
            myNotificationManager.notify(1, notification);
        }
    }

你想只设定一个特定的声音吗?如果是,则可以将该文件放入原始文件夹。如果要使用默认声音,则只需在生成器对象集合DefaultSnotificationCompat.default\u ALL中调用此方法即可使用默认设置