0

Android获得手机所有应用(非系统)、正在运行的Service和Activity

Android中获得系统运行的进程、获得当前正在运行的service、 获得当前正在运行的activity、 获得最近运行的应用、获取手机内所有应用(非系统应用)

直接上代码,布局文件就是一个TextView

直接创建Activity后删除Activity中所有内容,复制下面代码直接就能运行

private TextView txtDes = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 强制为横屏
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtDes = (TextView) findViewById(R.id.textdes);

}

@Override
protected void onResume() {
super.onResume();
Log.w(“sss”, “onResume”);
StringBuffer str = new StringBuffer();
List<PackageInfo> listData = getAllApps(this);
str.append(“应用数量:” + listData.size());
str.append(“\n”);
for (PackageInfo packageInfo : listData) {
str.append(“” + packageInfo.packageName);
str.append(“\n”);
str.append(“”
+ packageInfo.applicationInfo
.loadLabel(getPackageManager()));
str.append(“\n”);
ActivityInfo[] list;
try {
list = getPackageManager().getPackageInfo(
packageInfo.packageName, PackageManager.GET_ACTIVITIES).activities;
if (list != null) {
str.append(“Activity数量:” + list.length);
str.append(“\n”);
for (ActivityInfo activityInfo : list) {
str.append(activityInfo.packageName);
str.append(“\n”);
str.append(activityInfo.name);
str.append(“\n”);
str.append(“activityInfo.screenOrientation=”
+ activityInfo.screenOrientation);
str.append(“\n”);
str.append(“activityInfo.configChanges=”
+ activityInfo.configChanges);
str.append(“\n”);
}
}
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
txtDes.setText(str);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
StringBuffer str = new StringBuffer();
List<PackageInfo> listData = getAllApps(this);
str.append(“应用数量:” + listData.size());
str.append(“\n”);
for (PackageInfo packageInfo : listData) {
str.append(“” + packageInfo.packageName);
str.append(“\n”);
str.append(“”
+ packageInfo.applicationInfo
.loadLabel(getPackageManager()));
str.append(“\n”);
ActivityInfo[] list;
try {
list = getPackageManager().getPackageInfo(
packageInfo.packageName, PackageManager.GET_ACTIVITIES).activities;
if (list != null) {
str.append(“Activity数量:” + list.length);
str.append(“\n”);
for (ActivityInfo activityInfo : list) {
str.append(activityInfo.packageName);
str.append(“\n”);
str.append(activityInfo.name);
str.append(“\n”);
str.append(“activityInfo.screenOrientation=”
+ activityInfo.screenOrientation);
str.append(“\n”);
str.append(“activityInfo.configChanges=”
+ activityInfo.configChanges);
str.append(“\n”);
}
}
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
txtDes.setText(str);
}

/**
* 查询手机内非系统应用
*
* @param context
* @return
*/
public List<PackageInfo> getAllApps(Context context) {
List<PackageInfo> apps = new ArrayList<PackageInfo>();
PackageManager pManager = context.getPackageManager();
// 获取手机内所有应用
List<PackageInfo> paklist = pManager.getInstalledPackages(0);
for (int i = 0; i < paklist.size(); i++) {
PackageInfo pak = (PackageInfo) paklist.get(i);
// 判断是否为非系统预装的应用程序
if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
// customs applications
apps.add(pak);
}
}
return apps;
}

private void getActivityManagerMM() {
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

// 获得系统运行的进程
List<ActivityManager.RunningAppProcessInfo> appList1 = mActivityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo running : appList1) {
System.out.println(running.processName);
}
System.out.println(“================”);

// 获得当前正在运行的service
List<ActivityManager.RunningServiceInfo> appList2 = mActivityManager
.getRunningServices(100);
for (ActivityManager.RunningServiceInfo running : appList2) {
System.out.println(running.service.getClassName());
}

System.out.println(“================”);

// 获得当前正在运行的activity
List<ActivityManager.RunningTaskInfo> appList3 = mActivityManager
.getRunningTasks(1000);
for (ActivityManager.RunningTaskInfo running : appList3) {
System.out.println(running.baseActivity.getClassName());
}
System.out.println(“================”);

// 获得最近运行的应用
List<ActivityManager.RecentTaskInfo> appList4 = mActivityManager
.getRecentTasks(100, 1);
for (ActivityManager.RecentTaskInfo running : appList4) {
System.out.println(running.origActivity.getClassName());
}
}

 

转自请注明转载地址:http://blog.etongwl.com/?p=426 

天边的星星