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