1 package com.android.nfc; 2 3 import android.app.ActivityManager; 4 import android.app.ActivityManager.RunningAppProcessInfo; 5 import android.app.Application; 6 import android.os.Process; 7 import android.os.UserHandle; 8 import java.util.Iterator; 9 import java.util.List; 10 11 public class NfcApplication extends Application { 12 13 static final String TAG = "NfcApplication"; 14 static final String NFC_PROCESS = "com.android.nfc"; 15 16 NfcService mNfcService; 17 18 public NfcApplication() { 19 20 } 21 22 @Override 23 public void onCreate() { 24 super.onCreate(); 25 26 boolean isMainProcess = false; 27 // We start a service in a separate process to do 28 // handover transfer. We don't want to instantiate an NfcService 29 // object in those cases, hence check the name of the process 30 // to determine whether we're the main NFC service, or the 31 // handover process 32 ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE); 33 List processes = am.getRunningAppProcesses(); 34 Iterator i = processes.iterator(); 35 while (i.hasNext()) { 36 RunningAppProcessInfo appInfo = (RunningAppProcessInfo)(i.next()); 37 if (appInfo.pid == Process.myPid()) { 38 isMainProcess = (NFC_PROCESS.equals(appInfo.processName)); 39 break; 40 } 41 } 42 if (UserHandle.myUserId() == 0 && isMainProcess) { 43 mNfcService = new NfcService(this); 44 } 45 } 46 } 47