1 problem Description: An Android application needs to open a separate process for a service to complete the logic of interacting with the server, while initializing the object in the OnCreate method of the Application object, most notably with a polling thread, tested to find Application power consumption is very serious. After testing found that the process of service is also running the polling thread, so that in the network environment is not good, frequent requests for network activation equipment, will lose a lot of power;
2 problem Reason: Every Android application is running on a virtual machine, when the application configured two processes, there are actually two VMs running, a foreground application process, a service background process, each process corresponds to a Application object, So when the application is configured with multiple processes, the OnCreate method of the Application object executes multiple times, so if you turn polling or initialize large amounts of data in the OnCreate method of application, you actually have to make a distinction between the processing;
3 Fix the problem: we already know that each process corresponds to a Application object, in order to avoid wasting resources, we can use the name of the process in application to distinguish which resources should be loaded, and what specific logic to execute.
method to get the process name:
Private String Getcurprocessname (context context) {
int pid = Android.os.Process.myPid ();
Activitymanager Activitymanager = (activitymanager) context.getsystemservice (Context.activity_service);
For (Activitymanager.runningappprocessinfo appProcess:activityManager.getRunningAppProcesses ()) {
if (appprocess.pid = = pid) {
return appprocess.processname;
}
}
Return "";
}
Application OnCreate multiple call issues in Android