2015-05-29 61 views
0

我有2個應用程序,ABstartActivityForResult()在調用另一個應用程序時立即調用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(); 
    } 
} 

回答

0
getCallingActivity() 

woun't爲空只在情況下,如果你調用特定的活動與你startActivityForResult意圖。
但您打電話的不是具體活動,而是getPackageManager().getLaunchIntentForPackage("com.app.B");
其檢索指定包的默認啓動活動,並以默認(空)意圖啓動它。

0

startActivityForResult()前加launchIntent.setFlags(0);那是你的編碼什麼.. 一旦乙應用的活動啓動你叫的setResult方法,因爲它是在onCreate()方法.. 你得到失敗的結果代碼的原因是因爲你已經設置

setResult(RESULT_OK, returnIntent) 

,但你應該使用Activity.RESULT_OK像

setResult(Activity.RESULT_OK, returnIntent) 
相關問題