Android开发——查询/卸载手机里的应用、应用图标创建
1. 获取手机里的所有已安装的应用
以前写过一个SoftProviderUtil工具类,拿出来分享一个。通过PackageManager,不仅可以获取PackageName,判断此进程是否为系统应用,安装位置(在内存卡还是SD卡),还可以应用名称以及应用图标。代码如下。其中SoftInfo为自定义的业务类,成员变量即为要获取的信息,加上set/get方法即可。
/**
* For Info of InstalledPackages
* Created by user on 2016/4/23.
*/
public class SoftProviderUtil {
public static List<SoftInfo> getSoftInfo(Context context){
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0);
List<SoftInfo> infos = new ArrayList<>();
for(PackageInfo info : installedPackages) {
SoftInfo info_item = new SoftInfo();
String packageName = info.packageName;
//应用名字和图标
String name = info.applicationInfo.loadLabel(packageManager).toString();
Drawable drawable = info.applicationInfo.loadIcon(packageManager);
//应用程序信息的标记
int flags = info.applicationInfo.flags; if((flags & ApplicationInfo.FLAG_SYSTEM)== 0){
//是用户应用
info_item.setUserAPP(true);
}else {
//系统应用
info_item.setUserAPP(false);
}
if((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE)== 0){
//内存储
info_item.setInMemory(true);
}else {
//外存储
info_item.setInMemory(false);
}
info_item.setName(name);
info_item.setPackageName(packageName);
info_item.setDrawable(drawable);
infos.add(info_item);
}
return infos;
}
}
2. 卸载则是通过
卸载动作则是通过发送指定Action和Data来完成。参数为指定包名。
Intent intent_uninstall = new Intent();
intent_uninstall.setAction("android.intent.action.VIEW");
intent_uninstall.setAction("android.intent.action.DELETE");
intent_uninstall.addCategory("android.intent.category.DEFAULT");
intent_uninstall.setData(Uri.parse("package:" + packageName));
startActivityForResult(intent_uninstall,0);
3. 创建应用图标
卸载操作会相应的删除对应的应用图标以及桌面图标。当然,前提是有桌面图标。一般在应用刚开始被启动时,便会判断是否已经存在了自己应用的图标,如果存在就不用再创建了,否则桌面上会出现两个或者更多图标。如果不存在,便可以使用Intent发送请求,Launcher通过自己注册的InstallShortCutReceiver实现快捷方式图标的生成过程。
public static boolean hasShortCut(Context context) {
String url = "";
System.out.println(getSystemVersion());
if(getSystemVersion() < 8){
url = "content://com.android.launcher.settings/favorites?notify=true";
}else{
url = "content://com.android.launcher2.settings/favorites?notify=true";
}
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(Uri.parse(url), null, "title=?",
new String[] {context.getString(R.string.app_name)}, null); if (cursor != null && cursor.moveToFirst()) {
cursor.close();
return true;
} return false;
}
private static int getSystemVersion(){
return android.os.Build.VERSION.SDK_INT;
}
private void installShortCut() {
if(hasShortCut)
return;
Intent intent = new Intent();
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
//三个信息 图标,名字,以及点击逻辑
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"XXXX");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));
//封装一个intent
Intent shortcut = new Intent();
shortcut.setAction("android.intent.action.MAIN");
shortcut.addCategory("android.intent.category.LAUNCHER");
shortcut.setClassName(pckageName,"<packageName>.SplashActivity");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcut);
//发送广播
sendBroadcast(intent);
edit.putBoolean("shortcut",true);
edit.commit();
}
Android开发——查询/卸载手机里的应用、应用图标创建的更多相关文章
- Android开发——查询/杀死手机里正在运行的进程
0. 前言 以前有同学好像做过一个叫"自习君"的App,开启后自动检测用户这一天的自习时间,在学校里宣传广告还打了不少.其实实现原理非常简单,在SQlite数据库(也可以通过文件) ...
- Android开发之获取手机SIM卡信息
TelephonyManager是一个管理手机通话状态.电话网络信息的服务类.该类提供了大量的getXxx(),方法获取电话网络的相关信息. TelephonyManager类概述: 可用于訪问有关设 ...
- android开发 BaseAdapter中getView()里的3个参数是什么意思
BaseAdapter适配器里有个getView()需要重写public View getView(int position,View converView,ViewGroup parent){ // ...
- Android开发:修改eclipse里的Android虚拟机路径
一.发现问题: 今天打开电脑发现C盘缩了不少,这才意识到:eclipse里配置的安卓虚拟机默认放在了C盘里. 当然,在不同的电脑上可能路径有所不同,我的默认路径是:C:\Users\lenovo\.a ...
- Android开发之控制手机音频
本实例通过MediaPlayer播放一首音乐并通过AudioManager控制手机音频.关于AudioManager的具体解释可參照:Android开发之AudioManager(音频管理器)具体解释 ...
- Android开发之布局文件里实现OnClick事件关联处理方法
一般监听OnClickListener事件,我们都是通过Button button = (Button)findViewById(....); button.setOClickLisener....这 ...
- Android开发:在布局里移动ImageView控件
在做一个app时碰到需要移动一个图案的位置,查了一上午资料都没找到demo,自己写一个吧 RelativeLayout.LayoutParams lp = new RelativeLayout.Lay ...
- 【原创】Android开发使用华为手机调试logcat没有应用输出信息
输入 *#*#2846579#*#* 点击project Menu点击后台 1.设置logcat 2. Dump & Log",打开开关"打开Dump & Log& ...
- Android开发之华为手机无法看log日志解决方法(亲测可用华为荣耀6)
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985, 转载请说明出处. 在上家公司上班的时候,公司配了华为荣耀6的测试机,发现在eclipse下 无法查看 ...
随机推荐
- linux下实现多台服务器同步文件(inotify-tools+rsync实时同步文件安装和配置)
inotify-tools+rsync实时同步文件安装和配置 注:转载https://www.linuxidc.com/Linux/2012-06/63624.htm
- PL/SQL笔记(1)-流程控制,循环,异常,块
流程控制 1.If,then,else,elsif(不是elseif) ' then null; endif; 2.Case 简单case表达式: 搜索型Case表达式: 3.goto语句 begin ...
- mysql多表查询20题
+-------------------+| Tables_in_dapeng3 |+-------------------+| class || course || s1 || score || s ...
- [BZOJ1053][SDOI2005]反素数ant 数学
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1053 假设这个最大的反素数为$x$,那么$1<p<x$中数的因子数都没有$x$ ...
- struct和union
struct的小秘密 C语言中的struct可以看做变量的集合,struct的问题: 空结构体占用多大内存? 例子1:空结构体的大小 #include<stdio.h> struct ST ...
- 02html基础
02_html 1.几个标签 1.1 meta标签 meta标签的属性有name和http-equiv,其中name属性用于描述网页,对应于content(网页内容). <meta name=& ...
- CENTOS6.4上KVM虚拟机环境搭建
CENTOS6.4上KVM虚拟机环境搭建 关键词: KVM,虚拟机,windows7, VNC, 桥接网络,br0, SCSI, IDE 环境: host: CENTOS6.4 guest: ...
- SQL Server 2012使用OFFSET/FETCH NEXT分页及性能测试
最近在网上看到不少文章介绍使用SQL Server 2012的新特性:OFFSET/FETCH NEXT 实现分页.多数文章都是引用或者翻译的这一篇<SQL Server 2012 - Serv ...
- HashSet LinkedHashSet TreeSet 分析
1.HashSet分析 hashset 底层是hash表,就是hashMap,是无序的,唯一的.也就是说,它的底层其实就是一个HashMap key 值的组成值.所以具有唯一性. public Ha ...
- XtraBackUp 热备份工具
是一款强大的在线热备份工具 备份的过程中,不锁表 使用percona-xtrabackup-24-2.4.7-1.el7.x86_64.rpm yum源安装: 1.安装Percona的库: ...