android通过快捷方式打开一个网页

有时候我们需要app在桌面生成一个快捷方式,通过这个快捷方式可以打开一个网页,方法如下:

	public void addShortcut(Parcelable icon, String name, Uri uri) {
		Intent intentAddShortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
		intentAddShortcut.putExtra("duplicate", false);
		// 添加名称
		intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
		// 添加图标
		intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
		// 设置Launcher的Uri数据
		Intent intentLauncher = new Intent();
		intentLauncher.setData(uri);
		// 添加快捷方式的启动方法
		intentAddShortcut
				.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intentLauncher);
		sendBroadcast(intentAddShortcut);
	}

在activity的onCreate中,使用如下调用:

		Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
		addShortcut(icon,"淘宝",Uri.parse("http://www.taobao.com"));

就可以使用浏览器打开一个网址,这里是淘宝网!!!

稍微更改一下,就可以生成一个指向主activity的快捷方式,进行如下的更改:

		Intent intentLauncher = new Intent(new Intent(this, this.getClass()).setAction(Intent.ACTION_MAIN));
		//intentLauncher.setData(uri);

最后别忘了,加上权限:

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


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值