安卓如何启动 Twitter 意图。

22

我使用以下代码通过意图来启动 Twitter,但它无法工作。 我的手机已安装 Twitter 应用程序。

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

尝试调用活动时遇到异常:

android.content.ActivityNotFoundException: 找不到显式活动类 {com.twitter.android/com.twitter.android.PostActivity};您在AndroidManifest.xml中声明了此活动吗?


你是否遇到了任何错误或异常?尝试将你的代码放在try catch中,看看是否有错误。 - Dev01
android.content.ActivityNotFoundException: 找不到明确的活动类 {com.twitter.android/com.twitter.android.PostActivity};您是否已在AndroidManifest.xml中声明此活动?我需要在Android清单文件中加入些什么吗? - user3065901
3个回答

61
通常用于启动用户的动态。
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();
}

15

稍微修改了一个用户的Feed意图(感谢Taranfx),将user_id改为screen_name:


    public static void startTwitter(Context context) {

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

就是这样!旧的 ?userId= 不再起作用了。 - Quentin Klein

-1
我自2016年以来一直使用com.twitter.android.composer.ComposerActivity来发布文本和图像。但是最近一段时间开始收到用户的崩溃报告:

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

这个问题是由于Twitter应用程序内部将
com.twitter.android.composer.ComposerActivity
重命名为
com.twitter.composer.ComposerActivity
所致。
问题已经得到解决,因为我将活动名称更改为com.twitter.composer.ComposerActivity
我使用以下代码将图像与文本发布到Twitter:
ShareCompat.IntentBuilder.from(activity)
    .setText(getTextToShare())
    .setStream(getImageUriToShare())
    .setType("image/png")
    .getIntent().addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    .setClassName("com.twitter.android", "com.twitter.composer.ComposerActivity");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接