我在我的应用中遇到了一个奇怪的Facebook连接行为。如果我在模拟器中使用它,则在facebook.authorize>上调用facebook.request(检索用户数据)和facebook.dialog(在墙上发布)。的onComplete。但是如果我使用设备进行调试,这些调用需要在onActivityResult()上才能工作。
我已经将经过设备测试的apk上传到谷歌播放,但它不起作用! SAME代码,如果从eclipse安装,它可以工作......如果从Play下载,则不行!
by“not not works”,我的意思是在用户获得授权后不会调用facebook.request和facebook.dialog,并且应用程序会被卡在空白屏幕中。
我的脸书连接活动:
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());
}
}
}
[UPDATE] 我的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>