Bonjour,
Voila mon petit souci:
Depuis quelques jours je souhaite échanger un objet entre 2 activity.
La meilleur solution que j'ai trouvé semble être d'utiliser un service en background qui s'occupera des Get et Set mes objets.
Pour cela j'ai codé le service en background en utilisant l'exemple référance de google http://developer.android.com/referen...p/Service.html
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class BackgroundService extends Service { /** * Class for clients to access. Because we know this service always * runs in the same process as its clients, we don't need to deal with * IPC. */ // This is the object that receives interactions from clients. See // RemoteService for a more complete example. private final IBinder mBinder = new LocalBinder(); public class LocalBinder extends Binder { public BackgroundService getService() { return BackgroundService.this; } } @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("BackgroudService", "Received start id " + startId + ": " + intent); // We want this service to continue running until it is explicitly // stopped, so return sticky. return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } }
et pour acceder au service j'ai codé dans mon activity principal:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 void DemarrerService () { BackgroundService backgroundService; ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub mIsBound = true; Log.i("BackgroundService","Conected"); mBoundService = ((BackgroundService.LocalBinder)service).getService(); // Toast.makeText(this, "Connected",Toast.LENGTH_SHORT).show(); } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub mIsBound = false; Log.i("BackgroundService","Disconected"); }}; // Establish a connection with the service. We use an explicit // class name because we want a specific service implementation that // we know will be running in our own process (and thus won't be // supporting component replacement by other applications). Intent intent = new Intent(this,BackgroundService.class); boolean a=bindService(intent, mConnection, Context.BIND_AUTO_CREATE); }
Dans mon intent je met en premier argument "this", alors que dans l'exemple de google que je suis ils mettent "binding.this", or je ne comprend pas ce que c'est et je ne trouve aucune info à ce sujet.....
Mon problème doc c'est que "bindService" revoit toujours false et je n'arrive pas à comprendre pourquoi, quelqu'un aurait-t-il une idée??
Merci
Partager