更新Facebook SDK Android

时间:2015-05-14 06:07:17

标签: android facebook-sdk-3.0 facebook-sdk-4.0

我使用旧版本的Facebook Android SDK(我认为是v 3.0.1)已经有一段时间了,直到我遇到通过Facebook登录的问题。

我在Stack Trace

中收到此错误
java.lang.RuntimeException: Unable to resume activity {com.example/com.facebook.LoginActivity}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.facebook.platform.PLATFORM_SERVICE cat=[android.intent.category.DEFAULT] }
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2958)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2989)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2372)
    at android.app.ActivityThread.access$800(ActivityThread.java:148)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5274)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.facebook.platform.PLATFORM_SERVICE cat=[android.intent.category.DEFAULT] }
    at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1745)
    at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1844)
    at android.app.ContextImpl.bindService(ContextImpl.java:1822)
    at android.content.ContextWrapper.bindService(ContextWrapper.java:538)
    at com.facebook.GetTokenClient.start(GetTokenClient.java:62)
    at com.facebook.AuthorizationClient$GetTokenAuthHandler.tryAuthorize(AuthorizationClient.java:535)
    at com.facebook.AuthorizationClient.tryCurrentHandler(AuthorizationClient.java:214)
    at com.facebook.AuthorizationClient.tryNextHandler(AuthorizationClient.java:193)
    at com.facebook.AuthorizationClient.authorize(AuthorizationClient.java:121)
    at com.facebook.AuthorizationClient.startOrContinueAuth(AuthorizationClient.java:102)
    at com.facebook.LoginActivity.onResume(LoginActivity.java:113)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
    at android.app.Activity.performResume(Activity.java:6063)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2947)
    ... 11 more
java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.facebook.platform.PLATFORM_SERVICE cat=[android.intent.category.DEFAULT] }
    at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1745)
    at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1844)
    at android.app.ContextImpl.bindService(ContextImpl.java:1822)
    at android.content.ContextWrapper.bindService(ContextWrapper.java:538)
    at com.facebook.GetTokenClient.start(GetTokenClient.java:62)
    at com.facebook.AuthorizationClient$GetTokenAuthHandler.tryAuthorize(AuthorizationClient.java:535)
    at com.facebook.AuthorizationClient.tryCurrentHandler(AuthorizationClient.java:214)
    at com.facebook.AuthorizationClient.tryNextHandler(AuthorizationClient.java:193)
    at com.facebook.AuthorizationClient.authorize(AuthorizationClient.java:121)
    at com.facebook.AuthorizationClient.startOrContinueAuth(AuthorizationClient.java:102)
    at com.facebook.LoginActivity.onResume(LoginActivity.java:113)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
    at android.app.Activity.performResume(Activity.java:6063)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2947)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2989)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2372)
    at android.app.ActivityThread.access$800(ActivityThread.java:148)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5274)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

如果我通过安装了Facebook Apps的Android Lollipop设备登录Facebook。

我目前通过使用此代码

更改SessionLoginBehavior来避免此问题
public void loginFacebook(){
    Session session = new Session(context);
    Session.setActiveSession(session);
    Session.OpenRequest openRequest = null;
    if (fragment != null) {
        openRequest = new Session.OpenRequest(fragment);
    } else if (context instanceof Activity) {
        openRequest = new Session.OpenRequest((Activity)context);
    }

    if (openRequest != null) {
        openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
        openRequest.setPermissions(Arrays.asList("email", "user_birthday"));
        //openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);         //  Native Login Changed
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {                    //  Lollipop force login from website
            openRequest.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
        } else {                                                                        //  Below Lollipop login with fallback
            openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
        }

        session.openForRead(openRequest);
    }
}

如果我将Facebook SDK更改为v.4.1.1,我会收到很多错误,因为Facebook SDK中的很多import不再存在于v 4.1.1

我应该怎样做才能在不更改SessionLoginBehavior的情况下解决此问题,我应该继续使用当前的修复实现还是其他?

0 个答案:

没有答案