Android Development android:process Properties _application OnCreate executed multiple times

Source: Internet
Author: User


Application to do in a hurry, many places only to realize the function, did not take into account good performance, so stop to refactor the code optimization performance, the results in the log to see the startup time, found

Application's oncreate executes several times, which results in a duplicate initialization of the resource, initialized 3 times resulting in a close to 1s time.


Found that this was caused by an independent process. That is, the process property.

android:process

            The name of the process where the service is located. Typically, all components of an app run in the default process that the system creates for the app. This default process is named after the application's package name.

label process Span style= "word-wrap:normal; Word-break:normal; line-height:30px; Font-family:simsun; " The value to override this default value, which allows your app to run across multiple processes.

If the process name that is set begins with a colon, the new process is private to the application, and the new process is created when it is needed or the service needs to run in a new process. If the name of the process starts with a lowercase character, the service will run in a global process named after that name, assuming that it has the appropriate permissions. This allows various components in different applications to share a single process, thereby reducing the resource footprint.

       For example, an app runs on a processCom.aoyousatuo.examplein whichandroid:processproperty is set toCom.rabbit.man,The new process name isCom.rabbit.run.


in general, a service does not have its own independent process, which is typically run as a thread in the process of the application in which it resides. But there are exceptions,Androidin the declaration fileandroid:processproperty can be used for any component, including applying a specified process, in other words, by setting in the declaration fileandroid:processProperties,We can let components (such asActivity, Serviceetc.) and applications(application)created and run in the process we specified.

  If we need to have a service run in a remote process (instead of the standard

> Note: Here you select "remote" ": '

   <service            android:name= "com.baidu.location.f"            android:enabled= "true"            android:process= ": BaiduMap" >        </service>

In Ddms, you can see that this process is

Com.example.hello:baiduMap

( If the component in the declaration file or the app does not specify this property then the default app and its components will run accordingly in the process named after their package name )


Here's how to fix it:


@Overridepublic void OnCreate () {super.oncreate (); mapplication = This;long S1 = System.currenttimemillis (); String processName = Osutils.getprocessname (This,android.os.process.mypid ()); L.D (Wmodel.time, "process name" +processname), if (processName! = null) {Boolean defaultprocess = Processname.equals ( Wmapconstants.real_package_name), if (defaultprocess) {//Required initialization of resource operations}}L.D (Wmodel.time, "OnCreate time-consuming" + ( System.currenttimemillis ()-S1));}

/**     * @return NULL may be returned if the specified process is not found     *    /public static String Getprocessname (C Ontext cxt, int pid) {        Activitymanager am = (activitymanager) cxt.getsystemservice (context.activity_service);        list<runningappprocessinfo> Runningapps = am.getrunningappprocesses ();        if (Runningapps = = null) {            return null;        }        for (Runningappprocessinfo Procinfo:runningapps) {            if (procinfo.pid = = pid) {                return procinfo.processname;< c11/>}        }        return null;    }


Such a dozen is initialized once and does not result in a long wasted time.


jason0539

Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)

Sweep code Follow me public number


Android Development android:process Properties _application OnCreate executed multiple times

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.