zoukankan      html  css  js  c++  java
  • Android--创建快捷方式

    需要权限:

    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

    调用方法:

    ShortCut.create(this);
    ShortCut代码如下:
    public class ShortCut {
        /**
         * 是否存在快捷方式
         * @param context
         * @return
         */
        private static boolean isAddShortCut(Context context) {
    
            boolean isInstallShortcut = false;
            final ContentResolver cr = context.getContentResolver();
    
            int versionLevel = android.os.Build.VERSION.SDK_INT;
            String AUTHORITY = "com.android.launcher2.settings";
    
            // 2.2以上的系统的文件文件名字是不一样的
            if (versionLevel >= 8) {
                AUTHORITY = "com.android.launcher2.settings";
            } else {
                AUTHORITY = "com.android.launcher.settings";
            }
    
            final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
                    + "/favorites?notify=true");
            Cursor c = cr.query(CONTENT_URI,
                    new String[] { "title", "iconResource" }, "title=?",
                    new String[] { context.getString(R.string.app_name) }, null);
    
            if (c != null && c.getCount() > 0) {
                isInstallShortcut = true;
            }
            return isInstallShortcut;
        }
    
        /**
         * 添加快捷方式
         * @param context
         */
        private static void addShortCut(Context context) {
    
            Intent shortcut = new Intent(
                    "com.android.launcher.action.INSTALL_SHORTCUT");
            // 设置属性
            shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getResources()
                    .getString(R.string.app_name));
            ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
                    context.getApplicationContext(), R.drawable.ic_launcher);
            shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconRes);
    
            // 是否允许重复创建
            shortcut.putExtra("duplicate", false);
    
            // 设置桌面快捷方式的图标
            Parcelable icon = Intent.ShortcutIconResource.fromContext(context,
                    R.drawable.ic_launcher);
            shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    
            // 点击快捷方式的操作
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setClass(context, AppListActivity.class);
    
            // 设置启动程序
            shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
    
            // 广播通知桌面去创建
            context.sendBroadcast(shortcut);
        }
    
        /**
         * 外部调用  创建快捷方式
         * @param context
         */
        public static void create(Context context) {
            if(context != null)
            {
                if(!isAddShortCut(context))
                {
                    addShortCut(context);
                }
            }
        }
  • 相关阅读:
    【左偏树+贪心】BZOJ1367-[Baltic2004]sequence
    【裸裸的左偏树】BZOJ1455-罗马游戏
    【贪心+Treap】BZOJ1691-[Usaco2007 Dec]挑剔的美食家
    【贪心】BZOJ3668-[NOI2014]起床困难综合症
    【多重背包小小的优化(。・∀・)ノ゙】BZOJ1531-[POI2005]Bank notes
    【可持久化并查集】BZOJ3673-可持久化并查集 by zky
    【manacher+FFT】BZOJ3160-万径人踪灭
    SCOI2008着色方案(记忆化搜索)
    [JSOI2008]魔兽地图(树形dp)
    NOI2017蔬菜(贪心)
  • 原文地址:https://www.cnblogs.com/crane13/p/4085604.html
Copyright © 2011-2022 走看看