Я потратил 6 часов, пытаясь решить эту проблему, но безуспешно.
У меня есть 2 приложения, которые взаимодействуют через службу AIDL. Вот исходный код:
Приложение А:
protected void onCreate(Bundle savedInstanceState) {
...
Intent i = new Intent();
i.setClassName("xxx.service","xx.service.MyService");
try {
Boolean ret = bindService(i, mConnection, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
Utils.error("application not installed");
}
...
button1.setOnClickListener(this);
//service connection instance
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder boundService) {
service = BillingInterface.Stub.asInterface((IBinder) boundService);
Utils.debug(mContext, "connection to service !");
}
public void onServiceDisconnected(ComponentName name) {
service = null;
}
};
...
//when clicking button, method of service is called
public void onClick(View arg0) {
bundle = null ;
bundle = service.myMethod(myId);
PendingIntent pIntent = bundle.getParcelable(Utils.RESPONSE_P_INTENT);
try {
Intent in = new Intent();
pIntent.send(mContext, 0, in);
} catch (PendingIntent.CanceledException e) {
Utils.error("Sending contentIntent failed:" + e.getMessage());
}
}
}
public void onDestroy(){
super.onDestroy();
unbindService(mConnection);
mConnection = null;
}
Служба в приложении B возвращает Bundle, который содержит PendingIntent:
@Override
public IBinder onBind(Intent arg0) {
return new BillingInterface.Stub() {
@Override
public Bundle myMethod(String id) throws RemoteException {
//Service class implements parcelable class
DB.domains.Service result = dao.getServiceDetails(appId);
Intent intent = new Intent();
intent.setClass(BillingService.this, xx.appli.test.class);
intent.putExtra(Consts.PENDING_INTENT_INFO, result);
PendingIntent pendingIntent;
pendingIntent = PendingIntent.getActivity(BillingService.this, 0, intent, 0);
bundle.putParcelable(Consts.PENDING_INTENT, pendingIntent);
return bundle;
}
};
}
В моем файле манифеста:
<service
android:name=".service.BillingService"
android:enabled="true"
android:exported="true"
android:process=":remote" >
<intent-filter>
<action android:name=".service.BillingInterface.aidl" />
</intent-filter>
</service>
Активность запущена с pendingIntent:
public void onCreate(Bundle savedInstanceState) {
...
Bundle bundle = getIntent().getExtras();
serviceInfo = bundle.getParcelable(Consts.BUNDLE_APPLI);
//Data processing
...
finish()
}
Данные, передаваемые в качестве параметров в PendingIntent (Consts.PENDING_INTENT_INFO), различаются для каждого вызова. Однако после первого вызова данные в классе активности (serviceInfo) каждый раз идентичны. Данные, кажется, сохраняются где-то.
Я проверил все пункты ниже:
- Активность закрывается с помощью finish() после обработки данных
- Соединение с сервисом закрывается в методе OnDestroy()
- Объект "результат" (в службе) создается при вызове метода
- Объект "bundle" создается перед вызовом службы
Я спешу и был бы признателен за вашу драгоценную помощь.
Спасибо за чтение !