有时候我们需要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" />