欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现获取安卓设备里已安装的软件包

程序员文章站 2024-03-07 15:41:03
之前的文章向大家介绍了几个项目中常用的实用功能,那么在这篇文中,我将向大家介绍如何获取android手机中已经安装的手机应用程序,好了,废话不多说,直接上代码 p...

之前的文章向大家介绍了几个项目中常用的实用功能,那么在这篇文中,我将向大家介绍如何获取android手机中已经安装的手机应用程序,好了,废话不多说,直接上代码

private string getinstalledapplistasstring(){
    packagemanager pm = context.getpackagemanager();
    list<applicationinfo> apps = pm.getinstalledapplications(packagemanager.get_gids);
    stringbuffer stringbuffer = new stringbuffer();
    for (applicationinfo app : apps) {
      if(pm.getlaunchintentforpackage(app.packagename) != null) {
        // apps with launcher intent
        if((app.flags & applicationinfo.flag_updated_system_app) == 1) {
          // updated system apps
 
        } else if ((app.flags & applicationinfo.flag_system) == 1) {
          // system apps
 
        } else {
          // user installed apps
          stringbuffer.append(app.packagename).append(';');
        }
      }
    }
    return stringbuffer.tostring();
  }

下面我们看看具体如何获取各个应用的相关信息呢

package cn.lyz.mobilesafe.domain;

import android.graphics.drawable.drawable;

/**
 * 获取的应用基本信息实体类
 * @author liuyazhuang
 *
 */
public class appinfo {
	//图标
	private drawable app_icon;
	//应用名称
	private string app_name;
	//应用版本号
	private string app_version;
	//应用包名
	private string packagename;
	//是否是用户app
	private boolean isuserapp;
	
	
	public appinfo() {
		super();
		// todo auto-generated constructor stub
	}
	public appinfo(drawable app_icon, string app_name, string app_version,
			string packagename) {
		super();
		this.app_icon = app_icon;
		this.app_name = app_name;
		this.app_version = app_version;
		this.packagename = packagename;
	}
	
	
	public appinfo(drawable app_icon, string app_name, string app_version,
			string packagename, boolean isuserapp) {
		super();
		this.app_icon = app_icon;
		this.app_name = app_name;
		this.app_version = app_version;
		this.packagename = packagename;
		this.isuserapp = isuserapp;
	}
	public drawable getapp_icon() {
		return app_icon;
	}
	public void setapp_icon(drawable app_icon) {
		this.app_icon = app_icon;
	}
	public string getapp_name() {
		return app_name;
	}
	public void setapp_name(string app_name) {
		this.app_name = app_name;
	}
	public string getapp_version() {
		return app_version;
	}
	public void setapp_version(string app_version) {
		this.app_version = app_version;
	}
	public string getpackagename() {
		return packagename;
	}
	public void setpackagename(string packagename) {
		this.packagename = packagename;
	}
	
	public boolean isuserapp() {
		return isuserapp;
	}
	public void setuserapp(boolean isuserapp) {
		this.isuserapp = isuserapp;
	}
	
	@override
	public string tostring() {
		return "appinfo [app_icon=" + app_icon + ", app_name=" + app_name
				+ ", app_version=" + app_version + ", packagename="
				+ packagename + ", isuserapp=" + isuserapp + "]";
	}
}