0

我可以简单地在 android 中发布推文,但我想在 WebView 的帮助下发布推文,您能否请我在其中 WebView 将在主要活动上重新定向,用户可以编辑消息并推文。

4

2 回答 2

2

试试这个代码

public class TwitterScreen extends Activity {

    private Twitter twitter;
    private ProgressDialog dialog;
    private WebView webView;
    private String twitterUrl;
    String twittMessage;

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.twitter_xml);

        twittMessage = "This Is Android";
        twitterUrl = twitterConnection();
        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);

        try {
            webView.loadUrl(twitterUrl);
            webView.setWebViewClient(new ContactUsWebViewClient());

        } catch (Exception e) {
            e.printStackTrace();
        }

        sendTweetByApp(twittMessage);

    }

    public String twitterConnection() {

        twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(TwitterConstants.CONSUMER_KEY,
                TwitterConstants.CONSUMER_SECRET);
        RequestToken requestToken = null;
        try {
            requestToken = twitter.getOAuthRequestToken();
        } catch (TwitterException e) {
            e.printStackTrace();
        }

        String url = requestToken.getAuthorizationURL();

        System.out.println("twitter Url load" + url);
        System.out.println("UserName");
        dialog = ProgressDialog.show(TwitterScreen.this, "", "Loading...",
                true, true);
        Thread threadHandler = new Thread() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    Thread.sleep(3000);
                    dialog.dismiss();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        threadHandler.start();

        return url;
    }

    private class ContactUsWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    public void sendTweetByApp(String message) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse("http://twitter.com/?status=" + Uri.encode(message)));
        startActivity(i);

    }

}
于 2012-10-22T13:39:58.177 回答
0

您可以使用这个 Java 库在您自己的 WebView http://twitter4j.org/en/index.html的帮助下实现 Twitter OAuth

于 2012-10-22T13:40:30.220 回答