not much done, however im alot closer to haveing twitter intergration finished.
i have made a java se twitter app to learn the api, and started the porting over to android. right now the issue im having is that the onActivityResult() is not being called at the right time.
This commit is contained in:
@@ -6,13 +6,14 @@
|
||||
*/
|
||||
package com.TwentyCodes.android.TravelPost;
|
||||
|
||||
import twitter4j.TwitterException;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.location.Location;
|
||||
import android.os.PowerManager;
|
||||
import android.os.PowerManager.WakeLock;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* The broadcast receiver that works with LocationService.java
|
||||
@@ -44,8 +45,12 @@ public class LocationReceiver extends BroadcastReceiver{
|
||||
*/
|
||||
private void onLocationUpdate(Location location) {
|
||||
//TODO something with the location i.e. report location to social services like twitter, ect...
|
||||
SharedPreferences shared_prefs = mContext.getSharedPreferences(TravelPost.SETTINGS, Context.MODE_PRIVATE);
|
||||
TravelPost.tweet(shared_prefs.getString(TravelPost.TWITTER_AUTH_CODE, ""), "yellow_jk", location.toString());
|
||||
try {
|
||||
TravelPost.tweet(location.toString());
|
||||
} catch (TwitterException e) {
|
||||
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
removeWakeLock();
|
||||
|
||||
@@ -6,8 +6,12 @@
|
||||
*/
|
||||
package com.TwentyCodes.android.TravelPost;
|
||||
|
||||
import winterwell.jtwitter.OAuthSignpostClient;
|
||||
import winterwell.jtwitter.Twitter;
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.TwitterException;
|
||||
import twitter4j.TwitterFactory;
|
||||
import twitter4j.conf.ConfigurationBuilder;
|
||||
import twitter4j.http.AccessToken;
|
||||
import twitter4j.http.RequestToken;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
@@ -15,6 +19,8 @@ import android.os.Bundle;
|
||||
import android.preference.Preference;
|
||||
import android.preference.Preference.OnPreferenceClickListener;
|
||||
import android.preference.PreferenceActivity;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* Main activity for the Travel Post widget.
|
||||
@@ -24,13 +30,15 @@ import android.preference.PreferenceActivity;
|
||||
public class TravelPost extends PreferenceActivity implements OnPreferenceClickListener {
|
||||
|
||||
private PostMortemReportExceptionHandler mExceptionReport = new PostMortemReportExceptionHandler(this);
|
||||
private RequestToken requestToken;
|
||||
private static SharedPreferences shared_prefs;
|
||||
public static final String SETTINGS = "settings";
|
||||
private static final int TWITTER_AUTH_REQUEST_CODE = 0;
|
||||
public static final String TWITTER_AUTH_CODE = "twitter";
|
||||
private static final String TC_TEST_OAUTH_KEY = "SSO8Vpy2Ecmc3BjvtwoRw";
|
||||
private static final String TC_TEST_OAUTH_SECRET = "aa6t6N0wlenvbWzU7fZbrhgzmEmUl5s7zGRbbKoU";
|
||||
|
||||
public static final String TWITTER_AUTH_TOKEN = "twitter_token";
|
||||
public static final String TWITTER_AUTH_SECRET = "twitter_secret";
|
||||
private static final String TC_TEST_OAUTH_KEY = "kmmcF8rjO4Aj9W4g8ao8ow";
|
||||
private static final String TC_TEST_OAUTH_SECRET = "5pgeFWHd7dkde284ed7NsRuIVsyeOvioXR0tVxp9K8";
|
||||
private static final String TAG = "TravelPost";
|
||||
|
||||
/**
|
||||
* called when the activity is first created
|
||||
@@ -45,9 +53,7 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL
|
||||
mExceptionReport.run();
|
||||
Thread.setDefaultUncaughtExceptionHandler(mExceptionReport);
|
||||
|
||||
// set shared_prefs name
|
||||
getPreferenceManager().setSharedPreferencesName(SETTINGS);
|
||||
shared_prefs = getPreferenceManager().getSharedPreferences();
|
||||
shared_prefs = getSharedPreferences(SETTINGS, 0);
|
||||
|
||||
// load preferences xml
|
||||
this.addPreferencesFromResource(R.xml.settings);
|
||||
@@ -56,9 +62,16 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
OAuthSignpostClient client = new OAuthSignpostClient(TC_TEST_OAUTH_KEY, TC_TEST_OAUTH_SECRET, "oob");
|
||||
this.startActivityForResult(new Intent(this, WebAuth.class).putExtra(WebAuth.AUTH_URL, client.authorizeUrl().toString()), TWITTER_AUTH_REQUEST_CODE);
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
Twitter twitter = new TwitterFactory().getInstance();
|
||||
twitter.setOAuthConsumer(TC_TEST_OAUTH_KEY, TC_TEST_OAUTH_SECRET);
|
||||
try {
|
||||
requestToken = twitter.getOAuthRequestToken();
|
||||
this.startActivityForResult(new Intent(this, WebAuth.class).putExtra(WebAuth.AUTH_URL, requestToken.getAuthorizationURL()), TWITTER_AUTH_REQUEST_CODE);
|
||||
} catch (TwitterException e) {
|
||||
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -70,29 +83,47 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL
|
||||
*/
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
Log.v(TAG, "onActivityResult()");
|
||||
Log.d(TAG, "Request code:"+ requestCode);
|
||||
if(data != null)
|
||||
Log.d(TAG, data.getStringExtra(WebAuth.AUTH_CODE));
|
||||
else
|
||||
Log.e(TAG, "WebAuth result was null!!!");
|
||||
if(resultCode == Activity.RESULT_OK)
|
||||
switch(requestCode){
|
||||
case TWITTER_AUTH_REQUEST_CODE:
|
||||
shared_prefs.edit().putString(TWITTER_AUTH_CODE, data.getStringExtra(WebAuth.AUTH_CODE)).commit();
|
||||
|
||||
//FOR TESTING
|
||||
// OAuthSignpostClient client = new OAuthSignpostClient(TC_TEST_OAUTH_KEY, TC_TEST_OAUTH_SECRET, "oob");
|
||||
// client.setAuthorizationCode(data.getStringExtra(WebAuth.AUTH_CODE));
|
||||
// Twitter twitter = new Twitter("yellow_jk", client);
|
||||
// // Optional: store the authorisation token details
|
||||
// Object accessToken = client.getAccessToken();
|
||||
//
|
||||
// // use the API!
|
||||
// twitter.setStatus("test");
|
||||
try {
|
||||
AccessToken accessToken = null;
|
||||
Twitter twitter = new TwitterFactory().getInstance();
|
||||
accessToken = twitter.getOAuthAccessToken(requestToken, data.getStringExtra(WebAuth.AUTH_CODE));
|
||||
Log.d(TAG, accessToken.getToken());
|
||||
Log.d(TAG, accessToken.getTokenSecret());
|
||||
shared_prefs.edit().putString(TWITTER_AUTH_TOKEN, accessToken.getToken()).commit();
|
||||
shared_prefs.edit().putString(TWITTER_AUTH_SECRET, accessToken.getTokenSecret()).commit();
|
||||
|
||||
} catch (TwitterException e) {
|
||||
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG);
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void tweet(String authCode, String userName, String msg){
|
||||
OAuthSignpostClient client = new OAuthSignpostClient(TC_TEST_OAUTH_KEY, TC_TEST_OAUTH_SECRET, "oob");
|
||||
Twitter jtwit = new Twitter(userName, client);
|
||||
client.setAuthorizationCode(authCode);
|
||||
jtwit.setStatus(msg);
|
||||
/**
|
||||
* posts a tweet to twitter
|
||||
* @param msg
|
||||
* @throws TwitterException
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public static void tweet(String msg) throws TwitterException{
|
||||
ConfigurationBuilder cb = new ConfigurationBuilder();
|
||||
cb.setDebugEnabled(true);
|
||||
cb.setOAuthConsumerKey(TWITTER_AUTH_TOKEN).setOAuthConsumerSecret(TWITTER_AUTH_SECRET);
|
||||
cb.setOAuthAccessToken(shared_prefs.getString(TWITTER_AUTH_TOKEN, ""));
|
||||
cb.setOAuthAccessTokenSecret(shared_prefs.getString(TWITTER_AUTH_SECRET, ""));
|
||||
TwitterFactory tf = new TwitterFactory(cb.build());
|
||||
Twitter twitter = tf.getInstance();
|
||||
twitter.updateStatus(msg);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -50,7 +50,7 @@ public class WebAuth extends Activity implements OnClickListener {
|
||||
*/
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
this.setResult(RESULT_OK, new Intent().putExtra(AUTH_CODE, mAuthCode.getText().toString()));
|
||||
this.finish();
|
||||
setResult(RESULT_OK, new Intent().putExtra(AUTH_CODE, mAuthCode.getText().toString()));
|
||||
finish();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user