简单实现Android获取已安装APP清单列表显示

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<PackageInfo> installedPackages = pm.getInstalledPackages(0); // 获取所以已安装的包
ArrayList<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>();
ArrayList<AppInfo> list = new ArrayList<AppInfo>();
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<String, Object> map = new HashMap<String, Object>();
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布局文件代码:
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout android:id=”@+id/LinearLayout01″
android:layout_width=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_height=”wrap_content”>
<ImageView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@mipmap/ic_launcher”
android:layout_alignParentLeft=”true”
android:id=”@+id/ItemImage” />
<TextView
android:layout_height=”wrap_content”
android:textSize=”20dip”
android:layout_width=”wrap_content”
android:layout_alignParentRight=”true”
android:text=”123″
android:id=”@+id/ItemTitle” />

</RelativeLayout>

评论:

le0102:请问有没有完整demo
le0102回复ovejur:谢谢,已经尝试过了,可行
ovejur回复:你可以直接拷贝到你的项目中运行即可
afrw4232:请问是用什么开发工具来实现?
ovejur回复:AS
qq_43050248:可以用