我有两个独立的应用程序。应用程序 A 和应用程序 B。我想从应用程序 A 在应用程序 B 中启动活动并返回结果。我可以使用 Action 从 A 调用应用程序 B 中的活动,但在完成活动后无法返回应用程序 A。A 中的 OnActivityResult 永远不会被调用。以下是代码。
public void onClickBtnToApplicationB(View v) {
try {
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
final ComponentName cn = new ComponentName("pakacagename","package.class");
intent.setComponent(cn);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivityForResult(intent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
//handle Exception
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case REQUEST_CODE:
handleResult(resultCode, intent);
break;
}
}
public void handleResult(int resultCode, Intent intentResult) {
switch (resultCode) {
case RESULT_OK:
String Result = intentResult.getStringExtra("RESULT");
// I need Results from Application B here..
break;
case RESULT_CANCELED:
break;
}
}
应用 B:
Intent s = new Intent(1.this,2.class);
startActivityForResult(s, REQUEST_CODE_B);
protected void onActivityResult(int requestCode, int resultCode, Intent intentResult) {
switch(requestCode){
case REQUEST_CODE_B:
handleResult(resultCode, intentResult);
}
}
public void handleResult(int resultCode, Intent intentResult) {
switch (resultCode) {
case RESULT_OK:
String scanResult = intentResult.getStringExtra("RESULT");
Intent newintent = new Intent();
newintent.putExtra("RESULT", scanResult);
setResult(Activity.RESULT_OK, newintent);
finish();
break;
case RESULT_CANCELED:
break;
}