Install launcher icon in home screen once

有些话、适合烂在心里 提交于 2019-12-20 12:30:37

问题


When a user installs an Android app, a launcher icon is created in the apps menu. Many users I talk to expect that when they install an app, an icon should appear automatically on their home screen ("launch pad").

A lot of apps achieve this somehow. My preference would be to have a window appear on install asking the user "Do you want to add a shortcut?" If that's not possible, any code that auto-adds the shortcut will do.

Android gives a bunch of code here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.html It is implied that adding this code (and the related xml) to your project will do the trick. But it does not have the effect I want. It seems the code provided is passive, and I need to trigger it somehow.

So my question is:

How do I trigger the installation of a shortcut, and how do I make sure it happens only once, preferably triggered by some kind of "app install" event?

PS: A complicating factor is that I am building my app using PhoneGap, meaning the main activity is not "Activity" but "DroidGap".


回答1:


In the example, it returns the intent in setResult(...). I believe you need to run sendBroadcast(intent) to trigger installation of the shortcut.




回答2:


    Intent shortcutIntent = new Intent(getApplicationContext(), HomeScreen.class);      
    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AIMS ICD");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.aims));
    addIntent.putExtra("duplicate", false);
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);



回答3:


The class DroidGap extends Activity so you can just add in the code from the link you provided to add a shortcut.



来源:https://stackoverflow.com/questions/9452121/install-launcher-icon-in-home-screen-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!