in Main Process - Android Android OS

Android examples for Android OS:Process

Description

in Main Process

Demo Code


import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Process;
import android.util.Log;
import static android.app.ActivityManager.RunningAppProcessInfo;

public class Main{
    private static final String TAG = ProcessHelper.class.getSimpleName();
    public static boolean inMainProcess(Context context) {
        String currentProcess = getProcessName(context);
        try {/* ww  w . j  a va 2s .  com*/
            ApplicationInfo appInfo = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(), 0);
            String appProcess = appInfo.processName;
            return currentProcess.equals(appProcess);
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public static String getProcessName(Context context) {
        int myPid = Process.myPid();
        return getProcessName(context, myPid);
    }
    public static String getProcessName(Context context, int pid) {
        ActivityManager am = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningAppProcessInfo info : am.getRunningAppProcesses()) {
            if (info.pid == pid) {
                return info.processName;
            }
        }

        Log.w(TAG, "Unexpected failure resolving pid " + pid + " to name");
        return String.valueOf(pid);
    }
}

Related Tutorials