Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sunshirond idea, works perfect for me, and I made some changes in <a href="http://www.android10.org/index.php/articleslibraries/291-twitter-integration-in-your-android-application" rel="nofollow">http://www.android10.org/index.php/articleslibraries/291-twitter-integration-in-your-android-application</a> source code. The main activity to login and twitt will be something like that:</p> <pre><code>public class PrepareRequestTokenActivity extends Activity { final String TAG = getClass().getName(); private OAuthConsumer consumer; private OAuthProvider provider; private WebView webView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_webview); try { this.consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET); this.provider = new CommonsHttpOAuthProvider(Constants.REQUEST_URL,Constants.ACCESS_URL,Constants.AUTHORIZE_URL); } catch (Exception e) { Log.e(TAG, "Error creating consumer / provider",e); } Log.i(TAG, "Starting task to retrieve request token."); webView = (WebView) findViewById(R.id.mywebview); webView.setWebViewClient(new MyWebViewClient(getApplicationContext(), PreferenceManager.getDefaultSharedPreferences(this))); webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); WebSettings webSettings = webView.getSettings(); webSettings.setSavePassword(false); webSettings.setSupportZoom(false); webSettings.setSaveFormData(false); webSettings.setJavaScriptEnabled(true); webSettings .setCacheMode(WebSettings.LOAD_NO_CACHE); try { final String url = provider.retrieveRequestToken(consumer, Constants.OAUTH_CALLBACK_URL); webView.loadUrl(url); } catch (Exception e) { webView.clearHistory(); webView.clearFormData(); webView.clearCache(true); finish(); } } private class MyWebViewClient extends WebViewClient { private Context context; private SharedPreferences prefs; public MyWebViewClient(Context context, SharedPreferences prefs) { this.context = context; this.prefs = prefs; } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.contains(Constants.OAUTH_CALLBACK_SCHEME)) { Uri uri = Uri.parse(url); new RetrieveAccessTokenTask(context,consumer,provider,prefs).execute(uri); finish(); return true; } return false; } } public class RetrieveAccessTokenTask extends AsyncTask&lt;Uri, Void, Void&gt; { private Context context; private OAuthProvider provider; private OAuthConsumer consumer; private SharedPreferences prefs; public RetrieveAccessTokenTask(Context context, OAuthConsumer consumer,OAuthProvider provider, SharedPreferences prefs) { Log.d("hi there", "im gonna make the twitt"); this.context = context; this.consumer = consumer; this.provider = provider; this.prefs=prefs; } /** * Retrieve the oauth_verifier, and store the oauth and oauth_token_secret * for future API calls. */ @Override protected Void doInBackground(Uri...params) { final Uri uri = params[0]; final String oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER); try { provider.retrieveAccessToken(consumer, oauth_verifier); final Editor edit = prefs.edit(); edit.putString(OAuth.OAUTH_TOKEN, consumer.getToken()); edit.putString(OAuth.OAUTH_TOKEN_SECRET, consumer.getTokenSecret()); edit.commit(); String token = prefs.getString(OAuth.OAUTH_TOKEN, ""); String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""); consumer.setTokenWithSecret(token, secret); //context.startActivity(new Intent(context,AndroidTwitterSample.class)); executeAfterAccessTokenRetrieval(); Log.i(TAG, "OAuth - Access Token Retrieved"); } catch (Exception e) { Log.e(TAG, "OAuth - Access Token Retrieval Error", e); } return null; } private void executeAfterAccessTokenRetrieval() { String msg = getIntent().getExtras().getString("tweet_msg"); try { TwitterUtils.sendTweet(prefs, msg); } catch (Exception e) { Log.e(TAG, "OAuth - Error sending to Twitter", e); } } } </code></pre> <p>}</p> <p>If you have some problems make it work, mail me... may I upload the projecto to google code.</p> <p>Note- here is the XML for the webview:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;WebView android:id="@+id/mywebview" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; </code></pre> <p></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload