【问题标题】:Android: Bind applications (AIDL) in singleTop/ singleTask ModeAndroid:在 singleTop/singleTask 模式下绑定应用程序 (AIDL)
【发布时间】:2013-03-18 09:35:16
【问题描述】:

我已经用一个 AIDL 文件绑定了 2 个安卓应用程序。 A 和 B 应用程序之间的交换如下:

  • A 应用通过 AIDL 连接到 B 应用 界面
  • A应用调用B服务的方法
  • B 服务返回一个 PendingIntent
  • 应用程序启动 PendingIntent
  • 当 B 应用程序中的操作完成时:返回 A 应用

但是,如果 B 应用程序在交换之前启动,则在调用“finish()”方法后,用户将被重定向到 B 应用程序的最后一个活动,而不是在 A 应用程序中。

在我的 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());
        }

我尝试在 A 应用程序清单文件中将 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>

【问题讨论】:

  • 不知道如何解决我的问题?我很着急,我会感谢你的帮助。谢谢
  • B应用不是服务吗? AIDL 通常用作客户端/服务器模型中的 IPC。所以我假设应用程序 A 是客户端,应用程序 B 是服务形式的服务器。为什么应用 B 有与之关联的活动?
  • B 应用程序包含一个服务,该服务为 B 应用程序中的活动返回一个pendingIntent。我的解释清楚吗?
  • 好的,我想我现在明白发生了什么。我认为您需要做的是将服务与应用程序 B 中的活动分开。您可以在应用程序 B 中发布服务的代码吗?计费活动在哪个应用程序中?这里是否涉及三个应用程序:App A(发出请求)、App B(响应请求)、App C(在 App B 响应后从 App A 启动)?还是计费活动是应用 A 的一部分?
  • 我已经更新了我的服务代码。 BillingActivity 在 B 应用程序中。谢谢!

标签: android aidl back-stack launchmode


【解决方案1】:

为了在应用程序 B 中完成 BillingActivity 后返回应用程序 A,请执行以下操作:

在应用程序 B 的清单文件中,将 BillingActivity 的 launchMode 设为 singleInstance,如下所示:

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

据此link

“singleTask”和“singleInstance”模式也各不相同 只有一个方面的其他:“singleTask”活动允许其他 活动成为其任务的一部分。它始终是它的根源 任务,但其他活动(必须是“标准”和“singleTop” 活动)可以启动到该任务中。一个“单一实例” 另一方面,活动不允许其他活动成为其中的一部分 它的任务。这是任务中的唯一活动。如果它开始另一个 活动,该活动被分配给不同的任务 - 好像 FLAG_ACTIVITY_NEW_TASK 在意图中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-29
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多