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

时间:2016-03-01 11:41:11

标签: java android launch telegram

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

提前致谢。

机器人名称:@InfotechAvl_bot

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

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

3 个答案:

答案 0 :(得分:15)

我解决了我的问题。 实际上你必须用这个打开机器人id:

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

答案 1 :(得分:3)

如果您更喜欢有点复杂的系统,我建议您使用:

/** 
     * 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 :(得分:0)

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

 void GetTelegram(){

        try {
            Intent telegramIntent = new Intent(Intent.ACTION_VIEW);
            telegramIntent.setData(Uri.parse("http://telegram.me/Id User"));
            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/mafia_sna"));
            startActivity(instagramIntent);
        } catch (Exception e) {
            // show error message
        }

    }

    void GetWhatsApp(){
        try{

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

        }catch (Exception e){

        }
    }