Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
707 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 Answer

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");
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...