Android获取应用信息

Android获取应用信息

书接上文: https://blog.csdn.net/qq_23025319/article/details/13473979

上篇文章介绍了,通过pacakeManager获取packageInfo,从而获取应用签名数据.

本文接着讲解packageinfo以及applicationinfo获取到应用的其他数据.

具体代码如下:

java 复制代码
public class DetailsActivity extends Activity {
    private static final String TAG = "DetailsActivity";
    private String packageName;
    private PackageManager packageManager;
    private ImageView ivIcon;
    private TextView tvLabel, tvPackage, tvVersionCode,
            tvVersionName, tvSign, tvFirst, tvLast, tvPermission,
            tvMin, tvTarget, tvDataDir, tvPath, tvSize,tv_other,
    tvActivity,tvService,tvProviders;

    public static void launcher(Context context, String packageName) {
        Intent intent = new Intent(context, DetailsActivity.class);
        intent.putExtra("packageName", packageName);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        packageName = getIntent().getStringExtra("packageName");
        if (TextUtils.isEmpty(packageName)) {
            Toast.makeText(this, "包名为空", Toast.LENGTH_LONG).show();
            finish();
        }
        packageManager = getPackageManager();
        if (packageManager == null) {
            Toast.makeText(this, "packageManager为空", Toast.LENGTH_LONG).show();
            finish();
        }
        initView();
        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
            PackageInfo packageInfo = packageManager.getPackageInfo(applicationInfo.packageName, PackageManager.GET_SIGNATURES);
            ivIcon.setImageDrawable(applicationInfo.loadIcon(packageManager));
            tvPackage.setText(packageName);
            tvLabel.setText(applicationInfo.loadLabel(packageManager));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                tvMin.setText("minSdkVersion:" + applicationInfo.minSdkVersion);
            }
            tvTarget.setText("targetSdkVersion:" + applicationInfo.targetSdkVersion);
            tvDataDir.setText("dataDir:" + applicationInfo.dataDir);
            tvPath.setText("sourceDir:" + applicationInfo.sourceDir);
            if (!TextUtils.isEmpty(applicationInfo.sourceDir)) {
                File file = new File(applicationInfo.sourceDir);
                tvSize.setText("apkSize:" + file.length());
            } else {
                tvSize.setText("apkSize:none");
            }
            StringBuilder other = new StringBuilder();
            other.append("processName:"+applicationInfo.processName+"\n");
            other.append("backupAgentName:"+applicationInfo.backupAgentName+"\n");
            other.append("className:"+applicationInfo.className+"\n");
            other.append("uid:"+applicationInfo.uid);
            tv_other.setText(other.toString());
            Signature[] signs = packageInfo.signatures;
            StringBuilder stringBuilder = new StringBuilder();
            for (Signature sig : signs) {
                String sha1 = ShaUtils.getSHA1(sig.toByteArray());
                stringBuilder.append("Signature: " + sha1 + "\n");
                Log.d("Signature", "packageName = " + packageInfo.packageName + ", signature = " + sha1);
            }
            tvSign.setText(stringBuilder.toString());
            tvFirst.setText("firstInstallTime:" + timeFormat(packageInfo.firstInstallTime));
            tvLast.setText("lastUpdateTime:" + timeFormat(packageInfo.lastUpdateTime));
            tvVersionCode.setText("versionCode:" + packageInfo.versionCode);
            tvVersionName.setText("versionName:" + packageInfo.versionName);
            String[] permissions = packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS).requestedPermissions;
            if (permissions != null) {
                StringBuilder strPermission = new StringBuilder();
                for (String permission : permissions) {
                    strPermission.append(permission + "\n");
                }
                tvPermission.setText(strPermission.toString());
            } else {
                tvPermission.setText("未申请权限");
            }
            ActivityInfo[] activities = packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES).activities;
            if (activities != null) {
                StringBuilder strAct = new StringBuilder();
                for (ActivityInfo act : activities) {
                    strAct.append(act.name + "\n");
                }
                tvActivity.setText(strAct.toString());
            } else {
                tvActivity.setText("未获取到activities");
            }
            ProviderInfo[] providers = packageManager.getPackageInfo(packageName, PackageManager.GET_PROVIDERS).providers;
            if (providers != null) {
                StringBuilder strProvider = new StringBuilder();
                for (ProviderInfo providerInfo : providers) {
                    strProvider.append(providerInfo.authority + "\n");
                }
                tvProviders.setText(strProvider.toString());
            } else {
                tvProviders.setText("未获取到providers");
            }
            ServiceInfo[] services = packageManager.getPackageInfo(packageName, PackageManager.GET_SERVICES).services;
            if (services != null) {
                StringBuilder strService = new StringBuilder();
                for (ServiceInfo act : services) {
                    strService.append(act.name + "\n");
                }
                tvService.setText(strService.toString());
            } else {
                tvService.setText("未获取到services");
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        ivIcon = findViewById(R.id.iv_icon);
        tvFirst = findViewById(R.id.tv_first_install);
        tvLast = findViewById(R.id.tv_last_install);
        tvLabel = findViewById(R.id.tv_name);
        tvPackage = findViewById(R.id.tv_package);
        tvSign = findViewById(R.id.tv_sign);
        tvVersionCode = findViewById(R.id.tv_version_code);
        tvVersionName = findViewById(R.id.tv_version_name);
        tvPermission = findViewById(R.id.tv_permission);
        tvMin = findViewById(R.id.tv_min);
        tvTarget = findViewById(R.id.tv_target);
        tvDataDir = findViewById(R.id.tv_datadir);
        tvPath = findViewById(R.id.tv_path);
        tvSize = findViewById(R.id.tv_apksize);
        tv_other =findViewById(R.id.tv_other);
        tvActivity =findViewById(R.id.tv_activity);
        tvProviders =findViewById(R.id.tv_provider);
        tvService =findViewById(R.id.tv_service);

    }

    public static String timeFormat(long currentTime) {
        Date date = new Date(currentTime); // 将long类型转成Date类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 设置要转换的时间格式
        String strTime = sdf.format(date); // 将Date类型转成String类型
        return strTime;
    }
}

details的布局如下:

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DetailsActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="100dp">
                <ImageView
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:id="@+id/iv_icon"
                    android:layout_margin="10dp"
                    />
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/iv_icon"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="10dp"
                    >
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="16sp"
                        android:text="xxx"
                        android:layout_alignParentTop="true"
                        android:textColor="@color/black"
                        android:id="@+id/tv_name"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="14sp"
                        android:text="xxx"
                        android:layout_below="@+id/tv_name"
                        android:textColor="@color/black"
                        android:id="@+id/tv_package"
                        />
                </RelativeLayout>
            </RelativeLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="15dp"
                android:layout_marginLeft="15dp"
                android:background="#330000"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#ff0000"
                android:textSize="14sp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:text="xxxxxxxxx"
                android:id="@+id/tv_sign"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:textSize="16sp"
                android:text="versionCode"
                android:id="@+id/tv_version_code"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:textSize="16sp"
                android:text="versionName"
                android:id="@+id/tv_version_name"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:textSize="16sp"
                android:text="min"
                android:id="@+id/tv_min"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:textSize="16sp"
                android:text="target"
                android:id="@+id/tv_target"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:textSize="16sp"
                android:text="datadir"
                android:id="@+id/tv_datadir"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:textSize="16sp"
                android:text="path"
                android:id="@+id/tv_path"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:textSize="16sp"
                android:text="size"
                android:id="@+id/tv_apksize"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:textSize="16sp"
                android:text="firstInstall"
                android:id="@+id/tv_first_install"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:textSize="16sp"
                android:text="lastInstall"
                android:id="@+id/tv_last_install"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:textSize="16sp"
                android:text=""
                android:id="@+id/tv_other"
                />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="15dp"
                android:layout_marginLeft="15dp"
                android:background="#330000"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textColor="@color/black"
                android:textStyle="bold"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="10dp"
                android:text="应用权限:"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="10dp"
                android:text="xxx:"
                android:id="@+id/tv_permission"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textColor="@color/black"
                android:textStyle="bold"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="10dp"
                android:text="Activities:"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="10dp"
                android:text="xxx:"
                android:id="@+id/tv_activity"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textColor="@color/black"
                android:textStyle="bold"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="10dp"
                android:text="Providers:"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="10dp"
                android:text="xxx:"
                android:id="@+id/tv_provider"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textColor="@color/black"
                android:textStyle="bold"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="10dp"
                android:text="Services:"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="10dp"
                android:text="xxx:"
                android:id="@+id/tv_service"
                />
        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

效果图如下:

相关推荐
隐擎fox1 小时前
深入 Android 移动端风控与协议层对抗:内核级虚拟环境检测、网络堆栈特征伪装与住宅 IP 协同实战
android·网络·python·网络协议·tcp/ip
换元不配限3 小时前
从 AsyncTask 到现代并发:Android 异步方案的演进与替代实践
android·asynctask·异步编程·方案对比·asynctask替代方案
用户2018792831673 小时前
你的 App 为什么是「电量刺客」?——从一条 WakeLock 追到 Linux 内核的 Android 省电原理全解
android
mmsx3 小时前
Android 安全校验下沉到原生层:防 patch 与构建期自动化
android·安全·自动化
老牛日常随笔5 小时前
Google把Android测试搬进AI对话:ARTEMIS如何用自然语言操作你的手机
android·人工智能·智能手机
朱涛的自习室5 小时前
预算 1000 美金的开源软件,DeepSeek 只花 1 美金就做完了(Day 6)
android·前端·人工智能
mmsx6 小时前
Android APK 加固方案怎么落地:从加壳到校验的组合取舍
android·网络·安全·签名
hunterandroid6 小时前
Navigation Component 工程落地:从单 Activity 架构到深度链接配置
android·前端
撩得Android一次心动7 小时前
Jetpack Compose 知识点整理3【个人用】
android·笔记·学习·kotlin·android compose