android提示程序正在执行,Android中获取正在运行的进程(一)
關于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等,謝謝!!!
總結
以上是生活随笔為你收集整理的android提示程序正在执行,Android中获取正在运行的进程(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql分组失效_请教MySql中使用
- 下一篇: android应用对于内存的大小是有限制