Sunshirondのアイデアは私にとって完璧に機能し、http://www.android10.org/index.php/articleslibraries/291-twitter-integration-in-your-android-applicationソースコードにいくつかの変更を加えました。ログインしてツイッターする主なアクティビティは次のようになります。
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<Uri, Void, Void> {
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);
}
}
}
}
問題が発生した場合は、メールでお知らせください...プロジェクトをGoogleコードにアップロードできますか。
注-これがWebビューのXMLです。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/mywebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />