zoukankan      html  css  js  c++  java
  • Android桌面快捷方式图标生成与删除,使用Intent与launcher交互

    原创作者: dingran

      通过分析Launcher的生成快捷方式的过程,找出了使用Intent发送请求,Launcher通过自己注册的InstallShortCutReceiver和UnInstallShortCutReceiver实现了快捷方式图标的生成与移除过程。本文主要分析外部apk如何使用Intent请求生成快捷方式和移除快捷方式图标的问题。

     

    生成快捷方式代码:

    private static final String ACTION_INSTALL_SHORTCUT =
     "com.android.launcher.action.INSTALL_SHORTCUT";
     
    /**
     * 是否可以有多个快捷方式的副本
    */
    static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";

    Intent shortcutIntent = new Intent(ACTION_INSTALL_SHORTCUT); 
            shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, 
                    getString(R.string.app_name)); 
            shortcutIntent.putExtra(EXTRA_SHORTCUT_DUPLICATE, false); 
            Intent intent2 = new Intent(Intent.ACTION_MAIN); 
            intent2.addCategory(Intent.CATEGORY_LAUNCHER);

            intent2.setComponent(new ComponentName(this.getPackageName(), 
                    ".Main")); 
             
            shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2); 
            shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
                    Intent.ShortcutIconResource.fromContext(this, 
                            R.drawable.icon)); 
            sendBroadcast(shortcutIntent);

     

     注:Intent intent2 = new Intent(Intent.ACTION_MAIN);  这个也可以换成的构造参数也可以是Intent.ACTION_CREATE_SHORTCUT,也可以生成快捷方式图标,但是这样不标准,在删除的时候如果不和这个对于相同则无法删除。所以还是用Intent.ACTION_MAIN。

     

    那么删除快捷方式的代码是:

    private static final String ACTION_UNINSTALL_SHORTCUT =
    "com.android.launcher.action.UNINSTALL_SHORTCUT";

    Intent intent = new Intent(ACTION_UNINSTALL_SHORTCUT );
      intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
      ComponentName comp = new ComponentName(info.activityInfo.packageName,
        info.activityInfo.name);
      intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent()
      .setComponent(comp).setAction("android.intent.action.MAIN"));
      sendBroadcast(intent);

  • 相关阅读:
    5.Docker服务进程关系
    朴素贝叶斯知识点概括
    k近邻法(KNN)知识点概括
    机器学习的应用实例
    HNU 10111 0-1矩阵
    CSU 1421 Necklace
    Poj 3469 Dual Core CPU
    Poj 2135 Farm Tour
    Poj 3180 The Cow Prom
    HDU 1004 Let the Balloon Rise
  • 原文地址:https://www.cnblogs.com/xiaoxiaoboke/p/2093180.html
Copyright © 2011-2022 走看看