创建桌面快捷方式并且实现跳转到筛选出的第一个浏览器里

创建桌面快捷方式:

  <!-- 添加快捷方式权限 -->
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="android.permission.INTERNET" />

 <!-- 如果是通过桌面长按添加快捷方式,才需要在activity中添加此配置 -->
            <intent-filter>  
              <action android:name="android.intent.action.CREATE_SHORTCUT" />  
              <category android:name="android.intent.category.DEFAULT" />  
          </intent-filter> 



在oncreate中调用的方法:

// /**
    // * 被动的Action方式: 在桌面长按方式添加快捷方式-----> 需要在Manifest.xml中对主activity添加action监听。
    // */
    // private void initShortcutAction() {
    // final Intent launchIntent = getIntent();
    // final String action = launchIntent.getAction();
    // if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
    // setResult(RESULT_OK,
    // ShortcutUtils.getShortcutToDesktopIntent(HomePage.this));
    //
    // finish();
    // }
    // }

    /**
     * 主动的发广播方式: 启动应用后发送广播方式添加快捷方式----->
     */
    private void addShortcut2Desktop() {
        // 启动应用后发送广播方式添加快捷方式----->即主动的发广播方式
        sendBroadcast(utils.getShortcutToDesktopIntent(HomePage.this));

    }



**********************************************************************************************************************************************

/**
     *
     * 返回添加到桌面快捷方式的Intent:
     *
     * 1.给Intent指定action="com.android.launcher.INSTALL_SHORTCUT"
     *
     * 2.给定义为Intent.EXTRA_SHORTCUT_INENT的Intent设置与安装时一致的action(必须要有)
     *
     * 3.添加权限:com.android.launcher.permission.INSTALL_SHORTCUT
     */

/**
     * 生成桌面快捷方式
     *
     * @param context
     * @return
     */
    public Intent getShortcutToDesktopIntent(Context context) {
        // Intent intent = new Intent();
        // intent.setClass(context, context.getClass());
        // /* 以下两句是为了在卸载应用的时候同时删除桌面快捷方式 */
        // intent.setAction("android.intent.action.MAIN");
        // intent.addCategory("android.intent.category.LAUNCHER");
        Intent mintent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.parse("http://www.2cto.com");
        mintent.setData(uri);
        Intent shortcut = new Intent(
                "com.android.launcher.action.INSTALL_SHORTCUT");
        // 不允许重建
        shortcut.putExtra("duplicate", false);
        // 设置名字
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
                context.getString(R.string.app_name));
        // 设置图标
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(context,
                        R.drawable.ic_launcher));
        // 设置意图和快捷方式关联程序
        printInterestedActivitiesByIntent(mintent);
        System.out.println("---------?" + map.get(0).toString());
        System.out.println("---------?" + map1.get(0).toString());
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mintent);
        if (map.get(0) != null && map1.get(0) != null) {
            // mIntent.setClassName(map.get(0), map1.get(0));
            // mcontext.startActivity(mIntent);
            mintent.setClassName(map.get(0), map1.get(0));
            // mcontext.startActivity(mintent);
        }
        return shortcut;
    }

/**
     * 通过intent类型筛选出系统安装的应用,这里筛选的是Intent.ACTION_VIEW,即浏览器
     *
     * @param intent
     */
    public void printInterestedActivitiesByIntent(Intent intent) {

        List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
        if (null != activities) {
            for (int i = 0; i < activities.size(); i++) {
                ResolveInfo info = activities.get(i);
                map.put(i, info.activityInfo.packageName);
                map1.put(i, info.activityInfo.name);
                System.out.println(info.activityInfo.packageName);
                System.out.println(info.activityInfo.name);
            }
        } else {
            System.out.println("no interested activities");
        }
    }

/**
     * 判断是否已添加快捷方式: 暂时没有方法能够准确的判断到快捷方式,原因是,
     * 1、不同厂商的机型他的快捷方式uri不同,我遇到过HTC的他的URI是content
     * ://com.htc.launcher.settings/favorites?notify=true
     * 2、桌面不只是android自带的,可能是第三方的桌面,他们的快捷方式uri都不同
     *
     * 提供一个解决办法,创建快捷方式的时候保存到preference,或者建个文件在SD卡上,下次加载的时候判断不存在就先发删除广播,再重新创建
     *
     * 添加权限:<uses-permission
     * android:name="com.android.launcher.permission.READ_SETTINGS"
     * ></uses-permission>
     */
    public static boolean hasInstallShortcut(Context context) {
        boolean hasInstall = false;

        String AUTHORITY = "com.android.launcher.settings";
        int systemversion = Build.VERSION.SDK_INT;
        Log.i("Build.VERSION.SDK==========>", systemversion + "");
        /* 大于8的时候在com.android.launcher2.settings 里查询(未测试) */
        if (systemversion >= 8) {
            AUTHORITY = "com.android.launcher2.settings";
        }
        Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
                + "/favorites?notify=true");

        Cursor cursor = context.getContentResolver().query(CONTENT_URI,
                new String[] { "title" }, "title=?",
                new String[] { context.getString(R.string.app_name) }, null);

        if (cursor != null && cursor.getCount() > 0) {
            hasInstall = true;
        }

        return hasInstall;
    }


/**
     * 删除快捷方式
     * */
    public static void deleteShortCut(Context context) {
        Intent shortcut = new Intent(
                "com.android.launcher.action.UNINSTALL_SHORTCUT");
        // 快捷方式的名称
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
                context.getString(R.string.app_name));
        /** 删除和创建需要对应才能找到快捷方式并成功删除 **/
        Intent intent = new Intent();
        intent.setClass(context, context.getClass());
        intent.setAction("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.LAUNCHER");

        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
        context.sendBroadcast(shortcut);
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值