Android Open Source - Antares Twitter Utils






From Project

Back to project page Antares.

License

The source code is released under:

Apache License

If you think the Android project Antares listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.takac_j30.antares;
// www  .jav a 2 s.  com
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.util.Log;
public class TwitterUtils {

    private static final String TOKEN = "token";
    private static final String TOKEN_SECRET = "token_secret";
    private static final String PREF_NAME = "antares_access_token";
    /**
     * Twitter????????????????????????????????????????????????????????????????????
     *
     * @param context
     * @return
     */
    public static Twitter getTwitterInstance(Context context) {
        String consumerKey = context.getString(R.string.CONSUMER_KEY);
        String consumerSecret = context.getString(R.string.CONSUMER_SECRET);

        TwitterFactory factory = new TwitterFactory();
        Twitter twitter = factory.getInstance();
        twitter.setOAuthConsumer(consumerKey, consumerSecret);

        if (hasAccessToken(context)) {
            twitter.setOAuthAccessToken(loadAccessToken(context));
        }
        return twitter;
    }

    /**
     * ????????????????????????????????
     *
     * @param context
     * @param accessToken
     */
    public static void storeAccessToken(Context context, AccessToken accessToken) {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString(TOKEN, accessToken.getToken());
        editor.putString(TOKEN_SECRET, accessToken.getTokenSecret());
        editor.commit();
    }

    /**
     * ???????????????????????????????????
     *
     * @param context
     * @return
     */
    public static AccessToken loadAccessToken(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        String token = preferences.getString(TOKEN, null);
        String tokenSecret = preferences.getString(TOKEN_SECRET, null);
        if (token != null && tokenSecret != null) {
            return new AccessToken(token, tokenSecret);
        } else {
            return null;
        }
    }

    /**
     * ????????????????????????true????????????
     *
     * @return
     */
    public static boolean hasAccessToken(Context context) {
        return loadAccessToken(context) != null;
    }

    /**
     * ??????????????????????????????????
     *
     * @param context
     * @param user
     */
    public static void storeMyData(Context context, User user) {
//        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
//        Editor editor = preferences.edit();
//        editor.putStringSet(USER_DATA, user);
//        editor.commit();
    }
}




Java Source Code List

com.takac_j30.antares.Account.java
com.takac_j30.antares.AppInformation.java
com.takac_j30.antares.AutoReply.java
com.takac_j30.antares.AutoTweetAdapter.java
com.takac_j30.antares.CustomPagerAdapter.java
com.takac_j30.antares.MainActivity.java
com.takac_j30.antares.OAuthActivity.java
com.takac_j30.antares.ReplySettings.java
com.takac_j30.antares.TweetSettings.java
com.takac_j30.antares.TwitterUtils.java