【问题标题】:How to send a Intent with telegram如何使用电报发送 Intent
【发布时间】:2014-02-07 11:55:41
【问题描述】:

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

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

这里放上我写的代码:

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

}

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

【问题讨论】:

标签: android android-intent


【解决方案1】:

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

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

如果您发送意图:

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

它会起作用的。

如果你喜欢稍微复杂一点的系统,我推荐你使用:

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

并检查是否安装了:

/**
         * 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】:

打开电报频道:

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

    【讨论】:

    • 什么是“myId”?如果你有电话号码,你是如何得到它的?
    • 你需要使用用户名。检查我的答案
    • 没有用户名怎么办?因为有些用户不给他们的 id 用户名,所以它不是强制性的
    【解决方案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");
                }
            });
    

    【讨论】:

    • 我在 vgonisanz 的帖子中做了一点改动,并在 MainAcitvity 中使用它,它至少可以在 Android 6 上运行!
    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 2017-04-28
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多