我怎样才能在Twitter上发布Intent Action_send?

use*_*361 12 twitter android share android-intent

我一直在努力将文本从我的应用程序发送到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"));
Run Code Online (Sandbox Code Playgroud)

sab*_*dow 49

我在我的代码上使用此代码段:

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 "";
    }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.

  • 这种方法的问题是它只允许官方Twitter应用程序. (2认同)

Luk*_*sen 17

你可以简单地用文本打开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);
Run Code Online (Sandbox Code Playgroud)

如果找不到推特应用程序,它也会打开浏览器登录推文.

  • 这不回答问题,它只是提供了一种使用Twitter发布文本的方法.我正在寻找Twitter在作为Intent Action_Send下的选项出现时接收文本的结果. (2认同)

Amt*_*t87 8

试试这个,我用它并且效果很好

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


小智 8

首先,您必须检查设备上是否安装了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();

    }
Run Code Online (Sandbox Code Playgroud)