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);
    
    	}
    
     
  • 相关阅读:
    IP 协议
    以太网协议
    制作Win10系统安装U盘和安装纯净版Win10
    IP地址的配置
    进制转换
    设置QQ环境变量
    修改IE默认页的指向
    虚拟机安装Linux ubuntu19.10
    【Eclipse】Editor does not contain a main type
    Vmware Workstation虚拟机
  • 原文地址:https://www.cnblogs.com/chuckTsao/p/3302907.html
Copyright © 2011-2022 走看看