get process Uid - Android Android OS

Android examples for Android OS:Process

Description

get process Uid

Demo Code


//package com.java2s;
import android.app.ActivityManager;
import android.content.Context;

import java.util.List;

public class Main {

    public static int getUid(Context context) {
        if (context == null) {
            return -1;
        }/*from   ww  w  .  j a v a 2s  . c  o m*/

        int pid = android.os.Process.myPid();
        ActivityManager manager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        if (manager != null) {
            List<ActivityManager.RunningAppProcessInfo> infos = manager
                    .getRunningAppProcesses();
            if (infos != null && !infos.isEmpty()) {
                for (ActivityManager.RunningAppProcessInfo processInfo : infos) {
                    if (processInfo.pid == pid) {
                        return processInfo.uid;
                    }
                }
            }
        }
        return -1;
    }
}

Related Tutorials