java 如何从我自己的 android 应用程序启动 Telegram 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35721942/
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
How to launch Telegram app from my own android application?
提问by BoshRa
I have an android app that should be able to open a chat in the telegram app by pressing a button.
I want to open an existing robot chat page DIRECTLY from my app. I have a valid token for my robot. How can this be achieved?
我有一个 android 应用程序,它应该能够通过按下按钮在电报应用程序中打开聊天。
我想直接从我的应用程序打开一个现有的机器人聊天页面。我的机器人有一个有效的令牌。如何做到这一点?
Thanks in advance.
提前致谢。
robot name : @InfotechAvl_bot
机器人名称:@InfotechAvl_bot
robot token: 179284***********
机器人令牌:179284***********
//-------------
case ContentFragment.lMenuTelegram:
Intent LaunchIntent=getPackageManager().getLaunchIntentForPackage("org.telegram.messenger");
startActivity(LaunchIntent);
break;
回答by BoshRa
I solved My problem. actually you have to open robot id with this :
我解决了我的问题。实际上你必须用这个打开机器人ID:
Intent telegram = new Intent(Intent.ACTION_VIEW , Uri.parse("https://telegram.me/InfotechAvl_bot"));
startActivity(telegram);
回答by Mayank Bhatnagar
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;
}
}
Hope this works for you.
希望这对你有用。
回答by Diako Hasani
for Instagram and Telegram and WhatsApp you can use this methods
对于 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){
}
}