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

    1. 需要权限:

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

    2. 判断是否创建

    /**
         * 判断快捷方式是否创建
         * @param context
         * @param name 快捷方式名称
         * @return
         */
        public static boolean hasShortcut(Context context, String name) {
            boolean isInstallShortcut = false;
            final ContentResolver cr = context.getContentResolver();
            final String AUTHORITY = "com.android.launcher.settings";
            final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
                    + "/favorites?notify=true");
            Cursor cursor = null;
            try {
                cursor = cr.query(CONTENT_URI, new String[] { "title",
                        "iconResource" }, "title=?", new String[] { name }, null);
                if (cursor != null && cursor.getCount() > 0) {
                    isInstallShortcut = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
    
            return isInstallShortcut;
        }
    3. 创建快捷方式
    /**
         * 添加快捷方式
         */
        public static void addShortcut(Activity activity, String name, int resourceId) {
    
            Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    
            // 快捷方式的名称
            shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
            shortcut.putExtra("duplicate", false); // 不允许重复创建
    
            // 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
            // 注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序
    
            /**************************** 此方法已失效 *************************/
            //ComponentName comp = new ComponentName(activity.getPackageName(), "." + activity.getLocalClassName());
            //shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
    
            // 快捷方式的图标
            ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(activity, resourceId);
            shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
    
            // 添加要做的事情
            Intent todo = new Intent(Intent.ACTION_MAIN);
            todo.setClassName(activity, activity.getClass().getName());
            todo.putExtra("test1", "test");
            // 点击快捷方式 进行 todo 操作
            shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, todo);
    
            activity.sendBroadcast(shortcut);
    
        }
    
    

    4.删除快捷方式

    /**
    	 * 删除快捷方式
    	 */
    	public static void delShortcut(Activity activity, String name) {
    		Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
    
    		// 快捷方式的名称
    		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    		String appClass = activity.getPackageName() + "." +activity.getLocalClassName();  
    		ComponentName comp = new ComponentName(activity.getPackageName(), appClass);
    		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
    		activity.sendBroadcast(shortcut);
    
    	}
    
     
  • 相关阅读:
    【2020-01-28】陪伴即陪伴,擦汗即擦汗
    【2020-01-27】曼巴走了,但他还在
    【2020-01-26】今年,远亲不如近邻了
    【2020-01-25】新的一年,新的传统
    【2020-01-24】上天为这小女孩开了一扇小小窗
    【2020-01-23】故作假装的毛病
    day 31 html(二) 和css入门
    前端 day 30 html 基础一
    day 17python 面对对象之继承
    多并发编程基础 之协成
  • 原文地址:https://www.cnblogs.com/chuckTsao/p/3302907.html
Copyright © 2011-2022 走看看