Starting Service in Android calls Applications onCreate

后端 未结 1 1569
忘了有多久
忘了有多久 2021-01-18 13:04

I am starting an android service using,

startService(getApplicationContext(), MyService.class);

I have correctly defined my service in Andr

1条回答
  •  孤城傲影
    2021-01-18 13:35

    Since you specified the android:process attribute in your 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 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.

    0 讨论(0)
提交回复
热议问题