private AlertDialog mAlertDialog;
/**
* 创建快捷icon对话框
*/
private void createShortcutDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(FingerPaint.this);
dialog.setTitle(getString(R.string.shortcut_dialog_title));
dialog.setMessage(getString(R.string.shortcut_dialog_message));
dialog.setIcon(R.drawable.dialog_logo);
dialog.setCancelable(false);
dialog.setPositiveButton(getString(R.string.shortcut_dialog_ok), new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
createShortCut(R.drawable.ic_launcher,R.string.app_name,".MainActivity");//MainActivityis your launcher activity

finish();
}
});
dialog.setNegativeButton(getString(R.string.shortcut_dialog_cancel), new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

finish();
}
});
mAlertDialog = dialog.create();
mAlertDialog.show();
}

private static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";


/**
* 是否可以有多个快捷方式的副本
*/
private void createShortCut(final int iconId, final int stringId, final String activityName) {
if (activityName == null || TextUtils.isEmpty(activityName))
return;
// 创建快捷方式的Intent
Intent shortcutintent = new Intent(ACTION_INSTALL_SHORTCUT);
// 不允许重复创建
shortcutintent.putExtra(EXTRA_SHORTCUT_DUPLICATE, false);
// 需要现实的名称
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(stringId));
// 快捷图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), iconId);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// 点击快捷图片,运行的程序主入口
Intent intent2 = new Intent();
intent2.setAction("android.intent.action.VIEW");
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent2.setAction(Intent.ACTION_MAIN);
// 包名
String packageName = this.getPackageName();
intent2.setComponent(new ComponentName(packageName, packageName + activityName));
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
// 发送广播。OK
sendBroadcast(shortcutintent);

}


当然权限少不了

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

更多相关文章

  1. Android ListView异步加载网络图片
  2. Android使用HttpURLConnection显示网络图片
  3. android 加载 网络图片
  4. Android 拖拽图片
  5. Android使用URLConnection显示网络图片
  6. android中异步加载图片信息
  7. android图片拖动

随机推荐

  1. 安卓学习随笔之 android:layout_width="0
  2. android 中shape的使用
  3. Android音乐播放器系列讲解之一
  4. 【Android(安卓)NDK 开发】Ubuntu 函数库
  5. Android(安卓)sdk 1.5 android工具的用法
  6. Android(安卓)常用开发术语
  7. 解决Fetching android sdk component inf
  8. Android(安卓)RelativeLayout
  9. Android(安卓)Studio 单刷《第一行代码》
  10. Android中RelativeLayout各个属性,例如:and