关于android中应用程序正在运行的进程有下面几种:1-包含services的进程,2-不包含services的进程,3-杀死应用时有些进程被init回收(类似于僵尸进程),ppid变为1,无法通过android 应用层代码获得的进程。这些进程可能是应用的重启进程,导致应用重启,也就是我们做了杀死应用进程的操作后,发现一段时间后,应用又重新启动的现象。
一、对于前两者,可以调用androud系统函数来获得,示例如下:
ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE) ;
//获得系统运行的进程
List appList1 = mActivityManager
.getRunningAppProcesses();
for(RunningAppProcessInfo running : appList1) {
System.out.println(running.processName);
}
System.out.println("================");
//获得当前正在运行的service
List appList2 = mActivityManager
.getRunningServices(100);
for(ActivityManager.RunningServiceInfo running : appList2) {
System.out.println(running.service.getClassName());
}
System.out.println("================");
//获得当前正在运行的activity
List appList3 = mActivityManager
.getRunningTasks(1000);
for(ActivityManager.RunningTaskInfo running : appList3) {
System.out.println(running.baseActivity.getClassName());
}
System.out.println("================");
//获得最近运行的应用
List appList4 = mActivityManager
.getRecentTasks(100,1);
for(ActivityManager.RecentTaskInfo running : appList4) {
System.out.println(running.origActivity.getClassName());
}
二、对于第三种,我们都知道在linux shell中输入ps命令,可以查看所有运行的进程,
root@android:/ #
root@android:/ # ps
USER PID PPID VSIZE RSS WCHAN PC NAME
root 1 0 412 244 c00cf1c0 0000ffe4 S /init
root 2 0 0 0 c0044560 00000000 S kthreadd
root 3 2 0 0 c002eae4 00000000 S ksoftirqd/0
root 6 2 0 0 c007aaa4 00000000 S migration/0
root 7 2 0 0 c007dfb0 00000000 S watchdog/0
root 12 2 0 0 c003fd14 00000000 S khelper
root 13 2 0 0 c003fd14 00000000 S netns
.......
root 17 2 0 0 c003fd14 00000000 S suspend_sys_syn
u0_a55 16896 1413 545192 52208 ffffffff 4010eea4 S com.tencent.mm
u0_a55 16934 1413 526176 34352 ffffffff 4010eea4 S com.tencent.mm:push
u0_a37 17719 1413 512816 30040 ffffffff 4010eea4 S com.nufront.NufrontTestDemo
u0_a25 17835 1413 487536 22400 ffffffff 4010eea4 S com.android.smspush
root 18674 1432 928 544 c0011ad4 40160934 S /system/bin/sh
root 18688 18674 1236 460 00000000 400bfd38 R ps
再执行kill命令,就可以杀死对应的进程
root@android:/ # kill 16896
再有,如果执行ps | grep试试看:
root@android:/ #
root@android:/ #ps | grep tencent
u0_a55 16896 1413 545192 52208 ffffffff 4010eea4 S com.tencent.mm
u0_a55 16934 1413 526176 34352 ffffffff 4010eea4 S com.tencent.mm:push
root@android:/ #
在android中有一个函数是有kill命令功能的: Process.killProcess(int pid);
所以,我们只需要根据package name,找到没有被杀死的进程的进程号,再调用killprocess()函数,就可以杀死一直杀不死的进程了。可参考下面的代码:
java.lang.Process process=null;
Runtime rt=Runtime.getRuntime();
System.out.println("start");
try {
process=rt.exec("busybox pgrep nuomi");
InputStream is=null;
InputStreamReader isr=null;
BufferedReader buffer=null;
String line=null;
String xxx = null;
is=process.getInputStream();
isr=new InputStreamReader(is);
buffer=new BufferedReader(isr);
while((line=buffer.readLine())!=null){
System.out.println(line+"----------");
Process.killProcess(Integer.valueOf(line));
xxx = line;
}
System.out.println(xxx+"***********");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end");
三、另外一些关于应用程序的函数可参考下面
推荐方法
1、
//全部程序包
private void queryFilterAppInfo() {
PackageManager pm = this.getPackageManager();
// 查询所有已经安装的应用程序
List
listAppcations = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);// GET_UNINSTALLED_PACKAGES代表已删除,但还有安装目录的
for (ApplicationInfo app : listAppcations) {
AppInfo appInfo = new AppInfo();
appInfo.setAppLabel((String) app.loadLabel(pm));
appInfo.setAppIcon(app.loadIcon(pm));
appInfo.setPkgName(app.packageName);
mlistAppInfo.add(appInfo);
}
}
2、
// 获得所有系统的
public void querySysAppInfo()
{
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// 通过查询,获得所有ResolveInfo对象.
List resolveInfos =
pm.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo reInfo : resolveInfos) {
String activityName = reInfo.activityInfo.name; //
获得该应用程序的启动Activity的name
String pkgName = reInfo.activityInfo.packageName; // 获得应用程序的包名
String appLabel = (String) reInfo.loadLabel(pm); //
获得应用程序的Label
Drawable icon = reInfo.loadIcon(pm); // 获得应用程序图标
System.out.println(appLabel + " activityName---" +
activityName
+ " pkgName---" + pkgName);
}
}
3、 //最简单的方法
public boolean appIsInstalled(String
packageName){
try {
PackageManager pm = this.getPackageManager();
pm.getPackageInfo(packageName,
PackageManager.GET_ACTIVITIES);
System.out.println("已安装"+packageName);
} catch (NameNotFoundException e) {
System.out.println("没有安装"+packageName);
return false;
}
return true;
}
4、// 获得所有包信息
public void getInstallPackage() {
PackageManager packageManager =
this.getPackageManager();
List
packageInfoList=packageManager.getInstalledPackages(0);
mlistAppInfo.clear();
for (int
i=0;i
{
PackageInfo pi = packageInfoList.get(i);
System.out.println((i+1)+":"+pi.toString());
}
}
5、 //直接启动方法
public void srartApp(String
packageName){
Intent intent =
this.getPackageManager().getLaunchIntentForPackage(packageName);
if(intent!=null)
//已安装包 直接启动
startActivity(intent);
}
参考:androidbin.iteye.com/blog/1594908,blog.sina.com.cn/s/blog_3e333c4a01015v3e.html等,谢谢!!!