I want to add a shortcut to the app to the Android home screen.

Asked 2 years ago, Updated 2 years ago, 107 views

I'd like to create a shortcut to the app on the Android home screen using Java's program, is it possible?

android

2022-09-30 21:21

1 Answers

There was an article on how to create a shortcut on the home screen, so I'll introduce it to you.

1. Add permissions to the manifest

<uses-permission android: name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

2. Implement the following code

/**Create app shortcuts*/
private void makeAppShortCut(Contextcon){
    // Create Intent to launch applications
    // This time, I started this application (ShortCutTestActivity).
    Intent targetIntent= new Intent(Intent.ACTION_MAIN);
    targetIntent.setClassName(con, "jp.test.shortcut.ShortCutTestActivity");

    // The shortcut is titled "APPSShortCut"
    // Use pre-created tips_icon.png for icons
    makeShortCut(con, targetIntent, "APPSShortCut", R.drawable.tips_icon);
}
/** Create Shortcut*/
private void makeShortCut(Context con, Intent targetIntent, String title, inticonResource) {
    // Intent for requesting shortcut creation
    Intent=new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

    // Specify Intent to launch when tapping shortcut
    int.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);

    // Specify shortcut icons and titles
    Parcelable icon=Intent.ShortcutIconResource.fromContext(con,iconResource);
    int.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    int.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);

    // Using BroadCast, Ask the System to Create Shortcuts
    con.sendBroadcast(intent);
}

Also, if scheme is configured in the manifest with intent-filter
You will be able to open the application in the URL, so
You can also implement these features by creating a web link.


2022-09-30 21:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.