startActivityForResult

startActivityForResult

本文介绍了为什么startActivityForResult不从AlarmClock.ACTION_SET_ALARM意向返回任何结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的应用设置闹钟,为此,我正在使用此处.

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

这是我的代码:

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);
        }
}

这里是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();
            }
        }
}

这里的问题是我收到此Toast消息:resultCode not OK.

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

那么,为什么startActivityForResult没有返回任何结果?

So, why startActivityForResult is not returning any result?

推荐答案

Intent操作为未记录为返回任何内容.因此,实现无需使用setResult(),因此您将获得默认响应.

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.

startActivityForResult()替换为startActivity(),并从与该startActivityForResult()绑定的onActivityResult()中删除结果处理代码.

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

顺便说一句,将所有getBaseContext()通话替换为this.

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

这篇关于为什么startActivityForResult不从AlarmClock.ACTION_SET_ALARM意向返回任何结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 08:28