android programming implements a way to create shortcuts for a program

  • 2020-10-23 20:17:00
  • OfStack

This article gives an example of how an android programming implementation creates a shortcut for a program. To share for your reference, the details are as follows:


/**
*  Create a desktop shortcut for your program 
*/
private void addShortcut(){
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// The name of the shortcut 
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
shortcut.putExtra("duplicate", false); // Duplicate creation is not allowed 
// Specify the current Activity Object launched for the shortcut :  Such as 
//com.everest.video.VideoPlayer
// Pay attention to : ComponentName The first 2 The parameters must be marked with a dot (.) , otherwise the shortcut cannot start the corresponding program 
ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
// Shortcut icon 
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcut);
}

I hope this article has been helpful in Android programming.


Related articles: