Android 启动 Twitter 意图 [英] Android launch Twitter intent

查看:31
本文介绍了Android 启动 Twitter 意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码通过 Intent 启动 Twitter,但它不起作用.我在手机上安装了推特应用.

I used below code for launching Twitter through intent but it's not working. I have twitter app installed on my phone.

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Content to share");
PackageManager pm = contexto.getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
    if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) {
        final ActivityInfo activity = app.activityInfo;
        final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
        shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        shareIntent.setComponent(name);
        contexto.startActivity(shareIntent);
        break;
    }
}

尝试调用活动时出现异常:

Getting exception when I try to calling the activity:

android.content.ActivityNotFoundException:无法找到明确的活动类 {com.twitter.android/com.twitter.android.PostActivity};您是否在 AndroidManifest.xml 中声明了此活动?

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.twitter.android/com.twitter.android.PostActivity}; have you declared this activity in your AndroidManifest.xml?

推荐答案

通常用于启动用户的提要

Typically for launching a user's feed

Intent intent = null;
try {
    // get the Twitter app if possible
    this.getPackageManager().getPackageInfo("com.twitter.android", 0);
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=USERID"));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch (Exception e) {
    // no Twitter app, revert to browser
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/USERID_OR_PROFILENAME"));
}
this.startActivity(intent);

用于发布意图

Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "This is a Test.");
tweetIntent.setType("text/plain");

PackageManager packManager = getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(tweetIntent,  PackageManager.MATCH_DEFAULT_ONLY);

boolean resolved = false;
for(ResolveInfo resolveInfo: resolvedInfoList){
    if(resolveInfo.activityInfo.packageName.startsWith("com.twitter.android")){
        tweetIntent.setClassName(
            resolveInfo.activityInfo.packageName, 
            resolveInfo.activityInfo.name );
        resolved = true;
        break;
    }
}
if(resolved){
    startActivity(tweetIntent);
}else{
    Toast.makeText(this, "Twitter app isn't found", Toast.LENGTH_LONG).show();
}

这篇关于Android 启动 Twitter 意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆