Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

android - How to get PID from package name?

I need create a function but I don't know how to get the PID of the app.

// here return -1 but I need PID to kill process
Integer PID1= android.os.Process.getUidForName("com.android.email");

android.os.Process.killProcess(PID1);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try

restartPackage code

ActivityManager aM = (ActivityManager);
getApplicationContext().getSystemService(getApplicationContext().ACTIVITY_SERVICE);
aM.restartPackage("com.android.email");

Kill BackGround Process Code

ActivityManager aM = (ActivityManager)
getApplicationContext().getSystemService(Catalogue.content_holder.getApplicationContext().ACTIVITY_SERVICE);
aM.killBackgroundProcesses("com.android.email");

Here is code which fetches all running Application and check wether email app is already running or not , if it is running then kill that process

ActivityManager manager =  (ActivityManager) getApplicationContext.getSystemService(getApplicationContext.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> activityes = ((ActivityManager)manager).getRunningAppProcesses();

for (int iCnt = 0; iCnt < activityes.size(); iCnt++){

    System.out.println("APP: "+iCnt +" "+ activityes.get(iCnt).processName);

    if (activityes.get(iCnt).processName.contains("com.android.email")){
        android.os.Process.sendSignal(activityes.get(iCnt).pid, android.os.Process.SIGNAL_KILL);
        android.os.Process.killProcess(activityes.get(i).pid);
        //manager.killBackgroundProcesses("com.android.email");

        //manager.restartPackage("com.android.email");

        System.out.println("Inside if");
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...