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

2024-05-16 10:38

本文主要是介绍Android之使用PackageManager取得程序的包名、图标等,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



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

效果图:

 

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;  //  获得该应用程序的启动Activity的name
                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>  

复制代码


根据packagename来启动程序:

复制代码
                    final String activityName = getActivityName(packageName);if (null == activityName) {Toast.makeText(ShowAppActivity.this, "程序无法启动",Toast.LENGTH_SHORT);return;}Intent intent = new Intent();intent.setComponent(new ComponentName(packageName,activityName));startActivity(intent);
复制代码

 

 

根据packagename来卸载程序:

Intent deleteIntent = new Intent();deleteIntent.setAction(Intent.ACTION_DELETE);deleteIntent.setData(Uri.parse("package:" + packagename));

这篇关于Android之使用PackageManager取得程序的包名、图标等的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/994672

相关文章

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.