package com.example.srct.service;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); //用来存储获取的应用信息数据
AppInfo appInfo=new AppInfo();
List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packages.size();i++) {
PackageInfo packageInfo = packages.get(i);
AppInfo tmpInfo =new AppInfo();
tmpInfo.appName = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
tmpInfo.packageName = packageInfo.packageName;
tmpInfo.versionName = packageInfo.versionName;
tmpInfo.versionCode = packageInfo.versionCode;
if((packageInfo.applicationInfo.flags& ApplicationInfo.FLAG_SYSTEM)==0)
{
appList.add(tmpInfo);//非系统应用
tmpInfo.print();
}
}
Log.v("app","-----------------------------------------------");
for(int i=0;i<packages.size();i++) {
PackageInfo packageInfo = packages.get(i);
AppInfo tmpInfo =new AppInfo();
tmpInfo.appName = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
tmpInfo.packageName = packageInfo.packageName;
tmpInfo.versionName = packageInfo.versionName;
tmpInfo.versionCode = packageInfo.versionCode;
if((packageInfo.applicationInfo.flags& ApplicationInfo.FLAG_SYSTEM)!=0)
{
appList.add(tmpInfo);//系统应用
tmpInfo.printSystem();
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////
package com.example.srct.service;
import android.graphics.drawable.Drawable;
import android.util.Log;
/**
* Created by srct on 2018/1/4.
*/
public class AppInfo {
public String appName="";
public String packageName="";
public String versionName="";
public int versionCode=0;
public void print()
{
Log.v("app","Name:"+appName+" Package:"+packageName);
Log.v("app","Name:"+appName+" versionName:"+versionName);
Log.v("app","Name:"+appName+" versionCode:"+versionCode);
}
public void printSystem()
{
Log.v("app","Name:"+appName+" Package:"+packageName);
Log.v("app","Name:"+appName+" versionName:"+versionName);
Log.v("app","Name:"+appName+" versionCode:"+versionCode);
}
}