Добавление/удаление ярлыка на домашний экран
- public void addShortcut(Context ctx, Class startClass) {
- if (startClass==null || ctx == null) return;
- // Adding shortcut on Home screen
- Intent shortcutIntent = new Intent(ctx, startClass);
- shortcutIntent.setAction(Intent.ACTION_MAIN);
- Intent addIntent = new Intent();
- addIntent.putExtra("duplicate", false);
- addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
- addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, ctx.getString(R.string.app_name));
- addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
- Intent.ShortcutIconResource.fromContext(ctx, R.drawable.ic_launcher));
- addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
- ctx.sendBroadcast(addIntent);
- }
- public void removeShortcut(Context ctx, Class startClass) {
- if (startClass==null || ctx == null) return;
- // Deleting shortcut for MainActivity on Home screen
- Intent shortcutIntent = new Intent(ctx, startClass);
- shortcutIntent.setAction(Intent.ACTION_MAIN);
- Intent addIntent = new Intent();
- addIntent.putExtra("duplicate", false);
- addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
- addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, ctx.getString(R.string.app_name));
- addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
- Intent.ShortcutIconResource.fromContext(ctx, R.drawable.ic_launcher));
- addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
- ctx.sendBroadcast(addIntent);
- }
Нужны права
- <uses-permission
- android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
- <uses-permission
- android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />