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 itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

在不同系统间迁移Python程序的方法与教程

《在不同系统间迁移Python程序的方法与教程》本文介绍了几种将Windows上编写的Python程序迁移到Linux服务器上的方法,包括使用虚拟环境和依赖冻结、容器化技术(如Docker)、使用An... 目录使用虚拟环境和依赖冻结1. 创建虚拟环境2. 冻结依赖使用容器化技术(如 docker)1. 创

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

Java数字转换工具类NumberUtil的使用

《Java数字转换工具类NumberUtil的使用》NumberUtil是一个功能强大的Java工具类,用于处理数字的各种操作,包括数值运算、格式化、随机数生成和数值判断,下面就来介绍一下Number... 目录一、NumberUtil类概述二、主要功能介绍1. 数值运算2. 格式化3. 数值判断4. 随机

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

Springboot 中使用Sentinel的详细步骤

《Springboot中使用Sentinel的详细步骤》文章介绍了如何在SpringBoot中使用Sentinel进行限流和熔断降级,首先添加依赖,配置Sentinel控制台地址,定义受保护的资源,... 目录步骤 1: 添加 Sentinel 依赖步骤 2: 配置 Sentinel步骤 3: 定义受保护的

Python中Markdown库的使用示例详解

《Python中Markdown库的使用示例详解》Markdown库是一个用于处理Markdown文本的Python工具,这篇文章主要为大家详细介绍了Markdown库的具体使用,感兴趣的... 目录一、背景二、什么是 Markdown 库三、如何安装这个库四、库函数使用方法1. markdown.mark

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加