0

I want set Notification ring ,this is my code :

Notification notification = new Notification();     

String ringName = RingtoneManager.getActualDefaultRingtoneUri(
            MainActivity.this, RingtoneManager.TYPE_NOTIFICATION).toString();

notification.sound = Uri.parse(ringName);   

but i find that .toString() will led app crash in emulater.

"RingtoneManager.getActualDefaultRingtoneUri( MainActivity.this, RingtoneManager.TYPE_NOTIFICATION)" return NULL ,because emulater set notification ring is silent.

So i guest that if URI is NULL , we can's use Uri.toString() to convert URI to String ?

yes, i make a test to prove my guess . Because this android emulator set ringtone , notification ,alarm to be silent and there is no music file in emulator.

so first i use

abd to push 1.mp3 to sdcard/notification/

then go to

"dev tool -->media provider-->scan sdcard --> insert abulm . and go to android --> system setting-->sound-->set notification to 1.mp3"

and run my code again , there is no problem come out.

So, if URI is NULL , we can't use Uri.toString() to convert uri to string, this will get a nullpoint error.

But why ? i hope someone that could give a detail explain ! thanks

2010cpu
  • 1
  • 1

1 Answers1

0

There is a pretty good explanation Here about the toString() method. You could add an if statement.

 if (ringName != null) {
        notification.sound = Uri.parse(ringName);
    }
Community
  • 1
  • 1
DevJem
  • 656
  • 1
  • 13
  • 21
  • NO, when `RingtoneManager.getActualDefaultRingtoneUri( MainActivity.this, RingtoneManager.TYPE_NOTIFICATION) == NULL` , using `toString` will make app crash .so `ringName != null ` won't be excuted – 2010cpu Mar 09 '15 at 02:17