http://www.eoeandroid.com/forum.php?mod=viewthread&tid=263994
需求:应用程序启动的时候,可以检测下是否已经添加了快捷方式,没有添加的话就添加
public void checkShortCut() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mapViewActivity);
// 是否在桌面上添加了快捷方式
boolean never_check_shortCut = sp.getBoolean("never_check_shortCut",
false);
// 存在快捷方式或者不允许添加,return
if (never_check_shortCut){
return;
}else{
//其实这里最好做成对话框提示的形式,这样给用户感觉会更友好,我这就简单点,直接默认会添加了
addShortCut();
// 保存已经添加了快捷方式的信息,以便程序下次启动的不再提示
Editor editor = sp.edit();
editor.putBoolean("never_check_shortCut", true);
editor.commit();
}
}
public void addShortCut() {
// 添加快捷方式
//指定快捷方式的Action
Intent installShortCut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//添加快捷方式的名称
installShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
installShortCut.putExtra("duplicate", false);
//下面NewAppActivity是我这边当前的activity名字,用的时候可以适当改成你自己的activity名
ComponentName comp= new ComponentName(NewAppActivity.this.getPackageName(),"."+NewAppActivity.this.getLocalClassName());
installShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
//指定图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(NewAppActivity.this, R.drawable.icon);
installShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
//发送广播
sendBroadcast(installShortCut);
}
最后,别忘了在清单文件中加权限: