How to send a Intent with telegram

2019-01-17 12:23发布

问题:

I am trying to create a class in java which manages different social sharing apps. The class is based on android intents.

but when I try to execute Telegram intent, it doesn't find the app.

Here I put the code I have written:

public void shareTelegram(String message)
{
            Intent waIntent = new Intent(Intent.ACTION_SEND);
            waIntent.setType("text/plain");
            waIntent.setPackage("com.telegram");
            if (waIntent != null) {
                waIntent.putExtra(Intent.EXTRA_TEXT, message);//
                _androidActivity.startActivity(Intent.createChooser(waIntent, "Share with"));
            } 
            else 
            {
                Toast.makeText(_androidActivity.getApplicationContext(), "Telegram is not installed", Toast.LENGTH_SHORT).show();
            }

}

Where could I find the package name? Thanks in advance.

回答1:

All Android app have an unique ID, market ID. If you look into Google Play or google search market://details?id=org.telegram, It send you to

https://play.google.com/store/apps/details?id=org.telegram.messenger

If you send the intent with:

waIntent.setPackage("org.telegram.messenger");

It will work.

If you prefer a little bit complex system I recommend you to use:

/**
     * Intent to send a telegram message
     * @param msg
     */
    void intentMessageTelegram(String msg)
    {
        final String appName = "org.telegram.messenger";
        final boolean isAppInstalled = isAppAvailable(mUIActivity.getApplicationContext(), appName);
        if (isAppInstalled) 
        {
            Intent myIntent = new Intent(Intent.ACTION_SEND);
            myIntent.setType("text/plain");
            myIntent.setPackage(appName);
            myIntent.putExtra(Intent.EXTRA_TEXT, msg);//
            mUIActivity.startActivity(Intent.createChooser(myIntent, "Share with"));
        } 
        else 
        {
            Toast.makeText(mUIActivity, "Telegram not Installed", Toast.LENGTH_SHORT).show();
        }
    }

And check if is installed with:

/**
         * Indicates whether the specified app ins installed and can used as an intent. This
         * method checks the package manager for installed packages that can
         * respond to an intent with the specified app. If no suitable package is
         * found, this method returns false.
         *
         * @param context The application's environment.
         * @param appName The name of the package you want to check
         *
         * @return True if app is installed
         */
        public static boolean isAppAvailable(Context context, String appName) 
        {
            PackageManager pm = context.getPackageManager();
            try 
            {
                pm.getPackageInfo(appName, PackageManager.GET_ACTIVITIES);
                return true;
            } 
            catch (NameNotFoundException e) 
            {
                return false;
            }
        }


回答2:

For opening telegram channel :

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://telegram.me/shes_ir"));
final String appName = "org.telegram.messenger";
try {
    if (isAppAvailable(mainActivity.getApplicationContext(), appName))
        i.setPackage(appName);
} catch (PackageManager.NameNotFoundException e) {}
mainActivity.startActivity(i);


回答3:

> **//open telegram directly without intent to specify id.**


 Intent telegram = new Intent(android.content.Intent.ACTION_SEND);
     telegram.setData(Uri.parse("http://telegram.me/myId"));
     telegram.setPackage("org.telegram.messenger");
     Test.this.startActivity(Intent.createChooser(telegram, "Share with"));


回答4:

  void intentMessageTelegram(String msg)
    {
        final String appName = "org.telegram.messenger";
        final boolean isAppInstalled = isAppAvailable(this.getApplicationContext(), appName);
        if (isAppInstalled)
        {
            Intent myIntent = new Intent(Intent.ACTION_SEND);
            myIntent.setType("text/plain");
            myIntent.setPackage(appName);
            myIntent.putExtra(Intent.EXTRA_TEXT, msg);//
            this.startActivity(Intent.createChooser(myIntent, "Share with"));
        }
        else
        {
            Toast.makeText(this, "Telegram not Installed", Toast.LENGTH_SHORT).show();
        }
    }
    public static boolean isAppAvailable(Context context, String appName)
    {
        PackageManager pm = context.getPackageManager();
        try
        {
            pm.getPackageInfo(appName, PackageManager.GET_ACTIVITIES);
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }

  btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                intentMessageTelegram("Hi");
            }
        });