AppInfo
MainActivity.java
package com.example.a10223567.appinfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity{
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.list_item);
setAdapter(getAllapps(MainActivity.
this,
0));
PackageInfo info =
this.getPackageManager().getPackageArchiveInfo(
"/sdcard/app-release.apk", PackageManager.GET_ACTIVITIES);
ApplicationInfo appInfo =
null;
if (info !=
null) {
appInfo = info.applicationInfo;
Log.d(
"fuck",
"packageName--->"+ appInfo.packageName);
}
Button systemApp = (Button) findViewById(R.id.button_system);
Button nonsystemApp = (Button) findViewById(R.id.button_nonsystem);
systemApp.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
setAdapter(getAllapps(MainActivity.
this,
1));
}
});
nonsystemApp.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
setAdapter(getAllapps(MainActivity.
this,
2));
}
});
}
public void setAdapter(List<AppDetail> list){
AppInfoAdapter adapter =
new AppInfoAdapter(
this, R.layout.app_item, list);
lv.setAdapter(adapter);
}
private String
getVersionName(String packageName) {
PackageManager pm = getPackageManager();
PackageInfo packInfo =
null;
try {
packInfo = pm.getPackageInfo(packageName,
0);
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
String version = packInfo.versionName +
" Code: " + packInfo.versionCode;
return version !=
null ? version :
"未获取到系统版本号";
}
public Boolean
isSystemApp(Context context,ResolveInfo resolveInfo){
PackageInfo packInfo =
null;
String packageName = resolveInfo.activityInfo.packageName;
try {
packInfo = context.getPackageManager().getPackageInfo(packageName,
0);
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if ((packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) ==
0){
return false;
}
else {
return true;
}
}
public List<AppDetail>
getAllapps(Context context,
int type){
List<AppDetail> apps =
new ArrayList<>();
PackageManager pm = context.getPackageManager();
Intent mainIntent =
new Intent(Intent.ACTION_MAIN,
null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(mainIntent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
for (
int i =
0; i < resolveInfos.size(); i++){
AppDetail temp =
new AppDetail();
ResolveInfo resolveInfo = resolveInfos.get(i);
temp.setAppIcon(resolveInfo.loadIcon(pm));
temp.setAppName(resolveInfo.loadLabel(pm).toString());
temp.setPackageName(resolveInfo.activityInfo.packageName);
temp.setVersionName(getVersionName(resolveInfo.activityInfo.packageName));
switch(type){
case 1:
if (isSystemApp(
this, resolveInfo)){
apps.add(temp);
}
break;
case 2:
if (!isSystemApp(
this, resolveInfo)){
apps.add(temp);
}
break;
default:
apps.add(temp);
}
}
return apps;
}
}
AppInfoAdapter
package com.example.a10223567.appinfo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by 10223567 on 2017/8/3.
*/
public class AppInfoAdapter extends ArrayAdapter<AppDetail>{
private int resourceId;
public AppInfoAdapter(Context context,
int textViewResourceId, List<AppDetail> objects){
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
@Override
public View
getView(
int position, View convertView, ViewGroup parent){
AppDetail appDetail = getItem(position);
View view;
ViewHolder viewHolder;
if (convertView ==
null){
view = LayoutInflater.from(getContext()).inflate(resourceId, parent,
false);
viewHolder =
new ViewHolder();
viewHolder.appIcon = (ImageView) view.findViewById(R.id.app_icon);
viewHolder.appName = (TextView) view.findViewById(R.id.app_name);
view.setTag(viewHolder);
}
else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.appIcon.setImageDrawable(appDetail.getAppIcon());
viewHolder.appName.setText(appDetail.getAppName() +
"\n"+ appDetail.getPackageName() +
"\n"+
appDetail.getVersionName() +
"\n"+ appDetail.getAppPath());
return view;
}
class ViewHolder{
ImageView appIcon;
TextView appName;
}
}
AppDetail
package com.example.a10223567.appinfo;
import android.graphics.drawable.Drawable;
/**
* Created by 10223567 on 2017/8/3.
*/
public class AppDetail {
private Drawable appIcon;
private String appName;
private String packageName;
private String versionName;
private String appPath;
public String
getAppName(){
return appName;
}
public void setAppName(String appName){
this.appName = appName;
}
public Drawable
getAppIcon(){
return appIcon;
}
public void setAppIcon(Drawable appIcon){
this.appIcon = appIcon;
}
public void setVersionName(String versionName){
this.versionName = versionName;
}
public String
getVersionName(){
return versionName;
}
public void setPackageName(String packageName){
this.packageName = packageName;
}
public String
getPackageName(){
return packageName;
}
public void setAppPath(String appPath){
this.appPath = appPath;
}
public String
getAppPath() {
return appPath;
}
}
app_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/app_icon"
android:scaleType="centerInside"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:padding="2dp"
/>
<TextView
android:id="@+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:lineSpacingExtra="2dp"
/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_system"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="系统应用"
android:layout_weight="1"
/>
<Button
android:id="@+id/button_nonsystem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第三方应用"
android:layout_weight="1"
/>
</LinearLayout>
<ListView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
</LinearLayout>