Android 读取正在运行非系统的程序
2013-06-25 16:53
344 查看
1)创建RunningAppInfo类,代码如下
package com.example.runningapps;
import android.graphics.drawable.Drawable;
public class RunningAppInfo {
/*程序名称*/
private String appLabel;
public void SetAppLabel(String _appLabel){
this.appLabel=_appLabel;
}
public String GetAppLabel(){
return this.appLabel;
}
/*程序图标*/
private Drawable appIcon;
public void SetAppIcon(Drawable _appIcon){
this.appIcon=_appIcon;
}
public Drawable GetAppIcon(){
return appIcon;
}
/*包名称*/
private String pkgName;
public void SetPkgName(String _pkgName){
this.pkgName=_pkgName;
}
public String GetPkgName(){
return this.pkgName;
}
/*进程id*/
private int pid;
public void SetPid(int _pid){
this.pid=_pid;
}
public int GetPid(){
return this.pid;
}
/*进程名称*/
private String processName;
public void SetProcessName(String _processName){
this.processName=_processName;
}
public String GetProcessName(){
return processName;
}
}
2)创建applist.xml作为自定义ListView的样式代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/appIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/appLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:text="TextView" />
<TextView
android:id="@+id/pkgName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/pid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:text="TextView" />
<TextView
android:id="@+id/processName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
3)创建MyListAdapter.java作为自定ListAdapeter代码如下
package com.example.runningapps;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
class MyListAdapter extends BaseAdapter {
private ArrayList<RunningAppInfo> apps=null;
private Context context=null;
public MyListAdapter(ArrayList<RunningAppInfo> _apps,Context _context)
{
this.apps=_apps;
this.context=_context;
}
public int getCount() {
//设置绘制数量
return apps.size();
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView appIconImageView=null;
TextView appLabelTextView=null;
TextView pkgNameTextView=null;
TextView pidTextView=null;
TextView processNameTextView=null;
if (convertView == null || position < apps.size()) {
convertView = LayoutInflater.from(context).inflate(
R.layout.applist, null);
appIconImageView = (ImageView) convertView.findViewById(R.id.appIcon);
appLabelTextView = (TextView) convertView.findViewById(R.id.appLabel);
pkgNameTextView = (TextView) convertView.findViewById(R.id.pkgName);
pidTextView = (TextView) convertView.findViewById(R.id.pid);
processNameTextView = (TextView) convertView.findViewById(R.id.processName);
}
/*图标*/
appIconImageView.setImageDrawable(apps.get(position).GetAppIcon());
/*应用程序名称*/
appLabelTextView.setText(apps.get(position).GetAppLabel());
/*包名称*/
pkgNameTextView.setText(apps.get(position).GetPkgName());
/*进程id*/
pidTextView.setText(Integer.toString(apps.get(position).GetPid()));
/*进程名称*/
processNameTextView.setText(apps.get(position).GetProcessName());
return convertView;
}
}
4)修改MainActivity.java代码将自定ListAdapeter绑定给ListView代码如下
package com.example.runningapps;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
private PackageManager pm=null;
private ListView listview1=null;
private MyListAdapter adapter=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview1=(ListView)findViewById(R.id.listView1);
ArrayList<RunningAppInfo> runningApps=queryAllRunningAppInfo();
adapter=new MyListAdapter(runningApps, MainActivity.this);
listview1.setAdapter(adapter);
}
/* 查找正在运行的应用程序
* 1)获取系统安装的所有应用程序
* 2)根据报名pkgname过滤获取真正运行的应用程序
* */
private ArrayList<RunningAppInfo> queryAllRunningAppInfo(){
pm=this.getPackageManager();
ArrayList<RunningAppInfo> runningApps=new ArrayList<RunningAppInfo>();
//获取所有已经安装的应用程序
List<ApplicationInfo> installedApps=pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
//排序
Collections.sort(installedApps,new ApplicationInfo.DisplayNameComparator(pm));
//保存所有正在运行的包名以及它所在的进程信息
Map<String, RunningAppProcessInfo> pgkProcessAppMap=new HashMap<String, ActivityManager.RunningAppProcessInfo>();
//通过调用ActivityManager的getRunningAppProcesses()方法获得系统里所有正在运行的进程
ActivityManager mActivityManager=(ActivityManager)getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningAppProcesses=mActivityManager.getRunningAppProcesses();
for (RunningAppProcessInfo runningAppProcess : runningAppProcesses) {
//获得正在运行在该进程里的所有应用程序包
String[] pkgNameList=runningAppProcess.pkgList;
for (String pkgName : pkgNameList) {
//将所有的应用程序包添加到map对象里
pgkProcessAppMap.put(pkgName, runningAppProcess);
}
}
//遍历安装了应用程序
for (ApplicationInfo installedApp : installedApps) {
if(pgkProcessAppMap.containsKey(installedApp.packageName)){//安装程序正在运行
if((installedApp.flags&installedApp.FLAG_SYSTEM)==0){//非系统应用程序
RunningAppInfo item=new RunningAppInfo();
item.SetPid(pgkProcessAppMap.get(installedApp.packageName).pid);
item.SetProcessName(pgkProcessAppMap.get(installedApp.packageName).processName);
item.SetAppLabel((String)installedApp.loadLabel(pm));
item.SetAppIcon(installedApp.loadIcon(pm));
item.SetPkgName(installedApp.packageName);
runningApps.add(item);
}
}
}
return runningApps;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package com.example.runningapps;
import android.graphics.drawable.Drawable;
public class RunningAppInfo {
/*程序名称*/
private String appLabel;
public void SetAppLabel(String _appLabel){
this.appLabel=_appLabel;
}
public String GetAppLabel(){
return this.appLabel;
}
/*程序图标*/
private Drawable appIcon;
public void SetAppIcon(Drawable _appIcon){
this.appIcon=_appIcon;
}
public Drawable GetAppIcon(){
return appIcon;
}
/*包名称*/
private String pkgName;
public void SetPkgName(String _pkgName){
this.pkgName=_pkgName;
}
public String GetPkgName(){
return this.pkgName;
}
/*进程id*/
private int pid;
public void SetPid(int _pid){
this.pid=_pid;
}
public int GetPid(){
return this.pid;
}
/*进程名称*/
private String processName;
public void SetProcessName(String _processName){
this.processName=_processName;
}
public String GetProcessName(){
return processName;
}
}
2)创建applist.xml作为自定义ListView的样式代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/appIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/appLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:text="TextView" />
<TextView
android:id="@+id/pkgName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/pid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:text="TextView" />
<TextView
android:id="@+id/processName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
3)创建MyListAdapter.java作为自定ListAdapeter代码如下
package com.example.runningapps;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
class MyListAdapter extends BaseAdapter {
private ArrayList<RunningAppInfo> apps=null;
private Context context=null;
public MyListAdapter(ArrayList<RunningAppInfo> _apps,Context _context)
{
this.apps=_apps;
this.context=_context;
}
public int getCount() {
//设置绘制数量
return apps.size();
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView appIconImageView=null;
TextView appLabelTextView=null;
TextView pkgNameTextView=null;
TextView pidTextView=null;
TextView processNameTextView=null;
if (convertView == null || position < apps.size()) {
convertView = LayoutInflater.from(context).inflate(
R.layout.applist, null);
appIconImageView = (ImageView) convertView.findViewById(R.id.appIcon);
appLabelTextView = (TextView) convertView.findViewById(R.id.appLabel);
pkgNameTextView = (TextView) convertView.findViewById(R.id.pkgName);
pidTextView = (TextView) convertView.findViewById(R.id.pid);
processNameTextView = (TextView) convertView.findViewById(R.id.processName);
}
/*图标*/
appIconImageView.setImageDrawable(apps.get(position).GetAppIcon());
/*应用程序名称*/
appLabelTextView.setText(apps.get(position).GetAppLabel());
/*包名称*/
pkgNameTextView.setText(apps.get(position).GetPkgName());
/*进程id*/
pidTextView.setText(Integer.toString(apps.get(position).GetPid()));
/*进程名称*/
processNameTextView.setText(apps.get(position).GetProcessName());
return convertView;
}
}
4)修改MainActivity.java代码将自定ListAdapeter绑定给ListView代码如下
package com.example.runningapps;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
private PackageManager pm=null;
private ListView listview1=null;
private MyListAdapter adapter=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview1=(ListView)findViewById(R.id.listView1);
ArrayList<RunningAppInfo> runningApps=queryAllRunningAppInfo();
adapter=new MyListAdapter(runningApps, MainActivity.this);
listview1.setAdapter(adapter);
}
/* 查找正在运行的应用程序
* 1)获取系统安装的所有应用程序
* 2)根据报名pkgname过滤获取真正运行的应用程序
* */
private ArrayList<RunningAppInfo> queryAllRunningAppInfo(){
pm=this.getPackageManager();
ArrayList<RunningAppInfo> runningApps=new ArrayList<RunningAppInfo>();
//获取所有已经安装的应用程序
List<ApplicationInfo> installedApps=pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
//排序
Collections.sort(installedApps,new ApplicationInfo.DisplayNameComparator(pm));
//保存所有正在运行的包名以及它所在的进程信息
Map<String, RunningAppProcessInfo> pgkProcessAppMap=new HashMap<String, ActivityManager.RunningAppProcessInfo>();
//通过调用ActivityManager的getRunningAppProcesses()方法获得系统里所有正在运行的进程
ActivityManager mActivityManager=(ActivityManager)getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningAppProcesses=mActivityManager.getRunningAppProcesses();
for (RunningAppProcessInfo runningAppProcess : runningAppProcesses) {
//获得正在运行在该进程里的所有应用程序包
String[] pkgNameList=runningAppProcess.pkgList;
for (String pkgName : pkgNameList) {
//将所有的应用程序包添加到map对象里
pgkProcessAppMap.put(pkgName, runningAppProcess);
}
}
//遍历安装了应用程序
for (ApplicationInfo installedApp : installedApps) {
if(pgkProcessAppMap.containsKey(installedApp.packageName)){//安装程序正在运行
if((installedApp.flags&installedApp.FLAG_SYSTEM)==0){//非系统应用程序
RunningAppInfo item=new RunningAppInfo();
item.SetPid(pgkProcessAppMap.get(installedApp.packageName).pid);
item.SetProcessName(pgkProcessAppMap.get(installedApp.packageName).processName);
item.SetAppLabel((String)installedApp.loadLabel(pm));
item.SetAppIcon(installedApp.loadIcon(pm));
item.SetPkgName(installedApp.packageName);
runningApps.add(item);
}
}
}
return runningApps;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
相关文章推荐
- android应用完全退出,并且从最近运行程序以及系统正在运行列表中完全抹除。
- 获取Android系统中全部正在运行的程序
- Android中获得正在运行的程序和系统服务的方法
- Android程序运行时权限与文件系统权限的区别
- 在Android中获取系统正在运行的进程方法
- 正在运行的android程序,按home键之后退回到桌面,再次点击程序图标避免再次重新启动程序解决办法
- Android程序运行时权限与文件系统权限的区别
- Android获得手机所有应用(非系统)、正在运行的Service和Activity
- Android TASK 和 PROCESS的区别,获取最近运行的程序和正在运行的进程
- 在Linux编写程序arm程序,通过usb烧写在Android系统中安装和运行C程序
- Android中TASK和PROCESS的区别,获取最近运行的程序和正在运行的进
- 编译一个可以在Android系统上运行的可执行程序
- (OK) 在Android系统上运行C/C++程序
- Android点击通知栏信息后返回正在运行的程序,而不是一个新Activity
- 刚刚写了个小程序,显示系统中正在运行的进程及进程中模块的
- Android中TASK和PROCESS的区别,获取最近运行的程序和正在运行的进程
- 正在运行的android程序,按home键之后退回到桌面,在次点击程序图标避免再次重新启动程序解决办法
- android服务里生成通知点击后返回正在运行的程序和当前的Activity
- 正在运行的android程序,按home键之后退回到桌面,在次点击程序图标避免再次重新启动程序解决办法
- 正在运行的android程序,按home键之后退回到桌面,在次点击程序图标避免再次重新启动程序解决办法