如何发送电报用一个意向 [英] How to send a Intent with telegram
问题描述
我想创建一个类在Java中,其管理的不同社交共享应用程序。类是基于在Android的意图。
I am trying to create a class in java which manages different social sharing apps. The class is based on android intents.
但是当我尝试执行电报的意图,它没有找到该应用程序。
but when I try to execute Telegram intent, it doesn't find the app.
下面我把code我写了:
Here I put the code I have written:
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();
}
}
去哪儿包的名称? 先谢谢了。
Where could I find the package name? Thanks in advance.
推荐答案
所有的Android应用程序有一个唯一的ID,市场ID。如果你到谷歌播放或谷歌搜索市场://细节ID = org.telegram,它送你
All Android app have an unique ID, market ID. If you look into Google Play or google search market://details?id=org.telegram, It send you to
https://play.google.com/store/apps/details?id=org.telegram.messenger
如果您发送的意图有:
waIntent.setPackage("org.telegram.messenger");
这会工作。
如果您preFER一点点复杂的系统,我建议你使用:
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();
}
}
和检查是否安装:
/**
* 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;
}
}
这篇关于如何发送电报用一个意向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!