[英]Android - Facebook login dialog and progress indicatior keeps popping out upon logging in
I have implemented Facebook
capabilities in my Android
running application. 我已经在运行
Android
应用程序中实现了Facebook
功能。 When the user presses on the Facebook
ImageButton
, it will start an authentication process (SSO). 当用户按下
Facebook
ImageButton
,它将启动身份验证过程(SSO)。 And then do a post to the user's facebook
wall. 然后在用户的
facebook
墙上发布信息。 Everything runs smoothly on the emulator. 一切都在模拟器上顺利运行。 Able to post on wall and able to view.
能够张贴在墙上并能够查看。
However, while testing on a real device, one problem occur after user presses the ImageButton
. 但是,在实际设备上进行测试时,用户按下
ImageButton
之后会出现一个问题。 The ProgressDialog
keeps popping out and does not stop. ProgressDialog
会不断弹出,并且不会停止。 User will need to close program by pressing the Home
button on the device. 用户将需要通过按设备上的“
Home
按钮来关闭程序。
What could be the problem? 可能是什么问题呢? I didn't changed any codes in the
Facebook.java
class 我没有在
Facebook.java
类中更改任何代码
Adapter.java
ImageButton fbBtn = (ImageButton) view.findViewById(R.id.fb);
fbBtn.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v)
{
taskListener.doAuthentication();
taskListener.postToWall(data[position], text[position], name[position]);
System.out.println(text[position]);
}
}
);
public static interface FBookTaskListener{
public void doAuthentication();
public void postToWall(String data, String text, String name);
}
Activity.java
protected class TheTask extends AsyncTask<Void, Void, MyResultClass >{
protected void onPreExecute() {
dialog = ProgressDialog.show(Activity.this, "Retrieving Information", "Please wait for a few seconds...", true, false);
dialog.setCancelable(true);
}
protected MyResultClass doInBackground(Void... params) {
searchContent();
MyResultClass result = new MyResultClass();
result.mStrings = mStrings;
result.dStrings = dStrings;
result.date = date;
result.name = name;
return result;
}
@Override
protected void onPostExecute(MyResultClass result) {
dStrings = result.dStrings;
mStrings = result.mStrings;
date = result.date;
name = result.name;
LazyAdapter adapter = new Adapter(Activity.this, mStrings, dStrings, name);
list.setAdapter(adapter);
adapter.setTaskListener(new FBookTaskListener(){
public void doAuthentication()
{
// here all your FB authentication related stuff.
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if(access_token != null) {
facebook.setAccessToken(access_token);
}
if(expires != 0) {
facebook.setAccessExpires(expires);
}
/*
* Only call authorize if the access_token has expired.
*/
if(!facebook.isSessionValid()) {
facebook.authorize(Activity.this, new String[] {"publish_stream", "offline_access", "read_stream"}, new DialogListener() {
@Override
public void onComplete(Bundle values)
{
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
}
@Override
public void onFacebookError(FacebookError e) {
Log.d("FACEBOOK ERROR","FB ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
}
@Override
public void onError(DialogError e) {
Log.e("ERROR","AUTH ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
}
@Override
public void onCancel() {
Log.d("CANCELLED","AUTH CANCELLED");
}
});
}
}
@Override
public void postToWall(String data, String text, String name) {
postToFacebook(data, text, name);
}
});
dialog.dismiss();
}
}
Add this code in ur activity 在您的活动中添加此代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.