获取已安装的APP
/**
* 获取所有非系统应用
* 可以把代码中的判断去掉,获取所有的APP
*/
private void getAllApps() {
SLog.Console("获取所有非系统应用");
String result = "";
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) {
if (TextUtils.isEmpty(result)) {
result = pak.applicationInfo.loadLabel(pManager).toString();
} else {
result = result + "," + pak.applicationInfo.loadLabel(pManager).toString();
}
}
}
SLog.Console("设备安装非系统应用信息:" + result);
}
获取正在显示的APP
public void getCurrentDisplayAPPInfo() {
SLog.Console("当前界面显示")
try {
ActivityManager mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE)
List<ActivityManager.RunningAppProcessInfo> list = mActivityManager.getRunningAppProcesses()
String mPackageName = ""
if (Build.VERSION.SDK_INT > 20) {
mPackageName = getTopActivty()
} else {
mPackageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName()
}
if (!TextUtils.isEmpty(mPackageName)) {
SLog.Console("当前界面显示的App:" + mPackageName)
}
} catch (Exception e) {
e.printStackTrace()
}
}