How to Create a shortcut on android homescreen

Related imageCreating a shortcut on Android home screen is fairly simple.So let's get going.
We begin by creating an intent giving the target activity which will open when the shortcut is pressed


Intent target = new Intent(getApplicationContext(), Jumpto.class);

>>then we create another intent that is broadcast across the system :

Intent shout = new Intent();
shout.putExtra(Intent.EXTRA_SHORTCUT_INTENT, target);
shout.putExtra(Intent.EXTRA_SHORTCUT_NAME, "shortcut");
shout.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon));

shout.setAction("com.android.launcher.action.INSTALL_SHORTCUT");

getApplicationContext().sendBroadcast(shout);



>>this intent carries all information regarding the shortcut like the name, the icon, and the activity to which it has to jump.when the broadcast is sent the launcher apps receive it and create a shortcut based on the details you specify.

>>before running add this permission to the manifest file:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/


>>and if you want to launch any other activity other than main activity then find that activity in the 
manifest file and add this statement

android:exported="true
//for example
  <activity android:name=".Jumpto"
          android:exported="true"   ></activity>



Comments

Popular posts from this blog

How to load image from URL

How to add a library to Android Studio

How to add new theme in Android studio