Intent.FLAG_ACTIVITY_FORWARD_RESULT and PendingInt

2019-07-18 17:56发布

问题:

I want to receive an Intent instance from an Activity launched by an AlarmManager with a pendingIntent.

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

with the intent like this :

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

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

            }
        });

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 +"");

    }

}

it seems the flag Intent.FLAG_ACTIVITY_FORWARD_RESULT doesn't work...

回答1:

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.