0

How is it possible to share an object between a service running in a specific process and the rest of my Android app ?

In my manifest I set process="com.test.myprocess" to the service declaration.

I initialize a "session" object when my user login in himself on the app. Then I start an Android service and the session object is injected in it with Android Annotation. But when I do session.getUuid() from my injected session in my service, I always get "null".

If I do the same in the process corresponding to my app (e.g in the UiThread) the value is not null.

Thanks

psv
  • 3,147
  • 5
  • 32
  • 67
  • Yes, when the user swipe off the app from the recent android app, I don't want that Android kill the service – psv Jul 12 '16 at 09:10
  • for this create a [Bind Service using Messenger](https://developer.android.com/guide/components/bound-services.html#Messenger) :) let me know if u have any question – ρяσѕρєя K Jul 12 '16 at 09:10
  • I'm using Android Annotation, I don't know if there is a simple way to bind a service with this framework ? – psv Jul 12 '16 at 09:12
  • @psv: you can do R&D for this because i have never used Android Annotation. and another option is use Broadcast receiver for communicating between two process – ρяσѕρєя K Jul 12 '16 at 09:17
  • Why are you running your `Service` in a separate process? – David Wasser Jul 13 '16 at 16:02
  • When the user swipe off the app from the recent android app, I don't want that Android kill the service – psv Jul 18 '16 at 09:16
  • When the user removes a task from the list of recent tasks, this does not necessarily kill off running processes. This behaviour is different in different Android versions. Also, even if your `Service` is running in a separate process, Android may still kill it off, just for fun. Android kills off background processes at any time for any reason, so your app needs to cope with that situation anyway. – David Wasser Jul 18 '16 at 10:59

2 Answers2

0

For sending an object from Service to an Activity I would suggest using a function:

public void sendBroadcast(Intent intent);

Create an appropriate Intent, put your variable or extra object. You object must implement Serialisable interface. If it can't serialise it to JSON and pass a string. Like this:

Intent intent = new Intent();
intent.setAction("UNIQUE_BROADCAST_ID");
intent.putExtra("EXTRA_ID", ...);
this.sendBroadcast(intent);

In your Activity register a BroadcastReceiver with a specific code:

registerReceiver(receiver, new IntentFilter("UNIQUE_BROADCAST_ID"));
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
  • yes... this may work but I will probably manually bind the service as the android doc advice. it's just weird that there is no simple way to do that with Android Annotation – psv Jul 12 '16 at 09:53
0

Did you bind the service? you can learn some about AIDL. A class object is only belong to a process ,so when you use Annotation to get session object in another process the result is null.

whis
  • 1
  • Are you sure a class object is only belong to a process even if my app creates the two processes? If so it would means that if I set process="com.test.myprocess", I must use AIDL architecture? It seems a bit difficult to use AIDL with Android Annotation, I have to work with the "stock" android API... which means no injection dependencies – psv Jul 12 '16 at 12:33
  • yes, a process is running on a dalvik virtual machine. for a java file there is only one calss object in the dvm . as others said you can use Broadcast receiver . – whis Jul 12 '16 at 12:57