Using a content Uri for alarm notification

喜欢而已 提交于 2019-12-25 10:56:23

问题


I have given the user the choice of what sound they want in their notifications but can't make it work. It always plays the default sound. They choose their sound in the preferences

 <RingtonePreference
        android:defaultValue="content://settings/system/notification_sound"
        android:key="alarmsound"
        android:ringtoneType="notification"
        android:showDefault="true"
        android:summary="Choose Your Alarm Sound"
        android:title="Alarm Sound" />
</PreferenceCategory>

Then it is pulled up in my notification activity

SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String alarms = getPrefs.getString("alarmsound", "");

This returns a content Uri content://media/external/audio/media/10

I tried to convert it to a file path with

String filePath = null;
Uri _uri = Uri.parse(alarms);
Cursor cursor = this.getContentResolver().query(_uri, new String [] { android.provider.MediaStore.Audio.Media.DATA }, null, null, null);
cursor.moveToFirst();
filePath = cursor.getString(0);
cursor.close();
Log.e(TAG5, filePath);

This returns /storage/sdcard0/Notfications/hangout_ringtone.m4a

I tried putting this into the .setSound(filePath); but it doesn't work. It wants a Uri and I know this isn't a full path. Any ideas?


回答1:


Not sure if anyone else will need this but I was able to solve my problem with the help of other stackoverflow posts and the following code. First I disabled any default sounds in my notification builder

NotificationManager notificationManager = (NotificationManager) getApplicationContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
            getApplicationContext())


            .setContentText(notificationContent)
            .setContentTitle(notificationTitle)
            .setSmallIcon(smallIcon)
            .setAutoCancel(true)
            .setTicker(notificationTitle)
            .setLargeIcon(largeIcon)
            .setSound(null);

Setting sound to null removed the default sound. then I added in on the next line

RingtoneManager.getRingtone(this, Uri.parse(alarms)).play();

(alarms) is a string that was from my sharedpreferences as seen in my original question. full implemented code is

SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean vibrate = getPrefs.getBoolean("vibrate", true);

    String alarms = getPrefs.getString("alarmsound", "");
    Log.e(TAG4, alarms);




    NotificationManager notificationManager = (NotificationManager) getApplicationContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
            getApplicationContext())


            .setContentText(notificationContent)
            .setContentTitle(notificationTitle)
            .setSmallIcon(smallIcon)
            .setAutoCancel(true)
            .setTicker(notificationTitle)
            .setLargeIcon(largeIcon)
            .setSound(null);


            RingtoneManager.getRingtone(this, Uri.parse(alarms)).play();        

    if (vibrate == true ){


            notificationBuilder.setDefaults(
                    Notification.DEFAULT_LIGHTS
                            | Notification.DEFAULT_VIBRATE | Notification.PRIORITY_HIGH)
            .setContentIntent(pendingIntent);


    } else {
                notificationBuilder.setDefaults(
                        Notification.DEFAULT_LIGHTS
                                | Notification.PRIORITY_HIGH)
                .setContentIntent(pendingIntent);

    }
        Notification notification = notificationBuilder.build();
        notificationManager.notify(id, notification);



回答2:


I believe this will take care of it for you:

String alarms = getPrefs.getString("alarmsound", "");
Uri _uri = Uri.parse(alarms);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
        .setContentText(notificationContent)
        .setContentTitle(notificationTitle)
        .setSmallIcon(smallIcon)
        .setAutoCancel(true)
        .setTicker(notificationTitle)
        .setLargeIcon(largeIcon)
        .setSound(_uri);

Notification notification = nBuilder.getNotification();

// Make sure you do *not* include the default sound 
// or the setSound call above will be ignored
notification.defaults |= Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;


来源:https://stackoverflow.com/questions/17475316/using-a-content-uri-for-alarm-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!