activity代码:
import android.app.ListActivity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.SimpleAdapter;
import com.mob.at.demo.util.AppInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class AppListActivity extends ListActivity {
private SimpleAdapter listItemAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_applist);
initListView();
this.setListAdapter(listItemAdapter);
}
private void initListView() {
PackageManager pm = getPackageManager();
List installedPackages = pm.getInstalledPackages(0); // 获取所以已安装的包
ArrayList> listItems = new ArrayList>();
ArrayList list = new ArrayList();
for (PackageInfo packageInfo : installedPackages) {
AppInfo info = new AppInfo();
String packageName = packageInfo.packageName; // 包名
ApplicationInfo applicationInfo = packageInfo.applicationInfo; // 应用信息
String name = applicationInfo.loadLabel(pm).toString(); // 应用名称
Drawable icon = applicationInfo.loadIcon(pm); // 应用图标
System.out.println("name=========="+name);
System.out.println("packageName=========="+packageName);
info.name = name;
info.packageName = packageName;
info.icon = icon;
// 状态机,通过01状态来表示是否具备某些属性和功能
int flags = applicationInfo.flags; // 获取应用标记
if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo
.FLAG_EXTERNAL_STORAGE) {
//安装在sdcard
info.isRom = false;
} else {
//安装在手机
info.isRom = true;
}
if ((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo
.FLAG_SYSTEM) {
//系统应用
info.isUser = false;
} else {
//用户应用
info.isUser = true;
}
if (info.isUser) {
HashMap map = new HashMap();
map.put("ItemTitle", name); //文字
map.put("ItemImage", icon); //图片
listItems.add(map);
}
}
System.out.println("listItems=========="+listItems.size());
listItemAdapter = new SimpleAdapter(this, listItems,
R.layout.list_item,
new String[] {"ItemTitle", "ItemImage"},
new int[ ] {R.id.ItemTitle, R.id.ItemImage}
);
}
}
list_item布局文件代码:
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_alignParentLeft="true"
android:id="@+id/ItemImage" />
android:layout_height="wrap_content"
android:textSize="20dip"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:text="123"
android:id="@+id/ItemTitle" />
标签:info,name,APP,new,清单,import,Android,android,pm
来源: https://blog.csdn.net/ovejur/article/details/99732360