Facebook Connect 在模拟器/设备上的工作方式不同,但在 Google Play 上却不同 [英] Facebook Connect works different on emulator/device, but not on Google Play
问题描述
我的应用中出现了奇怪的 facebook 连接行为.如果我在模拟器中使用它,则在 facebook.authorize > onComplete 调用 facebook.request(检索用户数据)和 facebook.dialog(在墙上发布).但是如果我使用设备进行调试,这些调用需要在 onActivityResult() 才能工作.
I'm getting a weird facebook connect behavior in my app. If I use it in emulator, the calls for facebook.request (to retrieve user data) and facebook.dialog (to post on wall) are made at facebook.authorize > onComplete. But if I use a device for debugging, these calls need to be at onActivityResult() to work.
我已将经过设备测试的 apk 上传到 google play,但它不起作用!相同的代码,如果从 eclipse 安装,它可以工作......如果从 Play 下载,则不会!
And I've uploaded the device-tested apk into google play, but it doesn't works! The SAME code, if installed from eclipse, it works... if downloaded from Play, doesn't!
不起作用"是指在用户获得授权后不会调用 facebook.request 和 facebook.dialog,并且应用会卡在空白屏幕中.
by "doesn't works", I mean that facebook.request and facebook.dialog aren't called after user is authorized, and the app get stucked in a blank screen.
我的 facebook 连接活动:
my facebook connect activity:
public class FacebookConnectActivity extends Activity {
private static final String TAG_JSON = "json";
static Facebook facebook = new Facebook("111111111111111");
AsyncFacebookRunner mAsyncRunner;
private SharedPreferences mPrefs;
JSONObject json;
Context ctx = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resgatar_produto_layout);
Log.w("FacebookConnect", "Activity Started");
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);
}
if(!facebook.isSessionValid()) {
facebook.authorize(this, new String[] {"email", "user_photos", "user_birthday", "user_hometown", "user_relationships","user_location", "user_work_history", "publish_actions"}, new DialogListener() {
@Override
public void onComplete(Bundle values) {
Log.w("FacebookConnect", "User authorized");
/*mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me", new SampleRequestListener());
Bundle params = new Bundle();
params.putString("link", "www.qranio.com");
facebook.dialog(ctx, "feed", params, new SampleDialogListener());*/
}
@Override
public void onFacebookError(FacebookError error) {
Log.w("FacebookConnect", "Error authorizing: "+error.toString());
}
@Override
public void onError(DialogError e) {}
@Override
public void onCancel() {}
});
}
}
public static void logoutFB(){
if(facebook.isSessionValid()){
facebook.setAccessToken(null);
}
}
public void startDataProcess(){
while (json==null){
}
Intent a = new Intent(FacebookConnectActivity.this, FacebookDataProcess.class);
a.putExtra(TAG_JSON, json.toString());
startActivity(a);
finish();
}
public class SampleDialogListener extends BaseDialogListener {
public void onComplete(Bundle values) {
final String postId = values.getString("post_id");
if (postId != null) {
Log.d("Facebook-Example", "Dialog Success! post_id=" + postId);
mAsyncRunner.request(postId, new WallPostRequestListener());
} else {
Log.d("Facebook-Example", "No wall post made");
startDataProcess();
}
}
public void onCancel(){
Log.w("FacebookConnect", "Post to Wall Canceled");
startDataProcess();
}
}
public class WallPostRequestListener extends BaseRequestListener {
public void onComplete(final String response, final Object state) {
Log.d("WallPostRequestListener", "Got response: " + response);
String message = "<empty>";
try {
JSONObject json = Util.parseJson(response);
message = json.getString("message");
} catch (JSONException e) {
Log.w("Facebook-Example", "JSON Error in response");
} catch (FacebookError e) {
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
//Toast.makeText(ctx, "Erro ao postar no Facebook.", Toast.LENGTH_LONG).show();
}
startDataProcess();
}
}
public class SampleRequestListener extends BaseRequestListener {
public void onComplete(final String response, final Object state) {
try {
// process the response here: executed in background thread
Log.d("SampleRequestListener", "Response: " + response.toString());
json = Util.parseJson(response);
Log.e("FacebookConnect", "JSON " + json.toString());
startDataProcess();
// then post the processed result back to the UI thread
// if we do not do this, an runtime exception will be generated
// e.g. "CalledFromWrongThreadException: Only the original
// thread that created a view hierarchy can touch its views."
} catch (JSONException e) {
Log.w("Facebook-Example", "JSON Error in response");
} catch (FacebookError e) {
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
try{
mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me", new SampleRequestListener());
/*Bundle params = new Bundle();
params.putString("link", "www.qranio.com");
//params.putString("display", "touch");
facebook.dialog(ctx, "feed", params, new SampleDialogListener());*/
Log.e("FacebookConnect", "Authentication authorizeCallback called");
Log.e("FacebookConnect", "Access Token: "+facebook.getAccessToken().toString());
//facebook.
//if (facebook.isSessionValid()){
//}else{
//Log.e("FacebookConnect", "Invalid facebook session while trying to fetch user data");
//}*/
}catch(FacebookError fbe){
Log.e("FacebookConnect", "onActivityResult FacebookError " + fbe.toString());
}catch (Exception e){
Log.e("FacebookConnect", "onActivityResult Exception " + e.toString());
}
}
}
[更新]我的 AndroidManifest.xml
[UPDATE] My AndroidManifest.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="9" android:versionName="1.1.3"
package="androidhive.dashboard" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="8"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application android:icon="@drawable/icone"
android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<activity android:label="@string/app_name"
android:name="com.androidhive.dashboard.SplashScreen"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateHidden|adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.androidhive.dashboard.AndroidDashboardDesignActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateHidden|adjustResize"/>
<activity android:name="com.androidhive.dashboard.LoginActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateHidden|adjustResize"/>
<activity
android:name="com.androidhive.dashboard.RegisterActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateHidden|adjustResize"/>
<activity
android:name="com.androidhive.dashboard.LoginWithUsernameActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateAlwaysVisible"/>
<activity android:name="com.androidhive.dashboard.FacebookConnectActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateHidden|adjustResize"/>
<activity android:name="com.androidhive.dashboard.FacebookDataProcess"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateHidden|adjustResize"/>
...other activities...
</application>
</manifest>
推荐答案
Facebook Connect 是 SSO 流,完成后会回调 onActivityResult.你应该在那里处理响应
Facebook Connect is SSO flow, and when it is done onActivityResult is called back. you should handle the response in there
简单例子
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AppContext.FACEBOOK_SSO_ACTIVITY_CODE) {
// Successfully redirected.
if (resultCode == Activity.RESULT_OK) {
// Check OAuth 2.0/2.10 error code.
String error = data.getStringExtra("error");
if (error == null) {
error = data.getStringExtra("error_type");
}
else {// SSO successful
String access_token = data.getStringExtra("access_token");
// from here on, you can do whatever you need
}
}
}
另一方面,我建议两个实现这两个选项(sso 和 oauth 作为后备).这是 Facebook SSO 和 OAuth 的示例实现;https://github.com/wareninja/generic-oauth2-login-for-android
on the other hand, I suggest two implement both options (sso and oauth as fallback). here is an example implementation for both Facebook SSO and OAuth; https://github.com/wareninja/generic-oauth2-login-for-android
这篇关于Facebook Connect 在模拟器/设备上的工作方式不同,但在 Google Play 上却不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!