Android programming to add shortcuts of Short to mobile desktops of includes add delete and query

  • 2020-12-22 17:46:07
  • OfStack

This article illustrates how Android can be programmed to add a shortcut (Short) to the mobile desktop. To share for your reference, the details are as follows:

permissions

To add shortcuts to the mobile desktop, you first need to add permissions in manifest.


<!--  Add shortcuts  -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<!--  Remove shortcut  -->
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
<!--  Query shortcut  -->
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />

Add shortcuts

To add a shortcut, send a broadcast of the relevant action to the desktop application (launcher) as follows:

public static final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";

Add shortcuts:


private void addShortcut(String name) {
  Intent addShortcutIntent = new Intent(ACTION_ADD_SHORTCUT);
  //  Duplicate creation is not allowed 
  addShortcutIntent.putExtra("duplicate", false);//  The test did not determine duplicates based on the name of the shortcut 
  //  It should be based on the fast chain Intent To see if it's repeated , namely Intent.EXTRA_SHORTCUT_INTENT The field value
  //  But the name is not the same, although some phone systems will show it Toast The prompt repeats, but the fast chain is still created 
  //  When there is no space on the screen 
  //  Note: Repeat the created behavior MIUI and 3 Not so much on the star phone 1 Similarly, it seems impossible to create shortcuts repeatedly on Xiaomi 
  //  The name 
  addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
  //  icon 
  addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
      Intent.ShortcutIconResource.fromContext(MainActivity.this,
          R.drawable.ic_launcher));
  //  Setup associator 
  Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
  launcherIntent.setClass(MainActivity.this, MainActivity.class);
  launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  addShortcutIntent
      .putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
  //  Send broadcast 
  sendBroadcast(addShortcutIntent);
}

Remove shortcut

Remove the shortcut to action:

public static final String ACTION_REMOVE_SHORTCUT = "com.android.launcher.action.UNINSTALL_SHORTCUT";

Ways to remove shortcuts:


private void removeShortcut(String name) {
  // remove shortcut "Does not work on the Xiaomi system, in 3 The star can be removed 
  Intent intent = new Intent(ACTION_REMOVE_SHORTCUT);
  //  The name 
  intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
  //  Setup associator 
  Intent launcherIntent = new Intent(MainActivity.this,
      MainActivity.class).setAction(Intent.ACTION_MAIN);
  intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
  //  Send broadcast 
  sendBroadcast(intent);
}

After testing on two phones, it was found that shortcuts added to xiaomi phones could not be removed, while 3-star phones could.

Query shortcut

The method to check whether the shortcut exists was obtained from other information on the Internet, but the test query failed, and neither phone (Xiaomi, 3 stars) could be checked.

Save the code for later to see why:


private boolean hasInstallShortcut(String name) {
  boolean hasInstall = false;
  final String AUTHORITY = "com.android.launcher2.settings";
  Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
      + "/favorites?notify=true");
  //  Here is always failed to find provider info
  // com.android.launcher2.settings and com.android.launcher.settings All not line 
  Cursor cursor = this.getContentResolver().query(CONTENT_URI,
      new String[] { "title", "iconResource" }, "title=?",
      new String[] { name }, null);
  if (cursor != null && cursor.getCount() > 0) {
    hasInstall = true;
  }
  return hasInstall;
}

I hope this article has been helpful in Android programming.


Related articles: