Application multiple initialization problem

3049 단어
When doing interface optimization, it was found that the onCreate method of the APP's Application was executed several times, resulting in some initialization operations in onCreate being repeated many times. The interruption point was fruitless, but the packet capture really existed. This is very embarrassing to me. . . Only later did I find out that there are some third-party components, which opened a separate process. The solution is to initialize and filter according to the process name, which is only initialized once in onCreate of Application. Solution 1: Execute the initialization code only after judging that the current process is the main process. Android avoids executing the initialization code multiple times in the multi-process Application onCreate
if (TextUtils.equals(getCurrentProcessName(this), getPackageName())) {
         init();// 
}
private String getCurrentProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
}
Solution 2: The content is excerpted from: Application initialization problem when Android multi-process, big pit
public class MyApplication extends Application {
    private final static String PROCESS_NAME = "com.test";
    private static MyApplication myApplication = null;
 
    public static MyApplication getApplication() {
        return myApplication;
    }
 
    /**
     *  UI , UI 
     */
    public static boolean isAppMainProcess() {
        try {
            int pid = android.os.Process.myPid();
            String process = getAppNameByPID(MyApplication.getApplication(), pid);
            if (TextUtils.isEmpty(process)) {
                return true;
            } else if (PROCESS_NAME.equalsIgnoreCase(process)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
    }
 
    /**
     *  Pid 
     */
    public static String getAppNameByPID(Context context, int pid) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (android.app.ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo.pid == pid) {
                return processInfo.processName;
            }
        }
        return "";
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
 
        myApplication = this;
 
        if (isAppMainProcess()) {
            //do something for init
        }
    }
}

좋은 웹페이지 즐겨찾기