2016-03-01 136 views
4

我有一個android應用程序應該能夠通過按下按鈕在電報應用程序中打開聊天。
我想從我的應用程序直接打開現有的機器人聊天頁面。我的機器人有一個有效的標記。這怎麼能實現?如何從我自己的android應用程序啓動電報應用程序?

在此先感謝。

機器人的名字:@InfotechAvl_bot

機器人令牌:179284 ***********

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

希望這將幫助你http://stackoverflow.com/questions/30055201/android-send-telegram-message-to-a-specific-number –

+0

其實我不想發送消息,我只想打開機器人聊天畫面。 – BoshRa

回答

13

我解決了我的問題。 其實你要打開機器人ID與此:

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

你救了我的一天兄弟! –

+0

如何獲取朋友的用戶名? – NehaK

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

希望這對你有用。

相關問題