Why startActivityForResult not returning any resul

2019-08-23 04:26发布

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条回答
做自己的国王
2楼-- · 2019-08-23 04:32

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.

查看更多
登录 后发表回答