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?
That
Intent
action is not documented to return anything. Hence, implementations do not need to usesetResult()
, and so you are getting the default response.Replace your
startActivityForResult()
withstartActivity()
, and remove your result-processing code fromonActivityResult()
tied to thatstartActivityForResult()
.BTW, replace all your
getBaseContext()
calls withthis
.