android - launch an activity of another app to get it's result

旧巷老猫 提交于 2019-12-21 16:25:11

问题


I have two applications, A and B.

From A, I'm launching B for a result, using the following code:

Intent fmIntent = getPackageManager().getLaunchIntentForPackage("com.example.B");
fmIntent.putExtra("hello", "world");
startActivityForResult(fmIntent, REQUEST_TEST);

From B, I'm doing the following:

getIntent().putExtra("completed", true);
setResult(RESULT_OK, getIntent());
finish();

If I do the above for an activity within the same app, it works as expected.

However, since its two different apps, I receive an empty intent with no data and an unset result code. How should I edit the above to ensure that one intent is maintained throughout?


回答1:


Use setFlags(0) to clean all flags which can be created by getLaunchIntentForPackage:

Intent fmIntent = getPackageManager().getLaunchIntentForPackage("com.example.B");
fmIntent.setFlags(0);
fmIntent.putExtra("hello", "world");
startActivityForResult(fmIntent, REQUEST_TEST);



回答2:


The solution is provided in a related question "Android onActivityResult triggered before activity even starts!". Create the intent this way:

Intent myIntent = new Intent();
myIntent.setClassName("com.example.B", "com.example.B.ActivityB");
startActivityForResult(myIntent, 600);

I was facing the same problem and solved this way.



来源:https://stackoverflow.com/questions/16222704/android-launch-an-activity-of-another-app-to-get-its-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!