PackageManager是个非常好的东西,其他的详细的细节等日后有时间整理
PackageManager的功能:
•安装,卸载应用
•查询permission相关信息
•查询Application相关信息(application,activity,receiver,service,provider及相应属性等)
•查询已安装应用
•增加,删除permission
•清除用户数据、缓存,代码段等
我们可以用PackageManager来显示系统安装的应用程序列表或者系统程序列表
废话先不多说
java code
package com.loulijun.appshow;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class AppShowActivity extends Activity {
ListView lv;
MyAdapter adapter;
ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView)findViewById(R.id.lv);
//得到PackageManager对象
PackageManager pm = getPackageManager();
//得到系统安装的所有程序包的PackageInfo对象
//List<ApplicationInfo> packs = pm.getInstalledApplications(0);
List<PackageInfo> packs = pm.getInstalledPackages(0);
for(PackageInfo pi:packs)
{
HashMap<String, Object> map = new HashMap<String, Object>();
//显示用户安装的应用程序,而不显示系统程序
// if((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0&&
// (pi.applicationInfo.flags&ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)==0)
// {
// //这将会显示所有安装的应用程序,包括系统应用程序
// map.put("icon", pi.applicationInfo.loadIcon(pm));//图标
// map.put("appName", pi.applicationInfo.loadLabel(pm));//应用程序名称
// map.put("packageName", pi.applicationInfo.packageName);//应用程序包名
// //循环读取并存到HashMap中,再增加到ArrayList上,一个HashMap就是一项
// items.add(map);
// }
//这将会显示所有安装的应用程序,包括系统应用程序
map.put("icon", pi.applicationInfo.loadIcon(pm));//图标
map.put("appName", pi.applicationInfo.loadLabel(pm));//应用程序名称
map.put("packageName", pi.applicationInfo.packageName);//应用程序包名
//循环读取并存到HashMap中,再增加到ArrayList上,一个HashMap就是一项
items.add(map);
}
/**
* 参数:Context
* ArrayList(item的集合)
* item的layout
* 包含ArrayList中的HashMap的key的数组
* key所对应的值的相应的控件id
*/
adapter = new MyAdapter(this, items, R.layout.piitem,
new String[]{"icon", "appName", "packageName"},
new int[]{R.id.icon, R.id.appName, R.id.packageName});
lv.setAdapter(adapter);
}
}
class MyAdapter extends SimpleAdapter
{
private int[] appTo;
private String[] appFrom;
private ViewBinder appViewBinder;
private List<? extends Map<String, ?>> appData;
private int appResource;
private LayoutInflater appInflater;
public MyAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
appData = data;
appResource = resource;
appFrom = from;
appTo = to;
appInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent)
{
return createViewFromResource(position, convertView, parent, appResource);
}
private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource)
{
View v;
if(convertView == null)
{
v = appInflater.inflate(resource, parent,false);
final int[] to = appTo;
final int count = to.length;
final View[] holder = new View[count];
for(int i = 0; i < count; i++)
{
holder[i] = v.findViewById(to[i]);
}
v.setTag(holder);
}else
{
v = convertView;
}
bindView(position, v);
return v;
}
private void bindView(int position, View view)
{
final Map dataSet = appData.get(position);
if(dataSet == null)
{
return;
}
final ViewBinder binder = appViewBinder;
final View[] holder = (View[])view.getTag();
final String[] from = appFrom;
final int[] to = appTo;
final int count = to.length;
for(int i = 0; i < count; i++)
{
final View v = holder[i];
if(v != null)
{
final Object data = dataSet.get(from[i]);
String text = data == null ? "":data.toString();
if(text == null)
{
text = "";
}
boolean bound = false;
if(binder != null)
{
bound = binder.setViewValue(v, data, text);
}
if(!bound)
{
/**
* 自定义适配器,关在在这里,根据传递过来的控件以及值的数据类型,
* 执行相应的方法,可以根据自己需要自行添加if语句。另外,CheckBox等
* 集成自TextView的控件也会被识别成TextView,这就需要判断值的类型
*/
if(v instanceof TextView)
{
//如果是TextView控件,则调用SimpleAdapter自带的方法,设置文本
setViewText((TextView)v, text);
}else if(v instanceof ImageView)
{
//如果是ImageView控件,调用自己写的方法,设置图片
setViewImage((ImageView)v, (Drawable)data);
}else
{
throw new IllegalStateException(v.getClass().getName() + " is not a " +
"view that can be bounds by this SimpleAdapter");
}
}
}
}
}
public void setViewImage(ImageView v, Drawable value)
{
v.setImageDrawable(value);
}
}
xml code
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/lv" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
piitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/icon" android:layout_width="48dip"
android:layout_height="48dip" />
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/appName" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/packageName" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
分享到:
相关推荐
Android中获取应用程序(包)的信息-PackageManager的使用方法
PackageManager是Android中一个很有用的类,能够获取已安装的应用(包)的信息,如应用名称、图标、权限,安装、删除应用(包)等
主要介绍了Android实现整理PackageManager获取所有安装程序信息的方法,实例分析了Android使用PackageManager获取安装程序信息的具体步骤与相关技巧,需要的朋友可以参考下
使用方法 示例一 var pm = new PM . Manager ( ) ; pm . setPath ( './packages' ) ; pm . add ( 'moduleA' , [ 'ModuleA.js' ] ) ; pm . load ( 'moduleA' ) . then ( ( ) => { alert ( moduleNameA ) ; ...
安装套件有三种安装软件包的方法。 您可以按关键字搜索,按标签浏览,或者如果您知道软件包的URL,则可以选择从URL。 如果您选择按关键字搜索,则会出现一个文本框,您可以在其中输入搜索查询。 当您单击下一步时,...
通过queryIntentActivities()方法,使用PackageManager,获取应用程序的信息使用
导入jar包。即可使用PackAgeManager的installPackage方法进行静默安装。注意复写回调来回馈安装结果。
近日想实现静默安装(不提示用户),网上搜文章大多提到一个方法 PackageManager.install,这是个非公开方法,它的用法是借鉴了内置应用 packageInstaller,跳过Activity提示用户,直接调用核心方法。用Git下载...
使用方法 使用本工具需要先安装 Java 环境,nkstool.jar 是已编译好的文件。 按需修改 config.txt 中的配置信息,把相应apk放到项目根目录。 运行,三种方式 用 IDEA 打开项目并运行源码。 Windows 系统可以直接打开...
静态方法/属性 Generator.make(options) Generator.defaultPrefixRules Generator.sharedProps Generator.shareProps(props) 实例属性 sharedProps (只读) shareProps(props) isStandalone (只读) ...
用法 1.启动 作为该存储库的根执行以下命令。 docker-compose up 2.访问网络浏览器 http://127.0.0.1:8888/ Windows开发 安装 1.PackageManager 在具有管理员权限的情况下启动powershell 。 电源外壳 Set-Execut
|--Manager之PackageManager |--Menu之不同模式下显示不同菜单 |--openGL-ES上绘制文字 |--openGL-ES纹理贴图 |--openGL-ES获取帧率 |--openGL-ES雾化 |--PopupWindow的使用 |--PopupWindow的返回健关闭 |--...
例如,如果应用程序想要安装应用程序,我们都知道我们应该使用PackageManager#getInstalledPackages() 。 这实际上是应用程序进程和系统服务器进程的进程间通信(IPC)进程,只是Android框架为我们完成了内部工作。...
用法 在你的webpack.config.js : plugins : [ new InstallPlugin ( ) ] , 这相当于: plugins : [ new InstallPlugin ( { dependencies : { peer : true , } , packageManager : { type : this .
弃用:请使用 从v2.3.0开始,npm-check-updates支持bower。 它具有许多有用的功能,并且维护良好。 $ npm install -g npm-check-updates $ npm-check-updates --packageManager bower # or 'ncu -m bower' for ...
但是第三方ROM并不能提供给我们权限请求成功或失败的接口,这就导致我们无法通过PackageManager提供的checkPermission()方法来准确的获取到我们是否获得该权限。只能根据具体的权限来做相应的处理。但是在Android6.0...
" "import " "android.content.pm.PackageManager.NameNotFoundException; " "import android.os.Bundle; " "import android.widget.TextView; " "public class ReadOtherPreferences extends Activity{ " "Context...
配套–通用实用程序和引导程序 –使用备用运行时表示形式的碰撞和物理构造块 –简单,可扩展,空间化的声音和音乐流Kinemation –创作的动画,模拟动画以及介于两者之间的所有内容(无公开发行) Mach-Axle AI –专...