I have created an application which needs sign in from Facebook/Twitter/Google to get started. In a layout I have created three switches each for Facebook, Twitter and Google which has options ON and OFF. I want to make the switch of the particular account as ‘ON’ if the user is logged in from that corresponding account. Example if the user is logged in from Facebook, only the switch beside Facebook should be ON. How can I do that?
Any suggestions would be appreciated, and also if somebody know then please refer me to any tutorial related to this.
Below is my code for the login page. I have shown the login for Facebook part:
Thanx 🙂
private OnClickListener loginButtonListener = new OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick( View v ) {
String[] permissions = { "offline_access", "publish_stream", "user_photos", "publish_checkins","photo_upload" };
if(v.getId() == R.id.button1 )
{
facebookSwitch = true;
twitterSwitch = false;
googleSwitch = false;
if( !mFacebook.isSessionValid() ) {
Toast.makeText(Login.this, "Authorizing", Toast.LENGTH_SHORT).show();
mFacebook.authorize(Login.this, permissions, new LoginDialogListener());
}
else {
Toast.makeText( Login.this, "Has valid session", Toast.LENGTH_SHORT).show();
try {
JSONObject json = Util.parseJson(mFacebook.request("me"));
//Log.d("Login", "11111111111111111");
String facebookID = json.getString("id");
//Log.d("Login", "22222222222222");
String firstName = json.getString("first_name");
//Log.d("Login", "3333333333333333333");
String lastName = json.getString("last_name");
//Log.d("Login", "4444444444444444444444");
Toast.makeText(Login.this, "You already have a valid session, " + firstName + " " + lastName + ". No need to re-authorize.", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Login.this,MainActivity.class);
startActivity(intent);
}
catch( Exception error ) {
Toast.makeText( Login.this, error.toString(), Toast.LENGTH_SHORT).show();
}
catch( FacebookError error ) {
Toast.makeText( Login.this, error.toString(), Toast.LENGTH_SHORT).show();
}
}
}
Try it this way….
– First create a
Singleton Classwith 3 booleanvariables for 3 logins info, and thereGetter-Setter`. Singleton is needed over here so that only One object of that class is formed no matter from where that class is called in the whole application. So now you have a single point of info.– Always check
SingletonClass’s variables in the beginning of anotherActivityor when needed by you, to know that whether the user is logged into one or two or all the social networking sites.////////////////////////////////Edited Part/////////////////////////////
A simple way of creating a Singleton is below, thought there are few more:
Now To call this object in another class do as below…