如何从我自己的 android 应用程序启动 Telegram 应用程序?

How to launch Telegram app from my own android application?

我有一个 android 应用程序,它应该能够通过按一个按钮在电报应用程序中打开聊天。
我想直接从我的应用程序打开现有的机器人聊天页面。我的机器人有一个有效的令牌。如何实现?

提前致谢。

机器人名称:@InfotechAvl_bot

机器人令牌:179284***********

   //-------------
    case ContentFragment.lMenuTelegram:
     Intent LaunchIntent=getPackageManager().getLaunchIntentForPackage("org.telegram.messenger");
     startActivity(LaunchIntent);
            break;

如果您喜欢稍微复杂一点的系统,我建议您使用:

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

希望这对你有用。

要解决此问题,您必须使用以下命令打开机器人 ID:

Intent telegram = new Intent(Intent.ACTION_VIEW , Uri.parse("https://telegram.me/InfotechAvl_bot"));
startActivity(telegram);

对于 Instagram、Telegram 和 WhatsApp,您可以使用此方法

 void getTelegram(){

        try {
            Intent telegramIntent = new Intent(Intent.ACTION_VIEW);
            telegramIntent.setData(Uri.parse("https://telegram.me/diako099"));
            startActivity(telegramIntent);
        } catch (Exception e) {
            // show error message
        }
    }

    void getInstagram(){
        try {
            Intent instagramIntent = new Intent(Intent.ACTION_VIEW);
            instagramIntent.setData(Uri.parse("https://www.instagram.com/diako_hasani99/"));
            startActivity(instagramIntent);
        } catch (Exception e) {
            // show error message
        }

    }

    void getWhatsApp(){
        try{

            String trimToNumner = "+989180074693"; //10 digit number
            Intent intent = new Intent ( Intent.ACTION_VIEW );
            intent.setData ( Uri.parse ( "https://wa.me/" + trimToNumner + "/?text=" + "" ) );
            startActivity ( intent );

        }catch (Exception e){

        }
    }