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();
    }


转载于:https://www.cnblogs.com/qitian1/p/6461611.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值