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

661 views Asked by At

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?

1

There are 1 answers

9
CommonsWare On BEST ANSWER

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.