Using android: process = ": remote" recreates the android Application object

I am using the AIDL service in my application. I also want to start another process, so I use android:process=":remote" in the service declaration in the manifest.

My problem is that when the process starts :remote it apparently recreates the Application object.

I really do not do this, as I override the application object and call many client elements in the onCreate() method. However, I want the service code to be in the same apk with the client.

Can i achieve this? Is the Application object always recreated when a new process starts?

Appreciate your help. Thanks!

+5
android process service
source share
2 answers

I also want to start another process

Why? What value does this add to the user to compensate for additional RAM, CPU and battery cost? Very few applications require multiple processes.

My problem is that when the ": remote" process starts, it apparently recreates the application object

Of course. Each process gets its own.

I really don't understand this, because I am redefining the application object and calling many client elements in the onCreate () method

Then get rid of android:process=":remote" . Your users will thank you.

However, I want the service code to be in the same apk with the client.

What service?

Is the application object always recreated when starting a new process?

Yes.

+4
source share

As CommonsWare has already been mentioned, each of the processes gets its own Application object.

In your Application.onCreate() method, you can check whether the method is called from the main process or from a remote process, and initialize different things accordingly.

 @Override public void onCreate() { super.onCreate(); if(isRemoteProcess(this)) { // initialize remote process stuff here } else { // initialize main process stuff here } } private boolean isRemoteProcess(Context context) { Context applicationContext = context.getApplicationContext(); long myPid = (long) Process.myPid(); List<RunningAppProcessInfo> runningAppProcesses = ((ActivityManager) applicationContext.getSystemService("activity")).getRunningAppProcesses(); if (runningAppProcesses != null && runningAppProcesses.size() != 0) { for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) { if (((long) runningAppProcessInfo.pid) == myPid && "YOUR_PACKAGE_NAME:remote".equals(runningAppProcessInfo.processName)) { return true; } } } return false; } 
+1
source share

All Articles