【问题标题】:Android launch Twitter intentAndroid 推出 Twitter 意图
【发布时间】:2014-02-01 00:28:03
【问题描述】:

我使用下面的代码通过意图启动 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 中,看看是否有错误。
  • android.content.ActivityNotFoundException:找不到明确的活动类 {com.twitter.android/com.twitter.android.PostActivity};您是否在 AndroidManifest.xml 中声明了此活动?我需要在安卓清单中添加一些东西吗???

标签: android android-intent twitter


【解决方案1】:

通常用于启动用户的提要

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

【讨论】:

    【解决方案2】:

    稍微更正(感谢 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= 不再工作了
    【解决方案3】:

    我从 2016 年开始使用com.twitter.android.composer.ComposerActivity 发布文字和图片。但前段时间开始收到用户的崩溃报告:

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

    问题是由重命名引起的
    com.twitter.android.composer.ComposerActivity

    com.twitter.composer.ComposerActivity
    在 Twitter 应用程序中。

    问题已解决,因为我将活动名称更改为 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");
    

    【讨论】:

      猜你喜欢
      • 2013-02-25
      • 1970-01-01
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 2015-01-13
      • 2014-03-29
      相关资源
      最近更新 更多