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


  • 相关阅读:
    JavaScript--DOM修改元素的属性
    JavaScript---DOM文档
    SQL查询语言练习
    SQL Server视图复习
    SQL Server存储过程复习(一)
    SQL Server中的连接查询【内连接,左连接,右连接,。。。】
    Linq查询简介
    CSS--选择器
    csharp:Conversion Between DataTable and List
    csharp: Linq keyword example
  • 原文地址:https://www.cnblogs.com/qitian1/p/6461611.html
Copyright © 2011-2022 走看看