您的位置:首页 > 其它

通过程序在桌面添加快捷方式

2015-12-02 14:06 399 查看
通过程序添加快捷方式:

在程序中把一个软件的快捷方式添加到桌面上,按如下三步即可:
(1)创建一个添加快捷方式的Intent,该Intent的Action属性值应该为com.android.launcher.action.INSTALL_SHORTCUT
(2)通过为该Intent添加Extra属性来设置快捷方式的标题,图标以及快捷方式对应启动的程序。

(3)调用sendBroadcast()方法来发送广播即可添加快捷方式

/**
* 添加快捷方式的代码
* 注:需要添加相应的权限
*
* @param source
*/
public void addShortcut(View source) {
//添加系统的添加快捷方式的API
Intent addIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//快捷方式标题
String title = getResources().getString(R.string.title);
//加载快捷方式的图标
Parcelable icon = Intent.ShortcutIconResource.fromContext(MultiThreadDown.this, R.mipmap.ic_launcher);
//创建点击快捷方式后操作Intent,该处当点击创建的快捷方式之后,再次启动该程序
Intent intent = new Intent(MultiThreadDown.this, MultiThreadDown.class);
//设置快捷方式的标题
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
//设置快捷方式的图标
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//设置快捷方式对应的Intent
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
//发送广播添加快捷方式
sendBroadcast(addIntent);
}


在AndroidManifest.xml配置清单添加如下权限:

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


完成
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: