使用android登录facebook后,我如何进入下一页?
我正在制作一个登录页面,用户可以选择,如单击按钮(f继续使用facebook),他们可以通过facebook登录,登录后可以进入下一页。 我在这里输入了pagebook的登录代码,它还会询问我facebook的密码。问题是,登录后,它也会保留在同一页上。如果我登录一次,那么下次它将不会询问我facebook的密码,并保留在同一页上 以下是我的Login.java代码:使用android登录facebook后,我如何进入下一页?,android,facebook,Android,Facebook,我正在制作一个登录页面,用户可以选择,如单击按钮(f继续使用facebook),他们可以通过facebook登录,登录后可以进入下一页。 我在这里输入了pagebook的登录代码,它还会询问我facebook的密码。问题是,登录后,它也会保留在同一页上。如果我登录一次,那么下次它将不会询问我facebook的密码,并保留在同一页上 以下是我的Login.java代码: public class Login extends AppCompatActivity { private Strin
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);
}
}
}
公共类登录扩展了AppCompatActivity{
私有字符串eml;
私有字符串pswrd;
私人档案跟踪器mProfileTracker;
私人对话;
字符串状态=”;
私人按钮;
剖面图;
按钮登录;
//私有int-serverResponseCode=0;
文本视图tac1;
编辑文本邮件,传递;
私有静态字符串url_create_book=”http://cloud.....com/broccoli/login.php";
公共静态CallbackManager CallbackManager;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
sdkinInitialize(getApplicationContext());
setContentView(R.layout.activity\u登录);
获取散列密钥();
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
AppEventsLogger.activateApp(此);
fbbutton=(按钮)findViewById(R.id.fbtn);
fbbutton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//调用私有方法
onFblogin();
}
});
email=(EditText)findviewbyd(R.id.email);
pass=(EditText)findViewById(R.id.password);
tac1=(TextView)findViewById(R.id.cAcc);
tac1.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v){
startActivity(新意图(Login.this、RegistrationForm.class));
}
}
);
登录=(按钮)findViewById(R.id.lbtn);
login.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
pDialog=newprogressdialog(Login.this);
pDialog.setMessage(“请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
eml=email.getText().toString();
pswrd=pass.getText().toString();
//新建CreateNewProduct().execute();
StringRequest StringRequest=新建StringRequest(Request.Method.POST、url\u create\u book、,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
pDialog.disclose();
if(response.trim().equals(“success”)){
Toast.makeText(Login.this,“Login Success”,Toast.LENGTH_SHORT.show();
//你的意图代码在这里
}否则{
Toast.makeText(Login.this,“用户名/密码不正确”,Toast.LENGTH_SHORT.show();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
pDialog.disclose();
Toast.makeText(Login.this,error.toString(),Toast.LENGTH_LONG).show();
}
}) {
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
参数put(“电子邮件”,eml);
参数put(“密码”,pswrd);
返回参数;
}
};
RequestQueue RequestQueue=Volley.newRequestQueue(Login.this);
添加(stringRequest);
}
});
}
public void Get_hash_key(){
PackageInfo信息;
试一试{
info=getPackageManager().getPackageInfo(“com.example.zeba.brocoli”,PackageManager.GET_签名);
用于(签名:信息签名){
信息文摘md;
md=MessageDigest.getInstance(“SHA”);
md.update(signature.toByteArray());
stringsomething=新字符串(Base64.encode(md.digest(),0));
//stringsomething=新字符串(Base64.encodeBytes(md.digest());
Log.e(“哈希键”,某物);
}
}捕获(PackageManager.NameNotFoundException e1){
Log.e(“未找到名称”,e1.toString());
}捕获(无算法异常){
e(“没有这样的算法”,例如toString());
}捕获(例外e){
Log.e(“异常”,例如toString());
}
}
私有void onFblogin(){
callbackmanager=callbackmanager.Factory.create();
//设置权限
LoginManager.getInstance().logInWithReadPermissions(这是一个数组.asList(“电子邮件”,“用户照片”,“公共档案”));
LoginManager.getInstance().registerCallback(callbackmanager,
新建FacebookCallback(){
@凌驾
成功时公共无效(LoginResult LoginResult){
试一试{
如果(P
private ProfileTracker mProfileTracker;
Profile profile;
@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);
}
});
}
// 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());
}
});
}