2013-03-18 65 views
1

我已經用AIDL文件綁定了2個android應用程序。 A和B的應用程序之間的交換是象下面這樣:Android:在singleTop/singleTask模式下綁定應用程序(AIDL)

  • 該A應用程序通過AIDL 接口
  • 甲應用程序連接到B應用程序調用乙服務的方法
  • 乙服務返回的PendingIntent
  • 甲應用啓動的PendingIntent
  • 當動作結束在B應用:回甲 應用

但是,如果B應用程序在交換之前啓動,則在調用「finish()」方法後,用戶將被重定向到B應用程序的最後一個活動,而不是在A應用程序中。

在我的一個應用程序:

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    ... 
    Intent i = new Intent(); 
    i.setClassName(Utils.BILLING_PACKAGE,Utils.BILLING_INTERFACE); 

    try { 
     Boolean ret = bindService(i, mConnection, Context.BIND_AUTO_CREATE); 
    } catch (Exception e) {} 

} 

//ServiceConnection class  
private ServiceConnection mConnection = new ServiceConnection() { 

     public void onServiceConnected(ComponentName name, IBinder boundService) { 
      service = InBillingInterface.Stub.asInterface((IBinder) boundService); 
      Utils.debug(mContext, "connection status : "+ service); 
    } 

     public void onServiceDisconnected(ComponentName name) { 
      service = null; 
     } 

}; 

... 
//launch the second application 
Bundle response = service.prepareForBillingService(data); 
PendingIntent pIntent = response.getParcelable(Utils.RESPONSE_P_INTENT); 

     try { 
      Intent in = new Intent(); 
      in.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 
      pIntent.send(mContext, 0, in); 
     } catch (PendingIntent.CanceledException e) { 
      Utils.error("Sending contentIntent failed:" + e.getMessage()); 
     } 

我試過設置的Android:launchMode到singleTask或singleTop在一個應用程序清單文件,但問題仍然相同。正如你所看到的,我在發送活動時也放了一個「FLAG_ACTIVITY_MULTIPLE_TASK」標誌。

代碼在我的B級應用:

創建PendingActivity

public class InBillingService extends Service { 

    private static final String TAG = "my tag"; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.d(TAG, "Welcome!"); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.d(TAG, "Byebye!"); 
    } 

    /** 
    * Method for AIDL interface 
    */ 
    @Override 
    public IBinder onBind(Intent arg0) { 

     return new InBillingInterface.Stub() { 

      //a method that return a pendingIntent 
      @Override 
      public Bundle prepareForBillingService(String appId,String encryptedData) throws RemoteException { 

      Bundle bundle = new Bundle(); 
      Intent intent = new Intent(); 
      intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 
      intent.setClass(InBillingService.this, xx.xxx.activities.BilingActivity.class); 
      intent.putExtra(Consts.PENDING_INTENT_INFO, result); 

      PendingIntent pendingIntent = PendingIntent.getActivity(InBillingService.this, 0, intent, 0);       

      bundle.putParcelable(Consts.PENDING_INTENT, pendingIntent); 

      return bundle ; 

} 

    }; 

    } 

在BillingActivity

//if no problems 
finish() 

感謝您的閱讀!

---更新爲B活性

我的清單文件:

<activity 
     android:name=".activities.BillingActivity" 
     android:configChanges="orientation|keyboardHidden" 
     android:launchMode="singleTask" 
     android:label="@string/title_activity_billing" > 
     <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 
     </intent-filter> 
    </activity> 
+0

不知道解決我的問題嗎?我很着急,我會感謝您的幫助。謝謝 – johann 2013-03-21 01:17:53

+0

B應用程序不是服務嗎? AIDL通常用作客戶端/服務器模型中的IPC。所以我假設應用程序A是客戶端,應用程序B是服務器形式的服務器。爲什麼應用程序B有與之相關的活動? – Luis 2013-04-01 04:27:44

+0

B應用程序包含一個爲B應用程序中的活動返回pendingIntent的服務。我的解釋清楚了嗎? – johann 2013-04-01 04:46:10

回答

2

爲了返回應用程序A BillingActivity在應用程序B完成後,請執行下列操作:

在應用程序B的清單文件,使BillingActivity launchMode singleInstance如下所示:

<activity 
    android:name="xx.xxx.activities.BillingActivity" 
    android:launchMode="singleInstance" > 
</activity> 

每此link

的「singleTask」和「singleInstance」模式也從每個 其他不同只在一個方面:一個「singleTask」活動允許其他 活動是其任務的一部分。它始終在其任務的根目錄 任務中,但其他活動(必須是「標準」和「單個頂級」活動)可以啓動到該任務中。另一方面,「單一實例」活動不允許其他活動成爲其任務的一部分 。這是該任務中唯一的活動。如果它開始另一個 活動,那麼該活動將被分配給不同的任務 - 就好像 FLAG_ACTIVITY_NEW_TASK的意圖。

+0

完成!謝謝 – johann 2013-04-02 04:46:45

相關問題