Search code examples
androidalarmringtone

Android: firing/starting alarm tone on my AlarmReciever getRingtone() returns null


i have this code that fires a toast message on alarm set by a timepicker, i want to fire/start also, aside from the toast message, a alarm tone together with it.

this is my code on my reciever:

    public void onReceive(Context context, Intent intent) {

        long time = intent.getExtras().getLong("time");
        Toast.makeText(context, "Alarming at "+formatDate(time),
                Toast.LENGTH_LONG).show();
        //Uri alerTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        try {
            Uri alert = getAlarmUri();
            Log.d("alarmCheck","Value of URI: "+alert.toString());
            Ringtone r = RingtoneManager.getRingtone(context, alert);
            r.play()

;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private Uri getAlarmUri() {
        Uri alert = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_ALARM);
        if (alert == null) {
            alert = RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            if (alert == null) {
                alert = RingtoneManager
                        .getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            }
        }
        return alert;
    }

this is my mainActivity:

   @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TimePicker timepicker = (TimePicker) findViewById(R.id.alarm_time);
    Button set_alarm = (Button) findViewById(R.id.set_alarm);

        set_alarm.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                 Calendar c = Calendar.getInstance();
                 c.setTimeInMillis(System.currentTimeMillis());
                 c.set(Calendar.HOUR_OF_DAY, timepicker.getCurrentHour());
                 c.set(Calendar.MINUTE, timepicker.getCurrentMinute());
                 c.set(Calendar.SECOND, 0);
                 Intent in = new Intent(MainActivity.this, OnAlarmReciever.class);
                         in.putExtra("time", c.getTimeInMillis());
                 PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 123456789, in, 0);
                 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                 am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),pi);
            }
        });

    }

and on my receiver, the part where the try catch block, getRingtone() returns a null thus accessing play() produces nullexception.. any inputs on this matter will be much appreciated.

Thanks, God Bless!

EDIT (DDMS Logcat, URI value)


08-26 08:46:55.764: D/alarmCheck(537): Value of URI: content://settings/system/alarm_alert
08-26 08:46:55.774: D/MediaPlayer(537): Couldn't open file on client side, trying server side
08-26 08:46:55.825: E/MediaPlayer(537): Unable to to create media player
08-26 08:46:55.825: E/RingtoneManager(537): Failed to open ringtone content://settings/system/alarm_alert
08-26 08:46:55.825: W/System.err(537): java.lang.NullPointerException
08-26 08:46:55.844: W/System.err(537):  at com.example.alarmexercise.OnAlarmReciever.onReceive(OnAlarmReciever.java:36)
08-26 08:46:55.844: W/System.err(537):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2119)
08-26 08:46:55.844: W/System.err(537):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
08-26 08:46:55.844: W/System.err(537):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197)
08-26 08:46:55.844: W/System.err(537):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 08:46:55.844: W/System.err(537):  at android.os.Looper.loop(Looper.java:137)
08-26 08:46:55.854: W/System.err(537):  at android.app.ActivityThread.main(ActivityThread.java:4424)
08-26 08:46:55.854: W/System.err(537):  at java.lang.reflect.Method.invokeNative(Native Method)
08-26 08:46:55.854: W/System.err(537):  at java.lang.reflect.Method.invoke(Method.java:511)
08-26 08:46:55.854: W/System.err(537):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-26 08:46:55.864: W/System.err(537):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-26 08:46:55.874: W/System.err(537):  at dalvik.system.NativeStart.main(Native Method)

** Additional Edits.. Ringtone r = ... line where r really is null**

Is it possible that this default tone/sound won't work on the emulator?. because I've read some threads regarding playing tones on the emulator that won't work due to the fact that the emulator has no default sound or can't handle tones/sounds?..... can't confirm this yet because there are few threads that are saying so


Solution

  • Indeed, could be an emulator problem. Try to push an mp3 file to the sdcard and set it as ringtone and see if that helps?