Verify Twitter app is logged in on Android
Asked Answered
S

4

7

My app I'm developing launches the official twitter app new post screen so the user can post a tweet with some extra text added in the intent. I have got this working nicely however things get a little confused if the user is not logged in with the twitter app. The app launches but the user has to sign in, once they've done that the normal twitter screen appears, if they use the back button to get back to my app the new post screen actually appears after hitting back on the twitter feed screen.

Is there any way I can check that a user is actually signed into the twitter app before trying to run the intent?

Schwann answered 1/3, 2012 at 10:21 Comment(1)
I believe that's a bug in the Twitter app. The new post screen should appear after the successful login (based on the initial intent received).Ortrude
S
3

I think it's a Twitter app internal issue and you can't test for it.

On the other hand you could provide a Dialog warning the user for this matter with a "Do not show this dialog anymore" checkbox so he gets advised and can dimiss forever the Dialog. You could even provide instructions to authenticate insside the Twitter app in this Dialog.

Stuccowork answered 31/10, 2012 at 9:30 Comment(0)
T
3

I am using twitter4j lib. Here I check for the username. If the username is null then there is no user signed in , else I get the username. This user name is available in the access token which I store in shared preference.

username= mySession.getUsername();

username = (username.equals("")) ? "Not logged in" : username;

code for mySession :-

public class  MySession {
    private SharedPreferences sharedPref;
    private Editor editor;

    private static final String TWEET_AUTH_KEY = "auth_key";
    private static final String TWEET_AUTH_SECRET_KEY = "auth_secret_key";
    private static final String TWEET_USER_NAME = "user_name";
    private static final String SHARED = "Twitter_Preferences";

    public TwitterSession(Context context) {
        sharedPref = context.getSharedPreferences(SHARED, Context.MODE_PRIVATE);
        editor = sharedPref.edit();
    }

    public void storeAccessToken(AccessToken accessToken, String username) {
        editor.putString(TWEET_AUTH_KEY, accessToken.getToken());
        editor.putString(TWEET_AUTH_SECRET_KEY, accessToken.getTokenSecret());
        editor.putString(TWEET_USER_NAME, username);
        editor.commit();
    }

    public void resetAccessToken() {
        editor.putString(TWEET_AUTH_KEY, null);
        editor.putString(TWEET_AUTH_SECRET_KEY, null);
        editor.putString(TWEET_USER_NAME, null);

        editor.commit();
    }

    public String getUsername() {
        return sharedPref.getString(TWEET_USER_NAME, "");
    }

    public AccessToken getAccessToken() {
        String token = sharedPref.getString(TWEET_AUTH_KEY, null);
        String tokenSecret = sharedPref.getString(TWEET_AUTH_SECRET_KEY, null);

        if (token != null && tokenSecret != null)
            return new AccessToken(token, tokenSecret);
        else
            return null;
    }
}

Hope this will help you.

Tuberosity answered 31/10, 2012 at 12:3 Comment(0)
D
0

Try this function which will in turn returns you true or false.

True : Logged in
False : Not logged in

twitter.getAuthorization() function will throw you an error if it is not logged in by handling this you can find whether user is previously logged in or not.

public static boolean isAuthenticated(SharedPreferences prefs) {

      String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
      String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

      AccessToken a = new AccessToken(token,secret);
      Twitter twitter = new TwitterFactory().getInstance();
      twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);


      try {
      twitter.getAuthorization();
      return true;
      } catch (Exception e) {
      return false;
      }
}
Deary answered 1/3, 2012 at 10:53 Comment(1)
The question clearly states it opens the official app. It does not use the twitter api.Etsukoetta
J
0

just add these lines in the oncreate() in ur activity

final Session activeSession = Twitter.getInstance().core.getSessionManager().getActiveSession();

if (activeSession != null){
           //do someting
        }
Jeannettajeannette answered 26/8, 2016 at 4:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.