使用android登录后,我该怎么去下一页? [英] How can i go to next page after login with facebook using android?

查看:136
本文介绍了使用android登录后,我该怎么去下一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在登录页面,用户有选择,如果他们点击按钮(f继续Facebook),他们可以通过Facebook登录,登录后可以去下一页。
在这里我已经把登录的代码与pagebook,它问我的密码为facebook也。问题是,登录后也保留在同一页面上。如果我登录一次,下一次它不会问我Facebook的密码保留在同一页上。

i am making a login page where users are having options like if they click button(f continue with facebook)they can login through facebook and after login they can go to next page. here i have put code of login with pagebook and it ask me password for facebook also.The problem is that after login also its remains on the same page.And if i logged in once then next time it will not ask me password for facebook and remains on the same page.

这是我的Login.java代码:

here is my code for Login.java:

public class Login extends AppCompatActivity {
    private String eml;
    private String pswrd;
    private ProfileTracker mProfileTracker;
    private ProgressDialog pDialog;
    String status = "";
    private Button fbbutton;
    Profile profile;
    Button login;

    // private int serverResponseCode = 0;
    TextView tac1;
    EditText email, pass;
    private static String url_create_book = "http://cloud.....com/broccoli/login.php";
    public static CallbackManager callbackmanager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_login);
        Get_hash_key();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        AppEventsLogger.activateApp(this);

        fbbutton = (Button) findViewById(R.id.fbtn);

        fbbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Call private method
                onFblogin();
            }
        });

        email = (EditText)findViewById(R.id.email);
        pass = (EditText) findViewById(R.id.password);

        tac1 = (TextView)findViewById(R.id.cAcc);

        tac1.setOnClickListener(new View.OnClickListener()

                                {

                                    @Override
                                    public void onClick(View v) {


                                        startActivity(new Intent(Login.this, RegistrationForm.class));


                                    }
                                }

        );

        login = (Button) findViewById(R.id.lbtn);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                pDialog = new ProgressDialog(Login.this);
                pDialog.setMessage("Please wait..");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
                eml = email.getText().toString();
                pswrd = pass.getText().toString();


                // new CreateNewProduct().execute();
                StringRequest stringRequest = new StringRequest(Request.Method.POST, url_create_book,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                pDialog.dismiss();
                                if (response.trim().equals("success")) {
                                    Toast.makeText(Login.this, "Login Success", Toast.LENGTH_SHORT).show();

                                    //your intent code here
                                } else {
                                    Toast.makeText(Login.this, "username/password incorrect", Toast.LENGTH_SHORT).show();

                                }
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                pDialog.dismiss();
                                Toast.makeText(Login.this, error.toString(), Toast.LENGTH_LONG).show();
                            }
                        }) {
                    @Override
                    protected Map<String, String> getParams() {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("email", eml);
                        params.put("password", pswrd);

                        return params;
                    }
                };
                RequestQueue requestQueue = Volley.newRequestQueue(Login.this);
                requestQueue.add(stringRequest);

            }

        });
    }


    public void Get_hash_key() {
        PackageInfo info;
        try {
            info = getPackageManager().getPackageInfo("com.example.zeba.broccoli", PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md;
                md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                String something = new String(Base64.encode(md.digest(), 0));
                //String something = new String(Base64.encodeBytes(md.digest()));
                Log.e("hash key", something);
            }
        } catch (PackageManager.NameNotFoundException e1) {
            Log.e("name not found", e1.toString());
        } catch (NoSuchAlgorithmException e) {
            Log.e("no such an algorithm", e.toString());
        } catch (Exception e) {
            Log.e("exception", e.toString());
        }
    }




    private void onFblogin() {
        callbackmanager = CallbackManager.Factory.create();

        // Set permissions
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_photos", "public_profile"));

        LoginManager.getInstance().registerCallback(callbackmanager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        try {
                            if (Profile.getCurrentProfile() == null) {
                                mProfileTracker = new ProfileTracker() {
                                    @Override
                                    protected void onCurrentProfileChanged(Profile profile_old, Profile profile_new) {
                                        // profile2 is the new profile
                                        profile = profile_new;
                                        mProfileTracker.stopTracking();
                                    }
                                };
                                mProfileTracker.startTracking();
                            } else {
                                profile = Profile.getCurrentProfile();
                            }

                            GraphRequest request = GraphRequest.newMeRequest(
                                    loginResult.getAccessToken(),
                                    new GraphRequest.GraphJSONObjectCallback() {
                                        @Override
                                        public void onCompleted(JSONObject object, GraphResponse response) {
                                            Log.v("FACEBOOK LOGIN", response.toString());
                                            // Application code
                                            try {
                                                String fb_id = object.getString("id");
                                                String fb_name = object.getString("name");
                                                String profilePicUrl = "https://graph.facebook.com/" + fb_id + "/picture?width=200&height=200";
                                                String fb_gender = object.getString("gender");
                                                String fb_email = object.getString("email");
                                                String fb_birthday = object.getString("birthday");
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }

                                            //use shared preferences here
                                        }
                                    });
                            Bundle parameters = new Bundle();
                            parameters.putString("fields", "id,name,email,gender,birthday,picture.type(small)");
                            request.setParameters(parameters);
                            request.executeAsync();

                            //go to Home page
                            Intent intent = new Intent(getApplicationContext(), Home.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            finish();
                        } catch (Exception e) {
                            Log.d("ERROR", e.toString());
                        }
                    }

                    @Override
                    public void onCancel() {
                        // Log.d(TAG_CANCEL, "On cancel");
                    }

                    @Override
                    public void onError(FacebookException error) {
                        //Log.d(TAG_ERROR, error.toString());
                    }
                });
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}


推荐答案

您的代码中有几个问题:您正在以fb_login方法启动正常登录...

there are couple of problems in your code: your are initiating your normal login in fb_login method...

另外在 onsuccess ()方法,您需要请求允许撤销用户详细信息...

Also in onsuccess() method you need to request permisssion for retreiving user details...

尝试以下代码:

声明变量

private ProfileTracker mProfileTracker;
Profile profile;



in your onCreate() replace:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_login);
    Get_hash_key();
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    AppEventsLogger.activateApp(this);

    fbbutton = (Button) findViewById(R.id.fbtn);

    fbbutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Call private method
            onFblogin();
        }
    });

    email = (EditText)findViewById(R.id.email);
    pass = (EditText) findViewById(R.id.password);

    tac1 = (TextView)findViewById(R.id.cAcc);

    tac1.setOnClickListener(new View.OnClickListener()

                            {

                                @Override
                                public void onClick(View v) {


                                    startActivity(new Intent(Login.this, RegistrationForm.class));


                                }
                            }

    );

    login = (Button) findViewById(R.id.lbtn);
    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            pDialog = new ProgressDialog(Login.this);
            pDialog.setMessage("Please wait..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
            eml = email.getText().toString();
            pswrd = pass.getText().toString();


            // new CreateNewProduct().execute();
            StringRequest stringRequest = new StringRequest(Request.Method.POST, url_create_book,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            pDialog.dismiss();
                            if (response.trim().equals("success")) {
                                Toast.makeText(Login.this, "Login Success", Toast.LENGTH_SHORT).show();

                            //your intent code here
                            } else {
                                Toast.makeText(Login.this, "username/password incorrect", Toast.LENGTH_SHORT).show();

                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            pDialog.dismiss();
                            Toast.makeText(Login.this, error.toString(), Toast.LENGTH_LONG).show();
                        }
                    }) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("email", eml);
                    params.put("password", pswrd);

                    return params;
                }
            };
            RequestQueue requestQueue = Volley.newRequestQueue(Login.this);
            requestQueue.add(stringRequest);

        }

    });
}

还可以在您的fb_login方法中替换:

Also in your fb_login method replace:

// Private method to handle Facebook login and callback
private void onFblogin() {
    callbackmanager = CallbackManager.Factory.create();

    // Set permissions
    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_photos", "public_profile"));

    LoginManager.getInstance().registerCallback(callbackmanager,
            new FacebookCallback<LoginResult>() {
                 @Override
    public void onSuccess(LoginResult loginResult) {
        try {
            if (Profile.getCurrentProfile() == null) {
                mProfileTracker = new ProfileTracker() {
                    @Override
                    protected void onCurrentProfileChanged(Profile profile_old, Profile profile_new) {
                        // profile2 is the new profile
                        profile = profile_new;
                        mProfileTracker.stopTracking();
                    }
                };
                mProfileTracker.startTracking();
            } else {
                profile = Profile.getCurrentProfile();
            }

            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.v("FACEBOOK LOGIN", response.toString());
                            // Application code
                            try {
                                String fb_id = object.getString("id");
                                String fb_name = object.getString("name");
                                String profilePicUrl = "https://graph.facebook.com/" + fb_id + "/picture?width=200&height=200";
                                String fb_gender = object.getString("gender");
                                String fb_email = object.getString("email");
                                String fb_birthday = object.getString("birthday");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            //use shared preferences here
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender,birthday,picture.type(small)");
            request.setParameters(parameters);
            request.executeAsync();

            //go to Home page
            Intent intent = new Intent(getApplicationContext(), Home_page.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
        } catch (Exception e) {
            Log.d("ERROR", e.toString());
        }
    }

        @Override
        public void onCancel() {
         // Log.d(TAG_CANCEL, "On cancel");
        }

        @Override
        public void onError(FacebookException error) {
         //Log.d(TAG_ERROR, error.toString());
        }
    });
}

这篇关于使用android登录后,我该怎么去下一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆