How Android turns on application with remote service

Source: Internet
Author: User

Under normal circumstances, the application OnCreate method of the entire Android application will only be executed once while the program is running, but there are special cases when remote services are turned on in the application, and the remote service is running in a separate process. This time application's OnCreate method is called repeatedly. Many times we want the initialization method in the application to execute only once, so it's time to make a decision about whether the application OnCreate method executes because the remote service is turned on.

The principle of judgment is to get the name of the process through the process ID, if the name of the process is inconsistent with the current application, then it is the process of the remote service, by default the name of the main process is the package name of the app.

private string getappname (int pID) {String processName = null; Activitymanager am = (activitymanager) this.getsystemservice (Activity_service); list<runningappprocessinfo> Runningappprocessinfos = am.getrunningappprocesses ();Iterator< runningappprocessinfo> i = Runningappprocessinfos.iterator (); while (I.hasnext ()) { Activitymanager.runningappprocessinfo info = (activitymanager.runningappprocessinfo) (I.next ()); try {if (Info.pid = = PID) {processName = Info.processname;return processName;}} catch (Exception e) {e.printstacktrace ();}} return processName;}

The ID of the process can be obtained by Android.os.Process.myPid (), and the name of the process can be obtained by means of the method above.

The process name of the remote service is determined by the Android:process attribute under the service tag in the Androidmanifest.xml file. After testing this name must be in. The name of the remote service that can be determined by the above method is the name of the. + Name.

How Android turns on application with remote service

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.