How to deliver Android Facebook login information to Main Activity...

Asked 1 years ago, Updated 1 years ago, 82 views

package flug.com.afeel;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.Profile;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * * Created by Pc on 2016-02-04.
 */
public class FBLoginActivity extends Activity {
    // // Custom button
    private static final String TAG_CANCEL = "FACEBOOK LOGIN";
    private Button fbbutton;
    private Button hombtn;
    public String str_email;
    public String str_id;
    public String str_firstname;
    public String str_lastname;
    public Data data;
    public String email2;
    // // Creating Facebook CallbackManager Value
    public CallbackManager callbackmanager;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // // Initialize SDK before setContentView(Layout ID)
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.layout_common_facebook_login);
        callbackmanager = CallbackManager.Factory.create();

        // // Initialize layout button
        fbbutton = (Button) findViewById(R.id.login_button);

        fbbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                LoginManager.getInstance().registerCallback(callbackmanager, new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // // App code
                        GraphRequest request = GraphRequest.newMeRequest(
                                loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(
                                            JSONObject object,
                                            GraphResponse response) {
                                        // // Application code
                                        Log.v("LoginActivity", response.toString());
                                    }
                                });
                        Bundle parameters = new Bundle();
                        parameters.putString("fields", "id,name,email,gender, birthday");
                        request.setParameters(parameters);
                        request.executeAsync();
                        **// The value returned on success to the Web view...**
                    }

                    @Override
                    public void onCancel() {
                        // // not called
                        Log.d("fb_login_sdk", "callback cancel");
                    }

                    @Override
                    public void onError(FacebookException e) {
                        // // not called
                        Log.d("fb_login_sdk", "callback onError");
                    }
                });
            }
        });

        hombtn = (Button) findViewById(R.id.fbhome_btn);
        hombtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FBLoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

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

Implemented Android Facebook login. I can't log in to Facebook with a script from the webview, so I want to log in by sending the value to the web after logging in from the app, but I don't know how to pass the information logged in from the app to the webview. I can't sleep because of this What should I do if I just want to pass the email information to Main Activity in the onSuccess part?ㅜ<

facebook android-studio android

2022-09-22 22:17

1 Answers

There's a way to create a javascript function in Android webview and call it up.

Make a function like this in javascript.

function emailUpdate(email){
     //Do the work using e-email.
}

Looking at the Android code, you're doing GraphRequest again on Succes. Then you can get an email from the onCompleted GraphRequest. I think you can do this inside the onCompleted.

String  email=object.getString("email");
//Assume that you know webView by way of findViewById, etc
webView.loadUrl( "javascript:emailUpdate('"+email+"')" );


2022-09-22 22:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.