2

I'm having problem when I tried to create the shortcut icon in the homescreen of the Android App programmatically.

I was able to create the shortcut icon but after that, an alert stating "application shortcut created" pops up then the application closes. I don't want the application to close and if possible I want to get rid of the alert that pops up after creating the shortcut.

How can I achieve that?

Here is my current code:

Intent targetIntent = new Intent (Intent.ACTION_MAIN);
targetIntent.setClassName (getApplicationContext(), "com.mainListActivity");
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.addFlags(Intent.FLAG_FROM_BACKGROUND);

//repeat to create is forbidden
shortcutintent.putExtra("duplicate", false);

//set the name of shortCut
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"SecureLauncher");

//set icon
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher_cloud);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

//set the application to lunch when you click the icon
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,targetIntent);
sendBroadcast(shortcutintent);
She Smile GM
  • 1,322
  • 1
  • 11
  • 33

2 Answers2

0

I figured out the the cause why the application closes it is in my manifest.xml

before i have this in my manifest: i removed the receiver part:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<receiver
    android:name=".CreateShortcut"
    android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
    <intent-filter>
        <action android:name="com.android.launcher.action.INSTALL_SHORTCUT"/>
    </intent-filter>
</receiver>

so i only have this in my manifest:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
She Smile GM
  • 1,322
  • 1
  • 11
  • 33
0

Use this code when create short cut

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

Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AIMS DOCTOR");
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);

Also you should use this on your manifest:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118