Search code examples
javaandroidhomescreen

Shortcut Add to Android Home Screen


How can I do something similar to Telegram and many other apps, which allow you to add an element in this case by telegram is a contact that if clicked on it opens the contact chat window.

I would like to do something like this, adding an element to the home if you click on it, allows you to do a certain operation.

But I have to open an app external to mine.

Edit:

Intent which must be called when clicking on the link on the homescreen, str name connection element.

Intent appIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://instagram.com/_u/"+str));
appIntent.setPackage("com.instagram.android");
Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/"+str));
 try {
     startActivity(appIntent);
} catch (ActivityNotFoundException ex) {
    startActivity(webIntent);
}

Edit2:

add:

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

code:

if (ShortcutManagerCompat.isRequestPinShortcutSupported(getBaseContext())) {
Intent instagramIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/_u/" + str));
instagramIntent.setPackage("com.instagram.android");
Bitmap bmp = getCroppedBitmap(bitmap);
final IconCompat icon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) ? IconCompat.createWithAdaptiveBitmap(bmp) : IconCompat.createWithBitmap(bmp);
final ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(getBaseContext(), UUID.randomUUID().toString())
                        .setShortLabel(str)
                        .setLongLabel(str)
                        .setIcon(icon)
                        .setIntent(instagramIntent)
                        .build();
ShortcutManagerCompat.requestPinShortcut(getBaseContext(), shortcut, null);
            }

Solution

  • If I understand correctly, you want to launch an app from a shortcut created by your app. Then you can do something like that :

    public void createShortCut{
        Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        shortcutintent.putExtra("duplicate", false);
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
        Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext, R.drawable.icon);
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent("com.whatsapp"));
        sendBroadcast(shortcutintent);
    }
    

    Check this

    Edit :

    Here's a best way to do it using ShortcutManagerCompat :

    fun createShortCut(){
        val str= ""
        val uri = Uri.parse("http://instagram.com/_u/"+str)
        val instagramIntent = Intent(Intent.ACTION_VIEW,uri)
        instagramIntent.setPackage("com.instagram.android")
        val icon = IconCompat.createWithResource(this,R.drawable.ic_launcher_background)
        val pinShortcutInfo = ShortcutInfoCompat.Builder(this, "shortcutID")
                    .setIcon(icon)
                    .setShortLabel("MyShortcut")
                    .setIntent(instagramIntent)
                    .build()
        ShortcutManagerCompat.requestPinShortcut(this,pinShortcutInfo,null)
    }