0

I'm trying to set alarm using my app and for that I'm using Alarm Clock Common Intent as described here.

Here's my code:

public void createAlarm(String message, int hour, int minutes) {
        Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
                .putExtra(AlarmClock.EXTRA_MESSAGE, message)
                .putExtra(AlarmClock.EXTRA_HOUR, hour)
                .putExtra(AlarmClock.EXTRA_MINUTES, minutes)
                .putExtra(AlarmClock.EXTRA_SKIP_UI, true);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, SET_ALARM_REQUEST_CODE);
        }
}

Here's onActivityResult():

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == SET_ALARM_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    Toast.makeText(getBaseContext(), "Alarm for " + data.getData().toString() + " has been set successfully!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getBaseContext(), "data is null", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(getBaseContext(), "resultCode not OK", Toast.LENGTH_SHORT).show();
            }
        }
}

The problem here is that I'm getting this Toast message: resultCode not OK.

So, why startActivityForResult is not returning any result?

Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133
  • Because most probably your activity will be killed before alarm clock. It is long time task (for example 15 hours). Returning result to starting activity is bad design for alarm I think. Thats why it isn't return any result. – Efe AYDIN Aug 27 '17 at 17:15

1 Answers1

3

That Intent action is not documented to return anything. Hence, implementations do not need to use setResult(), and so you are getting the default response.

Replace your startActivityForResult() with startActivity(), and remove your result-processing code from onActivityResult() tied to that startActivityForResult().

BTW, replace all your getBaseContext() calls with this.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • so how can I confirm that the alarm has been set successfully so that I can show some text to the user? – Hammad Nasir Aug 27 '17 at 16:56
  • @HammadNasir: You can't. Whether the user chooses to set an alarm is between the user and the app that handles your `ACTION_SET_ALARM` action. – CommonsWare Aug 27 '17 at 17:01
  • Then what can I do if I want to show some confirmation text to user? – Hammad Nasir Aug 27 '17 at 17:05
  • 1
    @HammadNasir: Write your own alarm clock functionality into your app, rather than relying upon third-party apps. The user may not like this, and it will be a lot of work. It would be far simpler to eliminate your requirement for showing this confirmation text. – CommonsWare Aug 27 '17 at 17:09
  • Okay! Got it. Thanks. Can you help with this one too: https://stackoverflow.com/q/45802830/6144372 – Hammad Nasir Aug 27 '17 at 19:37
  • One more question. Can we cancel alarms which were set using `AlarmClock.ACTION_SET_ALARM` intent? – Hammad Nasir Aug 28 '17 at 19:13
  • @HammadNasir: There is `ACTION_DISMISS_ALARM`, but that is new to Android 7.0, and from the JavaDocs I have no idea how you can use it. – CommonsWare Aug 28 '17 at 19:15
  • so that means there is no way of cancelling the alarms set using this intent in Android version before `7.0`? – Hammad Nasir Aug 28 '17 at 19:17
  • @HammadNasir: AFAIK, there is no way to cancel the alarms set using that `Intent` before Android 7.0. – CommonsWare Aug 28 '17 at 19:18