zoukankan      html  css  js  c++  java
  • 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();
        }


  • 相关阅读:
    洛谷p1017 进制转换(2000noip提高组)
    Personal Training of RDC
    XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Eurasia
    XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Peterhof.
    Asia Hong Kong Regional Contest 2019
    XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Siberia
    XVIII Open Cup named after E.V. Pankratiev. Ukrainian Grand Prix.
    XVIII Open Cup named after E.V. Pankratiev. GP of SPb
    卜题仓库
    2014 ACM-ICPC Vietnam National First Round
  • 原文地址:https://www.cnblogs.com/qitian1/p/6461611.html
Copyright © 2011-2022 走看看