I noti开发者_如何学JAVAce that some apps on the Android Market automatically create shortcuts on the desktop after downloading and installing onto your device, while some don't. How would I go about implementing this behavior?
Send an intent to the Launcher. Broadcast a INSTALL_SHORTCUT intent with a EXTRA_SHORTCUT_NAME and EXTRA_SHORTCUT_INTENT. The extra EXTRA_SHORTCUT_DUPLICATE can be used to help manage duplicate shortcuts being made. The details can be found in the Launcher2 project in the AOSP repository.
Please be careful with this, some users may not appreciate having a shortcut created without permission.
Here is some pseudo code:
Intent installIntent = new Intent(InstallShortcutReceiver.ACTION_CREATE_SHORTCUT);
Intent myAppIntent = new Intent(Context.getPackageContext(), MyActivity.class);
installIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, Context.getString(R.string.app_name));
installIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myAppIntent);
installIntent.putExtra(Intent.SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
Context.sendBroadcast(installIntent);
There is some more information in the Intent class as well as an alternate Intent action.
It turns out the Android Market automatically installs a desktop shortcut when you download an app. I'm noticing this default behavior on an Android 3.1 tablet with a Honeycomb (3.0+) targeted app. It seems like this isn't standard convention for apps running on previous Android versions, where explicit user input/permission is needed upon first downloading/launching the app.
I've tested this demo. And put the addShortcut() in the onCreate(). Then deploy from intelliJ, my GEL(Google Experience Launcher) do create shortcut for the first launch. I afraid it keep creating shortcut, but it seem won't create another shortcut if app launch next time. Try for yourself if you needed.
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, getString(R.string.app_name));
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);
}
adding shortcuts
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, "ApplicationName");
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);
}
for removing shortcut
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, "ApplicationName");
addIntent
.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
permissions
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission
android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
精彩评论