sae*_*ati 5 android-intent telegram
我想通过android意图打开特定电报联系人的聊天页面,例如@userTest。
这是意图打开的电报片段:
Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
myIntent.setPackage("org.telegram.messenger");
activity.startActivity(myIntent);
Run Code Online (Sandbox Code Playgroud)
但是现在如何打开特定用户的聊天页面?
怎么运行的:
如果安装了电报客户端,它会构建浏览器列表以忽略它们。
如果只有一个客户端(goodresolvers == 1),那么它就会被打开。
如果没有好的客户端(goodresolvers == 0),它将回退到默认的意图处理程序。
如果您实现一个带有自定义选择器的对话框,您可以进一步改进此代码,该对话框仅允许在用户安装了多个 Telegram 客户端的情况下选择“好”客户端。
public static void openTelegram(Activity activity, String userName) {
Intent general = new Intent(Intent.ACTION_VIEW, Uri.parse("https://t.com/" + userName));
HashSet<String> generalResolvers = new HashSet<>();
List<ResolveInfo> generalResolveInfo = activity.getPackageManager().queryIntentActivities(general, 0);
for (ResolveInfo info : generalResolveInfo) {
if (info.activityInfo.packageName != null) {
generalResolvers.add(info.activityInfo.packageName);
}
}
Intent telegram = new Intent(Intent.ACTION_VIEW, Uri.parse("https://t.me/" + userName));
int goodResolver = 0;
// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = activity.getPackageManager().queryIntentActivities(telegram, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName != null && !generalResolvers.contains(info.activityInfo.packageName)) {
goodResolver++;
telegram.setPackage(info.activityInfo.packageName);
}
}
}
//TODO: if there are several good resolvers create custom chooser
if (goodResolver != 1) {
telegram.setPackage(null);
}
if (telegram.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivity(telegram);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:openTelegram(activity, "userTest");
| 归档时间: |
|
| 查看次数: |
3127 次 |
| 最近记录: |