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. VS2017专业版使用最新版Qt5.9.2教程(最新教材)

    QT配置  Creator    ================================================================= 之前运行release版本或者de ...

  2. 1051:A × B problem 大数相乘

    给你两个整数,请你计算A × B. 输入 数据的第一行是整数T(1 ≤ T ≤ 20),代表测试数据的组数.接着有T组数据,每组数据只有一行,包括两个非负整数A和B.但A和B非常大,Redraimen ...

  3. Educational Codeforces Round 19 A

    Description Given a positive integer n, find k integers (not necessary distinct) such that all these ...

  4. Mike and gcd problem CodeForces - 798C

    题目 (智商题 or 糟心的贪心) 题意: 有一个数列a1,a2,...,an,每次操作可以将相邻的两个数x,y变为x-y,x+y,求最少的操作数使得gcd(a1,a2,...,an)>1.gc ...

  5. 人品问题 树形dp

    题目 网上出现了一种高科技产品——人品测试器.只要你把你的真实姓名输入进去,系统将自动输出你的人品指数.把儿不相信自己的人品为0.经过了许多研究后,把儿得出了一个更为科学的人品计算方法.这种方法的理论 ...

  6. git导出代码

    1.快速查询 $git archive --format zip --output "./output.zip" master -0 ./output.zip 是生成的文件 mas ...

  7. bootstrap div 固定

    div固定在顶部样式: .navbar-fixed-top div固定在底部样式 .navbar-fixed-bottom

  8. spring boot jar启动

    1.依赖包输出 记得禁用热加载 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactI ...

  9. vue中的事件监听之——v-on vs .$on

    跟着视频中老师的教学视频学vue的时候,看很多时候都用@(v-on)来监听子级emit的自定义事件,但在bus总线那块,又用.$on来监听bus自身emit的事件,v-on之间似乎相似但又不同,今天对 ...

  10. 初学web前端,掌握这些就足够了!

    Web开发如今是如日中天,热的发烫.那我们应该怎么学习呢?这不光是初学者,很多学了几年的人也会有些迷茫或者彷徨,大家也都知道不断学习是不可避免的,不学习肯定要掉队:那怎么学效率更高,那些是坑,那些是路 ...