Wednesday, February 25, 2015

ANDROID code to create Shortcut Icon of App

Add Permisssion
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />   

private void createShortcut() {
        Log.d(CLASS, "Method called createShortcut()");

        SharedPreferences prefs = getApplicationContext().getSharedPreferences(
                "shortcut_created", 0);
        SharedPreferences.Editor editor = prefs.edit();
        if (!prefs.getBoolean("shortcut_created", false)) {
            Intent shortcutIntent = new Intent(getApplicationContext(),
                    WelcomeActivity.class);
            shortcutIntent.setAction(Intent.ACTION_MAIN);
            Intent addIntent = new Intent();
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "rVoc");
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                    Intent.ShortcutIconResource.fromContext(
                            getApplicationContext(),
                            R.drawable.rvoc_img));
            addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            getApplicationContext().sendBroadcast(addIntent);
            editor.putBoolean("shortcut_created", true);
            editor.commit();
            Log.e(CLASS, "Short Created Successfully");
        } else {
            Log.e(CLASS, "Shorrt Cut not created");
        }
    }

No comments:

Post a Comment