活动和服务之间持续存在的数据(aidl)

时间:2013-04-08 10:06:47

标签: android android-pendingintent aidl data-persistence

我花了6个小时试图解决这个问题但没有成功。

我有2个通过AIDL服务进行通信的应用程序。这是源代码:

申请表A:

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中的服务返回包含PendingIntent:

的Bundle
@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()方法
  • 中关闭与服务的连接
  • “result”对象(在服务中)在调用方法时实例化
  • “bundle”对象在调用服务
  • 之前是实例化的

我很着急,非常感谢你的宝贵帮助。

感谢您阅读!

1 个答案:

答案 0 :(得分:0)

我已经修好了,谢谢!

我改变了下面的一行:

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

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

感谢您的帮助。