- 添加桌面快捷方式
/**
* 创建快捷图标
*/
private void installShortCut() {
if (SharedPreferencesUtil.getBooleanConfig(this,
Constants.APP_SHAREDPREFERENCE_NAME, Context.MODE_PRIVATE,
"isCreateShortcut", false))
return;
SharedPreferencesUtil.setBooleanConfig(this,
Constants.APP_SHAREDPREFERENCE_NAME, Context.MODE_PRIVATE,
"isCreateShortcut", true);
Intent intent = new Intent();
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
try {
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getPackageManager().getApplicationLabel(
getPackageManager().getApplicationInfo(getPackageName(),
PackageManager.GET_META_DATA)).toString());
} catch (NameNotFoundException e) {
e.printStackTrace();
}
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory
.decodeResource(getResources(), R.drawable.ic_launcher));
Intent shortcutIntent = new Intent();
shortcutIntent.setAction("android.intent.action.MAIN");
shortcutIntent.addCategory("android.intent.category.LAUNCHER");
shortcutIntent.setClassName(getPackageName(),
"com.johnny.androiddemos.MainActivity");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
sendBroadcast(intent);
Toast.makeText(this, "创建桌面快捷方式成功", Toast.LENGTH_SHORT).show();
}
- 加权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>