Android 如何用电报发送意图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21627167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:50:09  来源:igfitidea点击:

How to send a Intent with telegram

androidandroid-intent

提问by uelordi

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

我正在尝试在 Java 中创建一个管理不同社交共享应用程序的类。该类基于 android 意图。

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

但是当我尝试执行 Telegram 意图时,它找不到该应用程序。

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.

我在哪里可以找到包名称?提前致谢。

回答by vgonisanz

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

所有 Android 应用程序都有一个唯一的 ID,即市场 ID。如果您查看 Google Play 或 google search market://details?id=org.telegram,它会将您发送至

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;
            }
        }

回答by irshst

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);

回答by fk_androidd

> **//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"));

回答by Nima178

  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");
            }
        });