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开发——查询/卸载手机里的应用、应用图标创建的更多相关文章

  1. Android开发——查询/杀死手机里正在运行的进程

    0. 前言 以前有同学好像做过一个叫"自习君"的App,开启后自动检测用户这一天的自习时间,在学校里宣传广告还打了不少.其实实现原理非常简单,在SQlite数据库(也可以通过文件) ...

  2. Android开发之获取手机SIM卡信息

    TelephonyManager是一个管理手机通话状态.电话网络信息的服务类.该类提供了大量的getXxx(),方法获取电话网络的相关信息. TelephonyManager类概述: 可用于訪问有关设 ...

  3. android开发 BaseAdapter中getView()里的3个参数是什么意思

    BaseAdapter适配器里有个getView()需要重写public View getView(int position,View converView,ViewGroup parent){ // ...

  4. Android开发:修改eclipse里的Android虚拟机路径

    一.发现问题: 今天打开电脑发现C盘缩了不少,这才意识到:eclipse里配置的安卓虚拟机默认放在了C盘里. 当然,在不同的电脑上可能路径有所不同,我的默认路径是:C:\Users\lenovo\.a ...

  5. Android开发之控制手机音频

    本实例通过MediaPlayer播放一首音乐并通过AudioManager控制手机音频.关于AudioManager的具体解释可參照:Android开发之AudioManager(音频管理器)具体解释 ...

  6. Android开发之布局文件里实现OnClick事件关联处理方法

    一般监听OnClickListener事件,我们都是通过Button button = (Button)findViewById(....); button.setOClickLisener....这 ...

  7. Android开发:在布局里移动ImageView控件

    在做一个app时碰到需要移动一个图案的位置,查了一上午资料都没找到demo,自己写一个吧 RelativeLayout.LayoutParams lp = new RelativeLayout.Lay ...

  8. 【原创】Android开发使用华为手机调试logcat没有应用输出信息

    输入 *#*#2846579#*#* 点击project Menu点击后台 1.设置logcat 2. Dump & Log",打开开关"打开Dump & Log& ...

  9. Android开发之华为手机无法看log日志解决方法(亲测可用华为荣耀6)

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985, 转载请说明出处. 在上家公司上班的时候,公司配了华为荣耀6的测试机,发现在eclipse下 无法查看 ...

随机推荐

  1. bzoj 4821 [Sdoi2017]相关分析

    题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4821 题解 做法显然 就是维护一颗线段树 里面装4个东西 区间x的和 区间y的和 区间$x^ ...

  2. HDU - 6063 RXD and math

    Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6063 打表发现规律是n^k #include <iostream> #inc ...

  3. 构造 Bubble Cup 8 - Finals D. Tablecity

    题目传送门 题意:在1000*2的格子里,在每个小时能派出两个警察在两个地点搜查小偷,求在2015小时内能抓住小偷的方案. 分析:首先每次扫过一列即i1 i2从左往右扫,这样会漏掉小偷正好从间隙穿过的 ...

  4. iOS7改变状态栏文字颜色

    1在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO2 在需要改变状态栏颜色的 AppDelegate中在 didFinishLaun ...

  5. 容器API

  6. Problem D: 勤奋的涟漪2 dp + 求导

    http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=3 dp[i][state]表示处理了前i个,然后当前状态是state的时候的 ...

  7. ionic之自定义图片

    一个好的app,必须都有很好的ui设计师来设计界面,增强客户的体验,表现自己本身公司的特色,但是,在ionic中有些是无法用img标签直接引入图片,只能通过设定的css之后引入css. 页面: < ...

  8. Winform datagridview 基础

    ======================================================================================== == 重点需要掌握 A ...

  9. 外文翻译 《How we decide》被情感愚弄 第三节

    本科论文答辩终于结束啦,一切都要继续回到正轨. 这是第三章章最后一节 书的导言 本章第二节 本章第一节 "信用卡是我的敌人."Herman Palmer这样说到.在平日,Herma ...

  10. 开启server-status失败

    近日在配置监控宝的apache监控老是出错,经过研究发现如下: 下面先做一些简要的介绍,以防以后查看之用. 一.server-status是什么?二.如何打开server-status?三.serve ...