Intent.FLAG_ACTIVITY_FORWARD_RESULT和PendingIntent [英] Intent.FLAG_ACTIVITY_FORWARD_RESULT and PendingIntent
问题描述
我想从AlarmManager发起的Activity中接收一个Intent实例,并且其中包含未决的Intent.
I want to receive an Intent instance from an Activity launched by an AlarmManager with a pendingIntent.
我让活动A通过AlarmManager启动具有活动状态的活动B,如下所示:
I have the Activity A launch the Activity B through alarmManager with pendingIntent like this :
PendingIntent pendingIntent = PendingIntent.getActivity(this, position, intentListAlarmActivityToWakeUpActivity, 0);
Log.i("position", position +"");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hourAlarm);
calendar.set(Calendar.MINUTE, minutesAlarm);
Log.i("timeInMillis", calendar.getTimeInMillis() + "");
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
具有这样的意图:
intentListAlarmActivityToWakeUpActivity = new Intent(ListAlarmActivity.this, WakeUpActivity.class);
intentListAlarmActivityToWakeUpActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_FORWARD_RESULT);
但是当我在活动B中设置方法setResult(...)时,我不明白,我的活动A中没有结果... 在我的活动B中:
But i don't understand when i set the method setResult(...) in my Activity B, i have no result in my Activity A... Here in my Activity B:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wake_up);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
);
mStopButton = (Button) findViewById(R.id.stop_button);
mStopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int stopAlarm = 1;
Intent intentResult = new Intent();
intentResult.putExtra("stopAlarm", stopAlarm);
setResult(RESULT_OK, intentResult);
finish();
}
});
在活动A的onActivityResult方法中:
And in the method onActivityResult in Activity A :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
Alarm alarm = (Alarm) intent.getSerializableExtra(AlarmAdapter.ALARM_INSTANCE);
int position = intent.getIntExtra(AlarmAdapter.ITEM_POSITION_ADAPTER, 0);
setAlarm(alarm.getHourOfAlarm(), alarm.getMinutesOfAlarm(), position);
mListOfAlarm.get(position).setData(alarm);
mAlarmAdapter.notifyDataSetChanged();
}
else if(requestCode == REQUEST_CODE_WAKE_UP_ACTIVITY && resultCode == RESULT_OK) {
int stop = intent.getIntExtra("stopAlarm", -1);
Log.i("stopAlarm", stop +"");
}
}
似乎标志Intent.FLAG_ACTIVITY_FORWARD_RESULT不起作用...
it seems the flag Intent.FLAG_ACTIVITY_FORWARD_RESULT doesn't work...
推荐答案
您不能像这样使用Intent.FLAG_ACTIVITY_FORWARD_RESULT
.由于ActivityB
由AlarmManager
启动,因此它将忽略FORWARD_RESULT标志.只有从另一个Activity
直接启动Activity
时,该标志才起作用.
You cannot use Intent.FLAG_ACTIVITY_FORWARD_RESULT
like that. Since ActivityB
is being launched by the AlarmManager
, it will ignore the FORWARD_RESULT flag. That flag only works if an Activity
is launched directly from another Activity
.
这篇关于Intent.FLAG_ACTIVITY_FORWARD_RESULT和PendingIntent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!