skip to Main Content

I need to open Telegram from my own Android App prepopulating both the message and recipient (and not having to choose betwenn Chrome or Telegram),
I achieved the first one with this code:

        final String appName = "org.telegram.messenger";
        Intent tIntent = new Intent(Intent.ACTION_SEND);
        tIntent.setType("text/plain");
        tIntent.setPackage(appName);
        tIntent.putExtra(Intent.EXTRA_TEXT, msg);
        mUIActivity.startActivity(tIntent);

And the second one with this code:

        Intent tIntent = new Intent(Intent.ACTION_VIEW);
        tIntent.setData(Uri.parse("http://telegram.me/USERID"));
        startActivity(tIntent);

(I removed all checks like isTelegramInstalled for simplicity)

I tried to mix the two methods
adding some intent extra such as msg we get to this code that will open Telegram, in one click, with pre-populated message and recipient:

        Intent telegramIntent = new Intent(Intent.ACTION_SEND);
        tIntent.setDataAndType(Uri.parse("http://telegram.me/username"), "text/plain");
        final String appName = "org.telegram.messenger";
        tIntent.setPackage(appName);
        tIntent.putExtra(Intent.EXTRA_TEXT, "hello");
        startActivity(tIntent);

…aaand it didn’t work!

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND dat=http://telegram.me/... flg=0x1 pkg=org.telegram.messenger clip={null T:hello} (has extras) }

Any idea how to achieve this?

2

Answers


  1. In your case

    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND dat=http://telegram.me/... flg=0x1 pkg=org.telegram.messenger clip={null T:hello} (has extras) }
    

    Telegram chat activity class has a different name and you pass other activity name.

    Find which class use telegram for chat activity and pass it on set package method

    Login or Signup to reply.
  2. Please try below function for open Telegram

    public static void openTelegram(Activity activity, String userName) {
        Intent general = new Intent(Intent.ACTION_VIEW, Uri.parse("https://t.com/" + userName));
        HashSet<String> generalResolvers = new HashSet<>();
        List<ResolveInfo> generalResolveInfo = activity.getPackageManager().queryIntentActivities(general, 0);
        for (ResolveInfo info : generalResolveInfo) {
            if (info.activityInfo.packageName != null) {
                generalResolvers.add(info.activityInfo.packageName);
            }
        }
    
        Intent telegram = new Intent(Intent.ACTION_VIEW, Uri.parse("https://t.me/" + userName));
        int goodResolver = 0;
        // gets the list of intents that can be loaded.
        List<ResolveInfo> resInfo = activity.getPackageManager().queryIntentActivities(telegram, 0);
        if (!resInfo.isEmpty()) {
            for (ResolveInfo info : resInfo) {
                if (info.activityInfo.packageName != null && !generalResolvers.contains(info.activityInfo.packageName)) {
                    goodResolver++;
                    telegram.setPackage(info.activityInfo.packageName);
                }
            }
        }
        //TODO: if there are several good resolvers create custom chooser
        if (goodResolver != 1) {
            telegram.setPackage(null);
        }
        if (telegram.resolveActivity(activity.getPackageManager()) != null) {
            activity.startActivity(telegram);
        }
    } 
    

    I hope this can help You!

    Thank You.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search