简体   繁体   中英

data intent equal null in onActivityResult(int requestCode, int resultCode, Intent data) when i make facebook login

data intent equal null in onActivityResult(int requestCode, int resultCode, Intent data) when i make facebook login. and FacebookCallbacks didn't call

below will find my code :

ChatMainActivity.class

public class ChatMainActivity extends AppCompatActivity {

    FacebookLogin facebookLogin;

    String userType="";

    public void getHashkey(){
        try {
            PackageInfo info = getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());

                Log.i("Base64", Base64.encodeToString(md.digest(),Base64.NO_WRAP));
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.d("Name not found", e.getMessage(), e);

        } catch (NoSuchAlgorithmException e) {
            Log.d("Error", e.getMessage(), e);
        }
    }

    String currentUserId="";

  /*  @Override
    public void onBackPressed() {
        CustomDialog customDialog=new CustomDialog(this);
        customDialog.show();

       super.onBackPressed();
    }*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getHashkey();

        FirebaseMessaging.getInstance().subscribeToTopic("android");

        MySharedPreferences.setUpMySharedPreferences(this,Constants.constKey);
        currentUserId=MySharedPreferences.getUserSetting(Constants.currentUserId);
        Log.d("asdasd",currentUserId);

        facebookLogin=new FacebookLogin(this);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                userType="u";
                facebookLogin.login(userType);

            }
        });

        FloatingActionButton fab1 = (FloatingActionButton) findViewById(R.id.fab1);
        fab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

                /*userType="a";
                Intent intent = new Intent(ChatMainActivity.this, ChatActivity.class);
                Bundle bundle = new Bundle();
                String name="admin";
                bundle.putString("name",name);
                bundle.putString("id", "admin");
                bundle.putString("img", "admin");
                bundle.putString("userType",userType);
                intent.putExtra("userData", bundle);
                Toast.makeText(ChatMainActivity.this,"welcome "+name,Toast.LENGTH_SHORT).show();
                startActivity(intent);*/

                Intent intent=new Intent(ChatMainActivity.this,UsersActivit.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        facebookLogin.registerCallbackManager(requestCode, resultCode, data);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            LoginManager.getInstance().logOut();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        /*Intent intent=new Intent(this ,ChatService.class);
        stopService(intent);*/
    /*    if( currentUserId!=null &&!currentUserId.equals("")) {
            Intent intent = new Intent(this, ChatService.class);
            startService(intent);
        }*/
        super.onResume();
    }

    @Override
    protected void onDestroy() {
       // Log.d("asdasd",currentUserId);
       /* if( currentUserId!=null &&!currentUserId.equals("")) {
            Intent intent = new Intent(this, ChatService.class);
            startService(intent);
        }*/
        super.onDestroy();
    }
}

FacebookLogin .class

public class FacebookLogin implements FacebookCallback<LoginResult> {

Activity activity;
private List<String> facebookPermissions;
private CallbackManager callbackManager;
private LoginManager loginManager;
private ProfileTracker profileTracker = new ProfileTracker() {
    @Override
    protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
        if (currentProfile != null) {
            Intent intent = new Intent(activity, ChatActivity.class);
            Bundle bundle = new Bundle();
            String name = currentProfile.getFirstName() + " " + currentProfile.getLastName();
            bundle.putString("name", name);
            bundle.putString("id", currentProfile.getId());
            intent.putExtra("userData", bundle);
            Toast.makeText(activity, "welcome " + name, Toast.LENGTH_SHORT).show();
            activity.startActivity(intent);
        }
    }
};
private AccessTokenTracker accessTokenTracker = new AccessTokenTracker() {
    @Override
    protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {

    }
};

public FacebookLogin(Activity activity) {

    this.activity = activity;
    facebookPermissions = Arrays.asList("public_profile", "email");
    profileTracker.startTracking();
    accessTokenTracker.startTracking();

}

private void initFacebookLogin() {

    FacebookSdk.sdkInitialize(activity.getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    loginManager = LoginManager.getInstance();
    //loginManager.registerCallback(callbackManager,this);

}

String userType = "";

public void login(String type) {
    if (NetworkState.ConnectionAvailable(activity)) {
        userType = type;
        initFacebookLogin();
       /*loginManager.logInWithReadPermissions(activity, facebookPermissions);*/
        LoginManager.getInstance().registerCallback(callbackManager, this);
        LoginManager.getInstance().logInWithReadPermissions(activity, facebookPermissions);

    } else
        Toast.makeText(activity, "No Internet Connection", Toast.LENGTH_SHORT).show();
}

public void registerCallbackManager(final int requestCode, final int resultCode, final Intent data) {
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onSuccess(LoginResult loginResult) {

    Profile profile = Profile.getCurrentProfile();
    if (profile != null) {
        Intent intent = new Intent(activity, ChatActivity.class);
        Bundle bundle = new Bundle();
        String name = profile.getFirstName() + " " + profile.getLastName();
        bundle.putString("name", name);
        bundle.putString("id", profile.getId());
        bundle.putString("img", profile.getProfilePictureUri(100, 100).toString());
        bundle.putString("userType", userType);
        intent.putExtra("userData", bundle);
        Toast.makeText(activity, "welcome " + name, Toast.LENGTH_SHORT).show();
        activity.startActivity(intent);
    }
    // Toast.makeText(activity,"welcome ",Toast.LENGTH_SHORT).show();
}

@Override
public void onCancel() {

}

@Override
public void onError(FacebookException error) {

}
}

This is what I do:

  • declare a "Facebook" LoginButton and a FacebookCallbackManager:
LoginButton btLogin;
CallbackManager mFacebookCallbackManager;
  • init Facebook callbacks handler:
 private void initFacebookLoginCallbacks() {
        mFacebookCallbackManager = CallbackManager.Factory.create();
        LoginManager.getInstance().registerCallback(mFacebookCallbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // logged in successfully!
                    }

                    @Override
                    public void onCancel() {
                        Log.v(TAG, "On cancel");
                    }

                    @Override
                    public void onError(FacebookException error) {
                        Log.v(TAG, "On error: " + error.toString());
                    }
                });
    }
  • Call onActivityResult on my FacebookCallbackManager
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mFacebookCallbackManager.onActivityResult(requestCode, resultCode, data);
    }

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