【问题标题】:How can I post on Twitter with Intent Action_send?如何使用 Intent Action_send 在 Twitter 上发帖?
【发布时间】:2018-08-02 13:58:02
【问题描述】:

我一直在努力将文本从我的应用程序发送到 Twitter。

下面的代码可以显示蓝牙、Gmail、Facebook 和 Twitter 等应用程序列表,但是当我选择 Twitter 时,它并没有像我预期的那样预填充文本。

我知道使用 Facebook 执行此操作存在问题,但我一定做错了,它不能与 Twitter 一起使用。

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Example Text");
startActivity(Intent.createChooser(intent, "Share Text"));

【问题讨论】:

  • This solution 如果安装了官方 Twitter 应用程序,则直接启动,或者回退到使用其他能够发送推文的应用程序(例如浏览器)打开选择器。

标签: android android-intent twitter share


【解决方案1】:

我在我的代码中使用了这个 sn-p:

private void shareTwitter(String message) {
    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 {
        Intent i = new Intent();
        i.putExtra(Intent.EXTRA_TEXT, message);
        i.setAction(Intent.ACTION_VIEW);
        i.setData(Uri.parse("https://twitter.com/intent/tweet?text=" + urlEncode(message)));
        startActivity(i);
        Toast.makeText(this, "Twitter app isn't found", Toast.LENGTH_LONG).show();
    }
}

private String urlEncode(String s) {
    try {
        return URLEncoder.encode(s, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        Log.wtf(TAG, "UTF-8 should always be supported", e);
        return "";
    }
}

希望对你有帮助。

【讨论】:

  • 这种方法的问题是它只允许官方推特应用。
  • @StackOverflowed 但我相信只有少数经常使用 twitter 的用户没有使用该应用程序来分享内容。
  • 此代码抛出安全异常:在 Twitter 上共享时拒绝权限。
【解决方案2】:

您只需打开带有文本的 URL,Twitter 应用程序就会执行此操作。 ;)

String url = "http://www.twitter.com/intent/tweet?url=YOURURL&text=YOURTEXT";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

如果没有找到推特应用,它也会打开浏览器登录推文。

【讨论】:

  • 谢谢,这很有用!我知道有一种方法可以显示应用程序列表(twitter、fb、gmail 等)并且 twitter 可以工作,因为我在其他应用程序(日志收集器等)中看到了它。我真的不想通过多个应用程序共享多个按钮:s
  • 这并没有回答问题,它只是提供了一种使用 Twitter 发布文本的方式。我正在寻找 Twitter 的结果,当它作为 Intent Action_Send 下的一个选项出现时接收文本。
  • 谢谢,伙计,您的回答很有帮助,请忽略讨厌的 cmets
【解决方案3】:

试试这个,我用过,效果很好

  Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/intent/tweet?text=...."));
  startActivity(browserIntent);         

【讨论】:

    【解决方案4】:

    首先,您必须检查设备上是否安装了 twitter 应用程序,然后在 twitter 上分享文本:

    try
        {
            // Check if the Twitter app is installed on the phone.
            getActivity().getPackageManager().getPackageInfo("com.twitter.android", 0);
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setClassName("com.twitter.android", "com.twitter.android.composer.ComposerActivity");
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, "Your text");
            startActivity(intent);
    
        }
        catch (Exception e)
        {
            Toast.makeText(getActivity(),"Twitter is not installed on this device",Toast.LENGTH_LONG).show();
    
        }
    

    【讨论】:

    【解决方案5】:

    textimageTwitter 上分享,下面是更多控制版本的代码,您可以添加更多方法与@987654321 分享@,Facebook ...这是官方应用,如果应用不存在则不打开浏览器。

    public class IntentShareHelper {
    
        public static void shareOnTwitter(AppCompatActivity appCompatActivity, String textBody, Uri fileUri) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.setPackage("com.twitter.android");
            intent.putExtra(Intent.EXTRA_TEXT,!TextUtils.isEmpty(textBody) ? textBody : "");
    
            if (fileUri != null) {
                intent.putExtra(Intent.EXTRA_STREAM, fileUri);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setType("image/*");
            }
    
            try {
                appCompatActivity.startActivity(intent);
            } catch (android.content.ActivityNotFoundException ex) {
                ex.printStackTrace();
                showWarningDialog(appCompatActivity, appCompatActivity.getString(R.string.error_activity_not_found));
            }
        }
    
        public static void shareOnWhatsapp(AppCompatActivity appCompatActivity, String textBody, Uri fileUri){...}
    
        private static void showWarningDialog(Context context, String message) {
            new AlertDialog.Builder(context)
                    .setMessage(message)
                    .setNegativeButton(context.getString(R.string.close), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setCancelable(true)
                    .create().show();
        }
    }
    

    要从文件中获取 Uri,请使用以下类:

    public class UtilityFile {
        public static @Nullable Uri getUriFromFile(Context context, @Nullable File file) {
            if (file == null)
                return null;
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                try {
                    return FileProvider.getUriForFile(context, "com.my.package.fileprovider", file);
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            } else {
                return Uri.fromFile(file);
            }
        }
    
        // Returns the URI path to the Bitmap displayed in specified ImageView       
        public static Uri getLocalBitmapUri(Context context, ImageView imageView) {
            Drawable drawable = imageView.getDrawable();
            Bitmap bmp = null;
            if (drawable instanceof BitmapDrawable) {
                bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
            } else {
                return null;
            }
            // Store image to default external storage directory
            Uri bmpUri = null;
            try {
                // Use methods on Context to access package-specific directories on external storage.
                // This way, you don't need to request external read/write permission.
                File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
                FileOutputStream out = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.close();
    
                bmpUri = getUriFromFile(context, file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bmpUri;
        }    
    }
    

    如需编写FileProvider,请使用此链接:https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-14
      相关资源
      最近更新 更多