Android之使用PackageManager取得程序的包名、图标等

2023-11-03

图:

 

Model代码:

public class AppInfo {
  
    
private String appLabel;    
    
private Drawable appIcon ;  
    
private Intent intent ;     
    
private String pkgName ;    
    
    
public AppInfo(){}
    
    
public String getAppLabel() {
        
return appLabel;
    }
    
public void setAppLabel(String appName) {
        
this.appLabel = appName;
    }
    
public Drawable getAppIcon() {
        
return appIcon;
    }
    
public void setAppIcon(Drawable appIcon) {
        
this.appIcon = appIcon;
    }
    
public Intent getIntent() {
        
return intent;
    }
    
public void setIntent(Intent intent) {
        
this.intent = intent;
    }
    
public String getPkgName(){
        
return pkgName ;
    }
    
public void setPkgName(String pkgName){
        
this.pkgName=pkgName ;
    }

 

继承BaseAdapter 

public class BrowseApplicationInfoAdapter extends BaseAdapter {
    
    
private List<AppInfo> mlistAppInfo = null;
    
    LayoutInflater infater = 
null;
    
    
public BrowseApplicationInfoAdapter(Context context,  List<AppInfo> apps) {
        infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mlistAppInfo = apps ;
    }
    @Override
    
public int getCount() {
        
// TODO Auto-generated method stub
        System.out.println("size" + mlistAppInfo.size());
        
return mlistAppInfo.size();
    }
    @Override
    
public Object getItem(int position) {
        
// TODO Auto-generated method stub
        return mlistAppInfo.get(position);
    }
    @Override
    
public long getItemId(int position) {
        
// TODO Auto-generated method stub
        return 0;
    }
    @Override
    
public View getView(int position, View convertview, ViewGroup arg2) {
        System.out.println("getView at " + position);
        View view = 
null;
        ViewHolder holder = 
null;
        
if (convertview == null || convertview.getTag() == null) {
            view = infater.inflate(R.layout.browse_app_item, 
null);
            holder = 
new ViewHolder(view);
            view.setTag(holder);
        } 
        
else{
            view = convertview ;
            holder = (ViewHolder) convertview.getTag() ;
        }
        AppInfo appInfo = (AppInfo) getItem(position);
        holder.appIcon.setImageDrawable(appInfo.getAppIcon());
        holder.tvAppLabel.setText(appInfo.getAppLabel());
        holder.tvPkgName.setText(appInfo.getPkgName());
        
return view;
    }

    
class ViewHolder {
        ImageView appIcon;
        TextView tvAppLabel;
        TextView tvPkgName;

        
public ViewHolder(View view) {
            
this.appIcon = (ImageView) view.findViewById(R.id.imgApp);
            
this.tvAppLabel = (TextView) view.findViewById(R.id.tvAppLabel);
            
this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName);
        }
    }
}

 

MainActivity

public class MainActivity extends Activity implements OnItemClickListener {

    
private ListView listview = null;

    
private List<AppInfo> mlistAppInfo = null;

    @Override
    
public void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.browse_app_list);

        listview = (ListView) findViewById(R.id.listviewApp);
        mlistAppInfo = 
new ArrayList<AppInfo>();
        queryAppInfo(); 
// 查询所有应用程序信息
        BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter(
                
this, mlistAppInfo);
        listview.setAdapter(browseAppAdapter);
        listview.setOnItemClickListener(
this);
    }
    
// 点击跳转至该应用程序
    public void onItemClick(AdapterView<?> arg0, View view, int position,
            
long arg3) {
        
// TODO Auto-generated method stub
        Intent intent = mlistAppInfo.get(position).getIntent();
        startActivity(intent);
    }
    
// 获得所有启动Activity的信息,类似于Launch界面
    public void queryAppInfo() {
        PackageManager pm = 
this.getPackageManager(); //获得PackageManager对象
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        
// 通过查询,获得所有ResolveInfo对象.
        List<ResolveInfo> resolveInfos = pm
                .queryIntentActivities(mainIntent, PackageManager.MATCH_DEFAULT_ONLY);
        
// 调用系统排序  根据name排序
        // 
该排序很重要,否则只能显示系统应用,而不能列出第三方应用程序
        Collections.sort(resolveInfos,new ResolveInfo.DisplayNameComparator(pm));
        
if (mlistAppInfo != null) {
            mlistAppInfo.clear();
            
for (ResolveInfo reInfo : resolveInfos) {
                String activityName = reInfo.activityInfo.name; 
// 获得该应用程序的启动Activityname
                String pkgName = reInfo.activityInfo.packageName; // 获得应用程序的包名
                String appLabel = (String) reInfo.loadLabel(pm); // 获得应用程序的Label
                Drawable icon = reInfo.loadIcon(pm); // 获得应用程序图标
                // 
为应用程序的启动Activity 准备Intent
                Intent launchIntent = new Intent();
                launchIntent.setComponent(
new ComponentName(pkgName,
                        activityName));
                
// 创建一个AppInfo对象,并赋值
                AppInfo appInfo = new AppInfo();
                appInfo.setAppLabel(appLabel);
                appInfo.setPkgName(pkgName);
                appInfo.setAppIcon(icon);
                appInfo.setIntent(launchIntent);
                mlistAppInfo.add(appInfo); 
// 添加至列表中
            }
        }
    }

 

 

AndroidManifest文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package
="com.qin.appinfo" android:versionCode="1" android:versionName="1.0">
    
<application android:icon="@drawable/icon" android:label="@string/app_name">
        
<activity android:name=".MainActivity" android:label="@string/app_name">
            
<intent-filter>
                
<action android:name="android.intent.action.MAIN" />
                
<category android:name="android.intent.category.LAUNCHER" />
                
<category android:name="android.intent.category.DEFAULT" />
            
</intent-filter>
        
</activity>
    
</application>

</manifest>   

 

browse_app_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
="fill_parent" android:layout_height="50dip">

    
<ImageView android:id="@+id/imgApp" android:layout_width="wrap_content"
        android:layout_height
="fill_parent" ></ImageView>
    
<RelativeLayout android:layout_width="fill_parent"  android:layout_marginLeft="10dip"
        android:layout_height
="40dip">
        
<TextView android:id="@+id/tvLabel" android:layout_width="wrap_content"
            android:layout_height
="wrap_content" android:text="AppLable : "></TextView>
        
<TextView android:id="@+id/tvAppLabel" android:layout_width="wrap_content"
            android:layout_toRightOf
="@id/tvLabel" android:layout_height="wrap_content"
            android:layout_marginLeft
="3dip" android:text="Label" android:textColor="#FFD700"></TextView>
        
<TextView android:id="@+id/tvName" android:layout_width="wrap_content"
            android:layout_height
="wrap_content" android:layout_below="@id/tvLabel" 
            android:text
="包名:"></TextView>
        
<TextView android:id="@+id/tvPkgName" android:layout_width="wrap_content"
            android:layout_height
="wrap_content" android:layout_below="@id/tvAppLabel"
            android:layout_alignLeft
="@id/tvAppLabel" android:textColor="#FFD700"></TextView>
    
</RelativeLayout>
</LinearLayout>

 

broswe_app_list.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/listviewApp" android:layout_width="fill_parent"
        android:layout_height
="fill_parent" ></ListView>

</LinearLayout>  

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android之使用PackageManager取得程序的包名、图标等 的相关文章

  • 在Windows7环境下使用GIT BASH免输入密码

    背景 根据一路向北的教 Windows下TortoiseGit over Putty or Openssh提交项目到GitLab 安装了TortoiseGit客户端 并完成在Gitlab上公钥的部署 但是有些操作必须通过GITbash命令行
  • 现代教育技术计算机网络试题及答案,《现代教育技术》期末复习题及答案

    现代教育技术期末复习1 一 填空题 1 教育技术就是人类在教育活动中所采用的一切 和方法的总和 它分为有形的技术 物化形态 和 的技术 智能形态 两大类 答案 技术手段 无形 2 学习资源主要包括教学材料 和 答案 支持系统 学习环境 3
  • UE4c++ Brush创建

    前言 UE4常用的new FSlateBrush的方式与正常的SlateStyle的方式就不说了 这类的文章很多 这里主要仿照引擎源码加载图片的方式加加载 参考源码 FTestStyle h class FMainStyle public
  • SpringBoot主程序运行及配置文件

    主程序运行 SpringBootApplication public class SpringbootApplication public static void main String args SpringApplication run
  • 5G 的未来

    目录 5G流量 5G应用场景 VR AR MR 5G关键技术 超密集组网 5G关键技术 动态自组织网 SON 软件定义网络SDN SDN与NFV的深度融合 5G挑战 频谱资源 新业务的挑战 新使用场景的挑战 终端设备带来的挑战 5G面临的安

随机推荐

  • 反射和多线程基础

    Version 邢朋辉 今日主播 邢朋辉 QQ 345086739 一 今日内容 1 1 课程回顾 1 2 反射是啥 1 3 进程和线程 1 4 线程的创建方式 1 5 线程的状态 1 6 线程的常用方法 二 课程回顾 Java的基本语法
  • R语言的常用的包

    在学习R的时候 R的包众多 很多时候对于初学者会造成很大的困扰就是不知道用什么样的包比较合适 我会在不断使用R的过程中 进行使用同时结合使用体验为大家推荐合适的R包 避免重复学习 以节约时间 标了 精 的是需要重点研究和掌握的包 1 数据导
  • 写CSDN博客时,调节字体、大小、颜色

    字体颜色样式系列 为了突出重点内容 想设置不同颜色 颜色挑选网址 https blog csdn net wo919191 article details 84249531 face设置字体 文本内容使用font标签包裹 可通过face设置
  • 查询tomcat可用 端口

    步骤一 cmd输入 输入 netstat ano 步骤二 查询端口号是否被占用 netstat aon findstr 端口号 步骤三 查询占用端口的应用 tasklist findstr 查询出的 listening 步骤四 taskki
  • 如果有多个异步函数需要保证同时执行并保证全部执行完毕后再进行下一步

    array push api post array push api get Promise all array then resArr gt resArr forEach res gt console log res 在 then后面判断
  • python3 抖音短视频链接去水印下载视频到本地

    基于近段时间对抖音 快手 秒拍等视频抓取一直想搞一下 加了个QQ群 里面全是自媒体 就是抖音 快手 秒拍的视频搬运工 把一个平台搬到另外一个平台上 去除水印 降低被干掉的危险 经过半天的琢磨 自己用python也搞出来一个根据抖音分享视频链
  • python用input输入列表_python怎么用input函数输入一个列表

    在Python3 0以后 键盘输入使用input函数 gt gt gt x input gt gt gt 123 123 在命令行没有任何显示 输入123后直接赋值给x 并打印 仅仅使用input是无法解决大部分数据处理的 通常输入的字符串
  • Python构建ANN模型预测气温变化

    在利用爬虫模型得到了气温数据集后 具体参考上篇Python构建爬虫模型爬取天气数据 我们开始利用tensorflow自带的模块搭建一个简单的ANN模型预测气温变化 其实这个模型适用于预测很多数据变化趋势 这里以预测气温变化为例 import
  • 数据结构--栈—JS实现一个栈结构

    数据结构 栈 JS实现一个栈结构 前言 数据结构和算法是脱离语言的 比如pop push在js中可以使用 但是其他的语言也有吗 不一定 但是都可以通过数据结构和算法写出其功能 1 栈是一种后进先出 LIFO last in first ou
  • 'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte问题的解决

    utf 8 codec can t decode byte 0xd6 in position 0 invalid continuation byte 把 utf 8 改为 gbk
  • git进行commit撤销,并撤销远程push,回退到之前的版本

    刚开始使用版本管理工具 选择的是git 各种git终端 常用的有SourceTree TortoiseGit git GUI等 本人选择的是第一个 闲话不多说 当你commit并push本地代码到云服务器后 发现自己修改的有问题 但又不想重
  • 知识图谱学习--网易云唐宇迪老师课程记录

    一 知识图谱是什么 知识图谱是一种图模型 可以将各个实体的信息联系在一起 形成一个整体 知识图谱会应用各种不同技术 不仅限于NLP 还包括图像 推荐系统等 构建一整个关系网络 知识图谱在医疗领域的作用 二 知识图谱的数据怎么处理 1 数据怎
  • GPT「高仿」问世:GPT-Neo,最大可达GPT-3大小,已开源

    GPT 高仿 问世 GPT Neo 最大可达GPT 3大小 已开源 近日 有个名叫 EleutherAI的团队 创始成员为 Connor Leahy Leo Gao和Sid Black 宣布推出GPT Neo开源项目 可用于复现GPT系列的
  • 强化学习-论文调研-experience replay

    experience replay 论文调研 一 论文概要 1 Hindsight Experience Replay 2017 NeurIPS 在奖励稀疏的情况下 要用强化学习算法训练是很困难的 本文提出一种通过增设不同的目标 增加状态转
  • Amazon Linux 2上面安装Amazon Corretto 8(JDK)

    shell 启用Amazon Linux 2 中的aws yum扩展库 sudo amazon linux extras enable corretto8 安装jre sudo yum install java 1 8 0 amazon c
  • spring cloud jackson自定义文本转换器

    由于 spring jackson default property inclusion 配置失效 所以得自定义文本转换器 废话不多说上代码 Configuration EnableWebMvc public class WebAppCon
  • pytorch中torchvision.utils包下的save_image函数

    雷郭出品 函数的用途 将NCHW的tensor以网格图的形式存储到硬盘中 该图也叫做雪碧图sprite image 如下图所示 将多张图以网格的形式拼凑起来 每张图的大小是28 28 单通道 那宽高如何确定 我们可以来看看该函数的源码 de
  • K8S的DaemonSet控制器

    1 什么是DaemonSet DaemonSet确保全部 或者一些 Node上运行一个pod的副本 当有Node加入集群时 也会为他们新增一个pod 当有Node从集群移除时 这些pod也会被回收 删除DaemonSet将会删除它创建的所有
  • 牛客剑指offer之【JZ13 机器人的运动范围】

    1 题目 2 示例解读 示例1输入的第一个参数为1 即threshold的值 第二 三个参数分别为2 3 即一个二行三列的格子 返回行坐标和列坐标的数位之和大于 threshold 的格子数 为3 具体如下 3 解题思路 根据题目分析可得
  • Android之使用PackageManager取得程序的包名、图标等

    图 Model代码 public class AppInfo private String appLabel private Drawable appIcon private Intent intent private String pkg