Skip to content

Commit

Permalink
添加获取桌面应用的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
ctj committed Nov 30, 2023
1 parent 1ff3145 commit 1f98b68
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 13 deletions.
22 changes: 17 additions & 5 deletions app/src/main/java/com/wave_chtj/example/allapp/AllAppAty.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,31 @@ public void run() {
KLog.e(TAG, "errMeg:" + e.getMessage());
tvTotal.setText("总流量:计算异常");
}
newsAdapter.setList(AppsUtils.getAllApp(true));
newsAdapter.setList(AppsUtils.getAllApp());
}
});

}

/**
* 查询桌面应用
* 查询全部应用
*
* @param view
*/
public void getAllAppClick(View view) {
List<AppEntity> appEntityList = AppsUtils.getAllApp(true);
List<AppEntity> appEntityList = AppsUtils.getAllApp();
tvCount.setText("总数:" + appEntityList.size());
newsAdapter.setList(appEntityList);
}


/**
* 查询桌面应用
*
* @param view
*/
public void getDeskAppClick(View view) {
List<AppEntity> appEntityList = AppsUtils.getDeskTopAppList();
tvCount.setText("总数:" + appEntityList.size());
newsAdapter.setList(appEntityList);
}
Expand Down Expand Up @@ -154,7 +166,7 @@ public void rebootClick(View view) {
* @param view
*/
public void getNormalApp(View view) {
List<AppEntity> appEntityList = AppsUtils.getAllApp(true);
List<AppEntity> appEntityList = AppsUtils.getAllApp();
List<AppEntity> normalAppList=new ArrayList<>();
for (int i = 0; i < appEntityList.size(); i++) {
if (!appEntityList.get(i).isSystemApp){
Expand All @@ -171,7 +183,7 @@ public void getNormalApp(View view) {
* @param view
*/
public void getSystemApp(View view) {
List<AppEntity> appEntityList = AppsUtils.getAllApp(true);
List<AppEntity> appEntityList = AppsUtils.getAllApp();
List<AppEntity> systemAppList=new ArrayList<>();
for (int i = 0; i < appEntityList.size(); i++) {
if (appEntityList.get(i).isSystemApp){
Expand Down
18 changes: 15 additions & 3 deletions app/src/main/res/layout/activity_allapp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
android:layout_width="match_parent"
android:layout_height="50dp"
app:centerText="应用列表"
app:leftBack="true"
app:leftText="&lt;&lt;"
app:rightText="旋转"
app:leftBack="true"
app:textSize="24sp" />

<LinearLayout
Expand All @@ -35,9 +35,9 @@

<LinearLayout
android:layout_width="0dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:orientation="vertical">

Expand All @@ -52,6 +52,17 @@
android:text="全部应用"
android:textSize="@dimen/small_text_size" />

<Button
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:layout_weight="1"
android:background="@drawable/button_switch"
android:onClick="getDeskAppClick"
android:text="桌面应用"
android:textSize="@dimen/small_text_size" />

<Button
android:layout_width="match_parent"
android:layout_height="55dp"
Expand Down Expand Up @@ -84,6 +95,7 @@
android:onClick="enableAllAppNetClick"
android:text="启用全部网络"
android:textSize="@dimen/small_text_size" />

<Button
android:layout_width="match_parent"
android:layout_height="55dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,46 @@ public static String getAppPath(String pkgName) {
return commandResult.result == 0 ? commandResult.successMsg.replace("package:", "") : "null";
}

/**
* 查询桌面所有应用 包含包名下app名称,图标的明细信息list
*/
public static List<AppEntity> getDeskTopAppList() {
try {
List<AppEntity> appEntityList = new ArrayList<AppEntity>();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PackageManager pm = BaseIotUtils.getContext().getPackageManager();
List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0);
for (int i = 0; i < apps.size(); i++) {
ResolveInfo info = apps.get(i);
KLog.d("getDeskTopAppList() info >> " + info.activityInfo.toString());
String pkg = info.activityInfo.packageName;
Drawable icon = info.loadIcon(BaseIotUtils.getContext().getPackageManager());
ApplicationInfo ai = pm.getApplicationInfo(info.activityInfo.packageName, PackageManager.GET_ACTIVITIES);
CharSequence name = info.activityInfo.loadLabel(BaseIotUtils.getContext().getPackageManager());
boolean isSys = false;
if ((ai.flags & ai.FLAG_SYSTEM) != 0) {
isSys = true;
}
String topApp=getTopApp();
boolean isTopApp = ai.packageName.contains(topApp);
int vCode = pm.getPackageInfo(pkg, 0).versionCode;
String vName = pm.getPackageInfo(pkg, 0).versionName;
String sourceDir = ai.sourceDir;
AppEntity entity = new AppEntity(name.toString(),pkg,vCode,vName,icon,isTopApp,isAppRunning(pkg),isSys,false,getUidByPackageName(pkg),getPidByPackageName(pkg),sourceDir,getAllProcess(pkg),getRunService(pkg));
appEntityList.add(entity);
}
return appEntityList;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* 查询所有应用 包含包名下app名称,图标的明细信息list
*/
public static List<AppEntity> getAllApp(boolean needSysApp) {
public static List<AppEntity> getAllApp() {
PackageManager packageManager = BaseIotUtils.getContext().getPackageManager();
ActivityManager activityManager = (ActivityManager) BaseIotUtils.getContext().getSystemService(Context.ACTIVITY_SERVICE);
List<AppEntity> appList = new ArrayList<>();
Expand All @@ -74,9 +110,6 @@ public static List<AppEntity> getAllApp(boolean needSysApp) {
try {
PackageInfo packageInfo = packageManager.getPackageInfo(appInfo.packageName, 0);
boolean isSystemApp = (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
if (!needSysApp&&isSystemApp){
continue;
}
String appName = packageManager.getApplicationLabel(appInfo).toString();
String packageName = appInfo.packageName;
int versionCode = packageInfo.versionCode;
Expand All @@ -86,8 +119,8 @@ public static List<AppEntity> getAllApp(boolean needSysApp) {
int pid = getPid(appInfo.packageName,activityManager);
String sourceDir = appInfo.sourceDir;
String topApp=getTopApp();
boolean isRunning = isAppRunning(appInfo.packageName);
boolean isTopApp = appInfo.packageName.contains(topApp);
boolean isRunning = isAppRunning(appInfo.packageName);
AppEntity app = new AppEntity(appName, packageName, versionCode, versionName, icon, isTopApp, isRunning, isSystemApp,false, uid, pid, sourceDir,getAllProcess(appInfo.packageName), getRunService(appInfo.packageName));
appList.add(app);
} catch (Throwable e) {
Expand Down

0 comments on commit 1f98b68

Please sign in to comment.