您的位置:首页 > 移动开发 > Android开发

Android添加桌面快捷方式的简单实现

2013-11-26 07:20 591 查看
核心代码如下:

Button bn = (Button) findViewById(R.id.bn);
// 为按钮的单击事件添加监听器
bn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View source) {
// 创建添加快捷方式的Intent
Intent addIntent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT"); // ①
String title = getResources().getString(R.string.title);
// 设置快捷方式的标题
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); // ②
// 加载快捷方式的图标
Parcelable icon = Intent.ShortcutIconResource.fromContext(
AddShortcut.this, R.drawable.ic_launcher);
// 设置快捷方式的图标
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); // ②
// 创建点击快捷方式后操作Intent,该处当点击创建的快捷方式后,再次启动该程序
Intent myIntent = new Intent(AddShortcut.this,
AddShortcut.class);
// 设置快捷方式对应的Intent
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent); // ②
// 发送广播添加快捷方式
sendBroadcast(addIntent); // ③
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: