2013-07-31 185 views
0

我有兩個獨立的應用程序。應用程序A和應用程序B.我想從應用程序A開始應用程序B中的活動並獲得結果。在應用程序B中還有一個活動。從B第二項活動中,我得到了B的第一項活動。現在我想將這些結果返回給應用程序A.但是,A中的OnActivityResult永遠不會被調用。以下是代碼。OnActivityResult永遠不會被調用

應用程序A:

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); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     
      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活動1.class:

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; 
} 
+0

應用程序B是否也是您的應用程序? –

+0

@Hoan Nguyen Yup – Shah

回答

2

documentation

注意,此方法應只意圖協議一起使用的是定義了 以返回結果。在其他協議(如 ACTION_MAIN或ACTION_VIEW)中,當您期望 時可能不會得到結果。例如,如果您正在啓動的活動使用 singleTask啓動模式,它將不會在您的任務中運行,因此您將立即收到取消結果 。

+0

感謝您的回答。但我是android的新手。在這種情況下可能的解決方案是什麼。我如何集成兩個應用程序。或換句話說,我如何調用獨立應用程序並從該應用程序獲取結果。你的幫助將不勝感激。 – Shah

+0

根據文檔,似乎避免singleTask啓動模式並重命名意向操作將消除此問題。如果你看看OpenIntents(可用的源代碼),你可以看到他們是如何解決它的。 – 323go

+0

我現在使用動作名稱來調用應用程序B,但我不知道如何獲得結果。我無法理解Openintents。應用程序onActivityResult仍未被調用。 – Shah

相關問題