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 bf1c90a commit 1ff3145
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
5 changes: 4 additions & 1 deletion app/src/main/java/com/wave_chtj/example/OptionAty.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.wave_chtj.example;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -100,7 +104,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
initData();
}


public void initView() {
rvinfo = findViewById(R.id.rvinfo);
itemAdapter = new IndexItemAdapter(dataList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public void onClick(View v) {
try {
AppsUtils.openPackage(list.get(posiNum).packageName);
} catch (Exception e) {
e.printStackTrace();
ToastUtils.error("打开错误!");
}
}
Expand All @@ -90,11 +89,10 @@ public void onClick(View v) {
ClipboardManager cm = (ClipboardManager) BaseIotUtils.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
StringBuilder sb = new StringBuilder();
String appPath=AppsUtils.getAppPath(pkgName);
sb.append("appName:" + list.get(posiNum).appName + "\n");
sb.append("packageName:" + list.get(posiNum).packageName + "\n");
sb.append("appPath:" + appPath+ "\n");
sb.append("apkVersion:" + list.get(posiNum).versionCode + "\n");
sb.append("pkgName:" + list.get(posiNum).packageName + "\n");
sb.append("appPath:" + list.get(posiNum).sourceDir+ "\n");
sb.append("version:" + list.get(posiNum).versionCode + "\n");
sb.append("isSys:" + list.get(posiNum).isSystemApp);
// 将ClipData内容放到系统剪贴板里。
ClipData mClipData = ClipData.newPlainText("Label", sb.toString());
Expand Down
8 changes: 4 additions & 4 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,7 +102,7 @@ public void run() {
KLog.e(TAG, "errMeg:" + e.getMessage());
tvTotal.setText("总流量:计算异常");
}
newsAdapter.setList(AppsUtils.getAllApp());
newsAdapter.setList(AppsUtils.getAllApp(true));
}
});

Expand All @@ -114,7 +114,7 @@ public void run() {
* @param view
*/
public void getAllAppClick(View view) {
List<AppEntity> appEntityList = AppsUtils.getAllApp();
List<AppEntity> appEntityList = AppsUtils.getAllApp(true);
tvCount.setText("总数:" + appEntityList.size());
newsAdapter.setList(appEntityList);
}
Expand Down Expand Up @@ -154,7 +154,7 @@ public void rebootClick(View view) {
* @param view
*/
public void getNormalApp(View view) {
List<AppEntity> appEntityList = AppsUtils.getAllApp();
List<AppEntity> appEntityList = AppsUtils.getAllApp(true);
List<AppEntity> normalAppList=new ArrayList<>();
for (int i = 0; i < appEntityList.size(); i++) {
if (!appEntityList.get(i).isSystemApp){
Expand All @@ -171,7 +171,7 @@ public void getNormalApp(View view) {
* @param view
*/
public void getSystemApp(View view) {
List<AppEntity> appEntityList = AppsUtils.getAllApp();
List<AppEntity> appEntityList = AppsUtils.getAllApp(true);
List<AppEntity> systemAppList=new ArrayList<>();
for (int i = 0; i < appEntityList.size(); i++) {
if (appEntityList.get(i).isSystemApp){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
Expand All @@ -32,14 +33,18 @@
import com.wave_chtj.example.util.PACKAGES;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
@Route(path = PACKAGES.BASE+"serialport")

@Route(path = PACKAGES.BASE + "serialport")
public class SerialPortAty extends BaseActivity implements CompoundButton.OnCheckedChangeListener {
private static final String TAG = "SerialPortAty";
@BindView(R.id.sp_com)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @author chtj
* create by chtj on 2019-8-6
* desc:AppsUtils相关工具类
* --查询桌面所有应用 {@link #getAllApp()} ()}
* --查询桌面所有应用 {@link #getAllApp(boolean)} ()}
* --获取当前应用名称 {@link #getAppName(String packageName)}
* --根据包名获取进程PID {@link #getPidByPackageName(String packagename)}
* --获取APP-VersionCode {@link #getAppVersionCode()}
Expand Down Expand Up @@ -65,26 +65,29 @@ public static String getAppPath(String pkgName) {
/**
* 查询所有应用 包含包名下app名称,图标的明细信息list
*/
public static List<AppEntity> getAllApp() {
public static List<AppEntity> getAllApp(boolean needSysApp) {
PackageManager packageManager = BaseIotUtils.getContext().getPackageManager();
ActivityManager activityManager = (ActivityManager) BaseIotUtils.getContext().getSystemService(Context.ACTIVITY_SERVICE);
List<AppEntity> appList = new ArrayList<>();
List<ApplicationInfo> installedApps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo appInfo : installedApps) {
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;
String versionName = packageInfo.versionName;
Drawable icon = packageManager.getApplicationIcon(appInfo);
String topApp=getTopApp();
boolean isTopApp = appInfo.packageName.contains(topApp);
boolean isRunning = isAppRunning(appInfo.packageName);
boolean isSystemApp = (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
int uid = appInfo.uid;
int pid = getPid(appInfo.packageName,activityManager);
String sourceDir = appInfo.sourceDir;
String topApp=getTopApp();
boolean isRunning = isAppRunning(appInfo.packageName);
boolean isTopApp = appInfo.packageName.contains(topApp);
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 1ff3145

Please sign in to comment.