0

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);
            }
Paul
  • 3,644
  • 9
  • 47
  • 113

2 Answers2

1

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)
}
  • I have read the code and the post you indicated but I'm not sure how to use it, createShortCut. I have edited the question by setting an example of the kind of intent that must be called when clicking on the link that is created in the homescreen. I tried to put the code you published to see if a link was created, but nothing. – Paul Oct 24 '18 at 15:43
  • I thank you for your help but it does not seem to work, I redeemed your code from hotlin to java, but it does not work, nothing is created. Is not that by chance that this code is the one used by version 8 to create shortcuts on the same app? The device I'm using is android version 7.0. – Paul Oct 24 '18 at 19:55
  • According to [this](https://developer.android.com/reference/android/support/v4/content/pm/ShortcutManagerCompat) the method requestPinShortcut should work on devices <= 25 : "On API <= 25 it creates a legacy shortcut with the provided icon, label and intent. For newer APIs it will create a ShortcutInfo object which can be updated by the app." Are sure you're using the ShortcutManagerCompat and not the ShortcutManager ? – Maxime Dupierreux Oct 24 '18 at 20:10
  • Thax, I solved. The solution put it in the post at the origin. – Paul Oct 24 '18 at 20:44
0

Not certain if I understand what you want to accomplish, but here goes. In your manifest you can add code like this

<activity
    android:name=".activityName"
    android:screenOrientation="sensorLandscape">
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

The intent-filter action.Main makes it possible to launch your activity from the home screen. So just add that filter to the activities you want to put on the home screen.

Darcia14
  • 214
  • 1
  • 13