简体   繁体   中英

using facebook login in android

I am developing an android app which is intended to use Facebook login.Users will log into the app using their Facebook credentials.All is working fine when the Facebook for android app is not installed.However,when the device has the Facebook for android app installed,the app doesn't get beyond the login interface.How can i rectify that?Any assistance will be highly appreciated.Thanks. Below is the code for the login activity.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.*;
import com.facebook.model.*;
import com.facebook.Session;


public class MokoActivity extends Activity implements OnClickListener{
    TextView create_account;
    EditText name;
    //private UiLifecycleHelper uiHelper;
    private boolean isResumed=false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        create_account=(TextView)findViewById(R.id.textView_createAccount);
        name=(EditText)findViewById(R.id.editText_name);
        create_account.setOnClickListener(this);
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.textView_createAccount:
                String user_name=name.getText().toString();
                if(user_name.length()<4){
                    Toast toast=Toast.makeText(MokoActivity.this,"Invalid Name",Toast.LENGTH_LONG);
                    toast.show();
                }

                else{
            Session.openActiveSession(this, true,new Session.StatusCallback(){
                public void call(Session session,SessionState state,Exception exception){
                    if(state.isOpened()){
                        Intent intent=new Intent(MokoActivity.this,HomeActivity.class);
                        intent.putExtra("username",name.getText().toString());
                        startActivity(intent);
                    }
                }
            });
            }
            break;
        }
    }

    public void onResume(){
        Session session=Session.getActiveSession();
        if(session != null && (session.isOpened() || session.isClosed())){
            onSessionStateChange(session, session.getState(), null);
        }
        super.onResume();
         isResumed = true;
    }

    @Override
    public void onPause() {
        super.onPause();
        isResumed = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if(isResumed){
        if (state.isOpened()) {
            Intent intent=new Intent(MokoActivity.this,HomeActivity.class);
            intent.putExtra("username",name.getText().toString());
            startActivity(intent);
        } 
        else if (state.isClosed()) {
            Toast toast=Toast.makeText(MokoActivity.this,"Logged out...",Toast.LENGTH_LONG);
            toast.show();
          }
        }
    }
    @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
      }
}

In your FACEBOOK SDK LIBRARY PROJECT . in FACEBOOK.JAVA line nos from 181 to 199 replace with the following codes.

**public void authorize(Activity activity, String[] permissions,
            int activityCode, final DialogListener listener) {
       // boolean singleSignOnStarted = false;
        mAuthDialogListener = listener;
      /*  // Prefer single sign-on, where available.
        if (activityCode >= 0) {
            singleSignOnStarted = startSingleSignOn(activity, mAppId,
                    permissions, activityCode);
        }
        // Otherwise fall back to traditional dialog.
        if (!singleSignOnStarted) {
            startDialogAuth(activity, permissions);
        }*/

        startDialogAuth(activity, permissions);
    }**

it will work perfectly only in api wont get link with facebook app which installed in mobile.

If you are not able to install application , then you have to check your package name , Check the exception if your exception is bad Character exception like '_' or unable to parse package name . then please Do change your package name . It must not contain character like _(underscore) . and also change the same in AndroidManifest.xml .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM