设置桌面快捷方式

如何在程序中设置桌面快捷方式(home screen shortcut):

/**
*创建桌面图标(Home Screen Shortcut)
*
* @param context the context
* @param clz 快捷方式启动的Activity
*/

public static void makeShortcut(Context context,Class<?> clz) {
//设置快捷方式的目标Activity
//该Activity一般具有如下的设置:
// <intent-filter>
// <action android:name="android.intent.action.MAIN" />
// <category android:name="android.intent.category.LAUNCHER" />
// </intent-filter>
Intent shortcutIntent = new Intent(context,clz);
// shortcutIntent.setClassName(packageName, name);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getResources().getString(R.string.app_name));
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon) );
intent.setAction(Intent.ACTION_CREATE_SHORTCUT);
context.sendBroadcast(intent);
}

你可能感兴趣的:(设置)