我能够在Android中发布推文,但是我想在WebView的帮助下发推文,你能不能请我在哪个WebView将重定向主要活动,用户可以编辑消息并发推特。< / p>
答案 0 :(得分: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);
}
}
答案 1 :(得分:0)
您可以使用此Java库在您自己的WebView http://twitter4j.org/en/index.html
的帮助下实现Twitter OAuth