我有2個應用程序,A
和B
。startActivityForResult()在調用另一個應用程序時立即調用OnActivityResult
在應用程序A
,我想打電話給應用程序B
並從應用程序B
得到結果。我試試這個代碼:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.app.B");
startActivityForResult(launchIntent, CODE);
,但之後我打電話的代碼,馬上叫onActivityResult()方法,並給出結果RESULT_CANCELLED
。
應用B清單:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.B">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".TestActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
應用B測試活動:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
if (getCallingActivity() == null) {
startActivity(new Intent(this, MainActivity.class));
finish();
} else {
Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);
finish();
}
}