Application OnCreate multiple call issues in Android

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.