package com.example.netfairy.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.AlertDialog;
import java.util.List;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
public class MainActivity extends AppCompatActivity {
PackageManager packageManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
packageManager = getPackageManager();
List list = packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
StringBuilder stringBuilder = new StringBuilder();
for (PackageInfo packageInfo : list) {
if(!isSystemApplication(packageInfo.packageName)) {
stringBuilder.append("包名:" + packageInfo.packageName + "\n");
stringBuilder.append("版本:" + packageInfo.versionName + "\n");
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
stringBuilder.append("应用名称:" + applicationInfo.loadLabel(packageManager) + "\n");
stringBuilder.append("权限:" + ListPermissionInfo(packageInfo.packageName) + "\n");
stringBuilder.append("\n\n\n");
}
}
new AlertDialog.Builder(MainActivity.this)
.setTitle("APP信息")
.setMessage(stringBuilder) //调用
.setPositiveButton("关闭", null)
.show();
}
public boolean isSystemApplication(String packageName){
try {
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_CONFIGURATIONS);
if((packageInfo.applicationInfo.flags & android.content.pm.ApplicationInfo.FLAG_SYSTEM)!=0){
return true;
}
}
catch (NameNotFoundException e) {
e.printStackTrace();
}
return false;
}
public String ListPermissionInfo(String packageName) {
PackageInfo pi;
StringBuilder s = new StringBuilder();
try {
pi = packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
String[] permissions = pi.requestedPermissions;
if (permissions != null) {
for (String str : permissions) {
s.append("\n" + str);
}
}
}
catch (NameNotFoundException e) {
e.printStackTrace();
}
return s.toString();
}
}