Why startActivityForResult not returning any result from AlarmClock.ACTION_SET_ALARM intent?
Asked Answered
L

1

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?

Lierne answered 27/8, 2017 at 16:40 Comment(1)
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.Carditis
M
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.

Maccaboy answered 27/8, 2017 at 16:43 Comment(9)
so how can I confirm that the alarm has been set successfully so that I can show some text to the user?Lierne
@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.Maccaboy
Then what can I do if I want to show some confirmation text to user?Lierne
@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.Maccaboy
Okay! Got it. Thanks. Can you help with this one too: https://mcmap.net/q/1488813/-getting-39-app-transformclasseswithdexfordebug-39-error-on-compiling-the-project-having-uselibrary-39-org-apache-http-legacy-39-in-build-gradle/6144372Lierne
One more question. Can we cancel alarms which were set using AlarmClock.ACTION_SET_ALARM intent?Lierne
@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.Maccaboy
so that means there is no way of cancelling the alarms set using this intent in Android version before 7.0?Lierne
@HammadNasir: AFAIK, there is no way to cancel the alarms set using that Intent before Android 7.0.Maccaboy

© 2022 - 2024 — McMap. All rights reserved.