I have developed android widget app and its working fine. Now my client asks that, when the user installed this app, it automatically needs to place on homescreen top position. How to do this? please help me.
-
1See the app CM flashlight it does the same thing, while the installation of the app itself the Widget and the app icon are placed on the Home screen , i am not sure how CM flshlight could do it , but i can be done for sure but i donot know how yet. – user2548816 Aug 21 '16 at 16:07
3 Answers
- Create Widget Provider Class
- Put into Manifest
- On button click which you wanna add to home
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AppWidgetManager mAppWidgetManager = getSystemService(AppWidgetManager.class);
ComponentName myProvider = new ComponentName(AddWidgetActivity.this, AppWidgetSmall.class);
Bundle b = new Bundle();
b.putString("ggg", "ggg");
if (mAppWidgetManager.isRequestPinAppWidgetSupported()) {
Intent pinnedWidgetCallbackIntent = new Intent(AddWidgetActivity.this, AppWidgetSmall.class);
PendingIntent successCallback = PendingIntent.getBroadcast(AddWidgetActivity.this, 0,
pinnedWidgetCallbackIntent, 0);
mAppWidgetManager.requestPinAppWidget(myProvider, b, successCallback);
}
}

- 11,633
- 6
- 46
- 69

- 611
- 7
- 8
As of Android O, In your app, you can create a request for the system to pin a widget onto a supported launcher.
- Create the widget in your app's manifest file
- Call the requestPinAddWidget() method
See the bottom part of this page: https://developer.android.com/preview/features/pinning-shortcuts-widgets.html

- 2,355
- 1
- 17
- 15
Refer To http://viralpatel.net/blogs/android-install-uninstall-shortcut-example/:
Android provide us an intent class com.android.launcher.action.INSTALL_SHORTCUT which can be used to add shortcuts to home screen. In following code snippet we create a shortcut of activity MainActivity with the name HelloWorldShortcut.
First we need to add permission INSTALL_SHORTCUT to android manifest xml.
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
The addShortcut() method create a new shortcut on Home screen.
private void addShortcut() {
//Adding shortcut for MainActivity
//on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
Note how we create shortcutIntent object which holds our target activity. This intent object is added into another intent as EXTRA_SHORTCUT_INTENT. Finally we broadcast the new intent. This adds a shortcut with name mentioned as EXTRA_SHORTCUT_NAME and icon defined by EXTRA_SHORTCUT_ICON_RESOURCE. Note: One thing worth noting here is when you define your activity that is invoked from shortcut, you must define android:exported=”true” attribute in tag.
An To Uninstall Shortcut from Home screen:
Similar to install, uninstalling or removing shortcut in Android uses an Intent (UNINSTALL_SHORTCUT) to perform the task. In following code we remove the shortcut added on home screen.
Again we need a permission to perform uninstall shortcut task. Add following permission to Android manifest xml.
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
The removeShortcut() method does exactly reverse of addShortcut(). Most of the code is similar except removeShortcut calls UNINSTALL_SHORTCUT intent.
private void removeShortcut() {
//Deleting shortcut for MainActivity
//on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
addIntent
.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
and you can try this demo HERE

- 1,730
- 4
- 17
- 32