2013-04-08 3 views
0

J'ai passé 6 heures à essayer de résoudre ce problème sans succès.Données qui persistent entre l'activité et le service (aidl)

J'ai 2 applications qui communiquent via un service AIDL. Voici le code source:

application 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; 
} 

Service dans l'application B retourne un Bundle qui contient une 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; 

    } 
}; 
} 

Dans mon fichier Manifest:

<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> 

Activité lancée avec le pendingIntent:

public void onCreate(Bundle savedInstanceState) { 
... 
Bundle bundle = getIntent().getExtras(); 
serviceInfo = bundle.getParcelable(Consts.BUNDLE_APPLI); 

//Data processing 
... 

finish() 

} 

Les données passées en tant que paramètres dans PendingIntent (Consts.PENDING_INTENT_INFO) sont différentes pour chaque appel. Cependant, après le premier appel, les données de la classe d'activité (serviceInfo) sont identiques à chaque fois. Les données semblent persister quelque part.

J'ai vérifié tous les points ci-dessous:

  • L'activité est fermée avec finition() après traitement des données
  • connexion au service est fermé dans la méthode OnDestroy()
  • "résultat" objet (au service) est instancier lorsque la méthode est appelée
  • objet « paquet » est intantiate avant d'appeler le service

Je suis pressé et j'apprécierais votre aide précieuse.

Merci d'avoir lu!

Répondre

0

Je l'ai réparé, merci!

J'ai changé la ligne ci-dessous:

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

à

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

Merci pour votre aide.

Questions connexes