I know it's not documented and won't work on every device, but I see more and more apps placing their shortcuts on the home screen after they got installed. Found bunch of code chunks how to do it but for me they don't fit together. This is what I got for now.
Need a permission in the manifest.
Create an Intent of activity that should be called. Ex (from cgeek):
My questions is: Where this code should go to make shortcut added after .apk installed? I tried this code in the launcher activity, it creates broken(another story) shortcut every time app starts.
回答1:
As far as I know, that's an optional feature of the Market app, not of the apps themselves. By design an application does not receive a broadcast about itself being installed. If that codes works, the soonest you can execute it is the first time the user launches the app. That said:
Do. Not. Automatically. Create. App. Shortcuts.
Ever.
Don't usurp the user's UI design.
回答2:
I agree with the currently accepted answer, that you should not do this by receiving a broadcast or at-install time. Don't do anything without user interaction or permission.
However, if you provide a button in your application, you would put this in the buttons OnClick handler. It would then add a shortcut when the user selects the "add shortcut" option.
回答3:
This can be possible just add the below code to your main activity in oncreate method
IntentHomeScreenShortCut=newIntent(getApplicationContext(),MainActivity.class);HomeScreenShortCut.setAction(Intent.ACTION_MAIN);HomeScreenShortCut.putExtra("duplicate",false);//shortcutIntent is added with addIntentIntent addIntent =newIntent(); addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,HomeScreenShortCut); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"AppName"); 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);
Add add this permission to your manifest
回答4:
I used this code to create 3 shortcuts on homepage:
IntentHomeScreenShortCut=newIntent(getApplicationContext(),MainActivity.class);HomeScreenShortCut.setAction(Intent.ACTION_MAIN);HomeScreenShortCut.putExtra("duplicate",false);//shortcutIntent is added with addIntentIntent addIntent =newIntent(); addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,HomeScreenShortCut); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"AppName"); 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);
回答5:
There are shortcomings in the same method above. Take the shortcut to the Home screen.