android tips for creating desktop shortcuts for applications
- 2020-05-07 20:25:09
- OfStack
After we developed 1 software, it will be difficult to turn over if there are too many software installed on the phone. Therefore, it is good to have a shortcut on the main page. The following is the detailed code:
Then add the name of the above method to the onCreate() method:
Finally, add the permission to create shortcuts in AndroidManifest.xml:
/**
* Create a desktop shortcut
*/
private void createShortcut() {
SharedPreferences setting = getSharedPreferences("silent.preferences", 0);
// Judge whether or not 1 Start the application a second time (default is true )
boolean firstStart = setting.getBoolean("FIRST_START", true);
// The first 1 Create a desktop shortcut on the second boot
if (firstStart) {
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_name2));
// Duplicate creation is not allowed
shortcut.putExtra("duplicate", false);
// The launch object for the specified shortcut
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.zhangxy);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
// A radio
sendBroadcast(shortcut);
// Will be the first 1 The id for the second boot is set to false
Editor editor = setting.edit();
editor.putBoolean("FIRST_START", false);
// Submit set
editor.commit();
}
}
Then add the name of the above method to the onCreate() method:
// After installation the first 1 Create a desktop shortcut on the second boot
createShortcut();
Finally, add the permission to create shortcuts in AndroidManifest.xml:
<!-- Permission to create a desktop shortcut -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />