6

I am starting an android service using,

startService(getApplicationContext(), MyService.class);

I have correctly defined my service in AndroidManifest. Now, I am calling above code from Application create.

Case 1: Calling above code from Application onCreate()

  • I see that Application.onCreate() gets called two time. One is the desired App create and other happens when startService is called.

Case 2: Calling above code from Activity in application

  • Same behavior as case 1.

Is this intended behavior?

My Android Manifest Code as requested:

    <service
            android:exported="false"
            android:enabled="true"
            android:name=".MyService"
            android:process=".MyService">
    </service>
VendettaDroid
  • 3,131
  • 2
  • 28
  • 41

1 Answers1

12

Since you specified the android:process attribute in your <service> element, and its value is not the same as your application package name, that service is actually running in a separate process from the default process for your application. (I don't know if it was intentional, but you also seem to have a typo in the process name.)

If you did not intend to run the service in a separate process (which is rare, and something you should only do if you have a good reason and understand the implications), you should just omit the android:process attribute in your <service> element -- this would cause it to run in the same process as the rest of your app.

A little-known and seemingly undocumented behavior of Android is that each process of an application has is own Application instance. This explains why starting your service created an additional Application instance.

Also, not only do the 2 processes have their own Application instances, they actually have their own Application classes, since they do not even share the same class loaders. Therefore, even their static variables can have different values.

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • I did intend to run a service in a different process. I understand its implications. All, I do not understand is why my application's onCreate is called when startService is called. Do you have any hint or reference to this undocumented behavior you are talking about. – VendettaDroid May 19 '15 at 23:28
  • I have only my own past experience, when I worked on an app with two processes. But it also makes sense that every process has its own `Application` instance, since there is no way to share an instance of an object between 2 processes. If you were to ask for an Application context from each process, you have to be able to get one. – cybersam May 19 '15 at 23:30
  • 1
    Also, not only do the 2 processes have their own Application instances, they actually have their own Application classes, since they do not even share the same class loaders. Therefore, even their static variables can have different values. – cybersam May 19 '15 at 23:37
  • Just tried to print Log.d("$$$$", "App. Created: " + this); in the Application OnCreate and it is the same instance. Log: 05-19 16:50:07.496 26145-26145/com.tivo.myapplication5.app D/$$$$﹕ App. Created: com.tivo.myapplication5.app.MyApplication@1da8af19 05-19 16:50:07.894 26177-26177/.helloservice D/$$$$﹕ App. Created: com.tivo.myapplication5.app.MyApplication@1da8af19 – VendettaDroid May 19 '15 at 23:51
  • 1
    In your case, it is possible that both instances have the same address, since they are in different processes (which have their own address space). Try this. In your Application subclass, define uninitialized static `foo` and nonstatic `bar` variables. In `onCreate()`, log their values, and then assign values to them. After the service is launched, `onCreate()` will be called again but you should see in the log that those variables still have null values. – cybersam May 19 '15 at 23:51
  • Did that, Log.d("$$$$", "App. Created: " + this + " foo: " + foo + " bar: " + bar); foo ="foo"; bar="bar"; and the values are null both times. – VendettaDroid May 20 '15 at 00:03
  • 1
    So, that proves that there are 2 classes and 2 instances. That implies that there are 2 processes. And actually, you could also log the process ID, using `Process.myPid()`. – cybersam May 20 '15 at 00:04
  • 1
    that explains a lot. Also, seems like android documented this here http://developer.android.com/reference/android/app/Application.html#onCreate() – VendettaDroid May 20 '15 at 00:14