书接上文: https://blog.csdn.net/qq_23025319/article/details/13473979
上篇文章介绍了,通过pacakeManager获取packageInfo,从而获取应用签名数据.
本文接着讲解packageinfo以及applicationinfo获取到应用的其他数据.
具体代码如下:
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的布局如下:
<?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>