一、在onCreat()方法中添加以下代码:
SharedPreferences preferences = getSharedPreferences("first",
Context.MODE_PRIVATE);
boolean isFirst = preferences.getBoolean("isfrist", true);
if (isFirst) {
createDeskShortCut();
}
Editor editor = preferences.edit();
editor.putBoolean("isfrist", false);
editor.commit();
二、 createDeskShortCut()方法:
public void createDeskShortCut() {
// 创建快捷方式
Intent shortcutIntent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 不允许重复创建
shortcutIntent.putExtra("duplicate", false);
// 应用名称
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
// 快捷图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(
getApplicationContext(), R.drawable.ic_launcher);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
// 点击快捷方法运行程序
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
// 发送广播
sendBroadcast(shortcutIntent);
}
三、在AndroidManifest中添加权限:
<uses-permissionandroid:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
转载于:https://blog.51cto.com/guowang1102/1377135