Friday 21 June 2013

Create Shortcut of your application on Startup in android

Hi friends this tutorial is for android developers those who want to create shortcut of their costume application on home screen
The Tutorial is Given below

Open your Launcher Activity
&
Create a Shared preference this to avoid the duplication of shortcuts

public class Splash extends Activity{

SharedPreferences preferences;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);

preferences = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
        Editor editor = preferences.edit();

        boolean status = preferences.getBoolean("shortcut", false);
                if (status) {
                    } else {
            Intent shortcutIntent = new Intent(getApplicationContext(),
                    Splash.class);

            shortcutIntent.setAction(Intent.ACTION_MAIN);

            Intent addIntent = new Intent();

            addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcutname"); //
            addIntent.putExtra("duplicate", false);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                    Intent.ShortcutIconResource.fromContext(
                            getApplicationContext(), R.drawable.ic_launcher));

            addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); //
            addIntent.putExtra("duplicate", false);
            getApplicationContext().sendBroadcast(addIntent);

            editor.putBoolean("shortcut", true);
            editor.commit();
        }

}
}


copy this code to your activity class and enjoy

Dont forget to add permission
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 


Thank you

please comment here if any error occurs

No comments:

Post a Comment