1.创建工具类
- public class AppInfo {
- public String appName = "";
- public String packageName = "";
- public String versionName = "";
- public int versionCode = 0;
- public Drawable appIcon = null;
- public Context context;
- public AppInfo(Context context) {
- this.context = context;
- }
- /**
- * 获取非系统应用
- */
- public ArrayList<AppInfo> getAppInfo() {
- ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); // 用来存储获取的应用信息数据
- List<PackageInfo> packages = context.getPackageManager()
- .getInstalledPackages(0);
- for (int i = 0; i < packages.size(); i++) {
- PackageInfo packageInfo = packages.get(i);
- if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {// 非系统应用
- AppInfo tmpInfo = new AppInfo(context);
- tmpInfo.appName = packageInfo.applicationInfo.loadLabel(
- context.getPackageManager()).toString();
- tmpInfo.packageName = packageInfo.packageName;
- tmpInfo.versionName = packageInfo.versionName;
- tmpInfo.versionCode = packageInfo.versionCode;
- tmpInfo.appIcon = packageInfo.applicationInfo.loadIcon(context
- .getPackageManager());
- appList.add(tmpInfo);
- } else {// 获取系统应用
- }
- }// 好啦 这下手机上安装的应用数据都存在appList里了。
- return appList;
- }
- }
==================工具类结束,下面只需要在Activity 中调用此方法就可以了=======
2.在Activity中显示
- import java.util.ArrayList;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- import com.pubukeji.www.chonghuafei.appinfo.AppInfo;
- public class InfoActivity extends Activity {
- TextView textView;
- ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); // 用来存储获取的应用信息数据
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.info_activity);
- AppInfo info = new AppInfo(this);
- appList = info.getAppInfo();
- StringBuffer buffer = new StringBuffer();
- for (int i = 0; i < appList.size(); i++) {
- buffer.append(appList.get(i).packageName + "\n");
- }
- textView = (TextView) findViewById(R.id.textView1);
- textView.setText(buffer);
- }
- }
结果如图所示