2

为什么 Android Digits 在号码验证后并不总是返回 onSuccess 或 onFalure?它只是完成 DigitsActivity 并且什么也不做,但有时它还可以,我收到 onSuccess。

public void initForm(){
    Digits.getSessionManager().clearActiveSession();// http://stackoverflow.com/questions/31679089/fabric-digits-returns-a-null-object-of-phonenumber-after-otp-verification-on-and
    DigitsAuthConfig.Builder digitsAuthConfigBuilder = new DigitsAuthConfig.Builder()
            .withAuthCallBack(new AuthCallback() {
                @Override
                public void success(DigitsSession digitsSession, String s) {
                    Toast.makeText(LoginActivity.this, s, Toast.LENGTH_LONG).show();
                    TwitterAuthToken token = (TwitterAuthToken)digitsSession.getAuthToken();
                    DigitsOAuthSigning oauthSigning = new DigitsOAuthSigning(SapronApplication.conf(), token);
                    Map<String, String> authHeaders = oauthSigning.getOAuthEchoHeadersForVerifyCredentials();
                    requestToken(authHeaders.get("X-Verify-Credentials-Authorization"));

                }

                @Override
                public void failure(DigitsException e) {
                    Toast.makeText(LoginActivity.this, e.toString(), Toast.LENGTH_LONG).show();
                }
            })
            .withPhoneNumber("+7");


    Digits.authenticate(digitsAuthConfigBuilder.build());
}
4

3 回答 3

2

我遇到了同样的问题,并且 phoneNumber 也像这个问题一样返回为 null 。我在使用自定义按钮时解决了这个问题。主要问题是没有正确清除数字会话并在每次单击时在 withAuthCallBack() 中设置新的 AuthCallback。下面给出了解决我的问题的代码。希望它能帮助需要使用自定义数字按钮的人.....

findViewById(R.id.customDigitsButton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            verifyMobileUsingDigits();
        }
    });

private AuthCallback authCallback;
public void verifyMobileUsingDigits(){
    logoutFabricDigits("verifyMobileUsingDigits");// manual deletion of all digit sessions, as only Digits.getSessionManager().clearActiveSession() did not helped me.....
    if(authCallback == null){
        authCallback = new AuthCallback() {
            @Override
            public void success(DigitsSession session, String phoneNumber) {
                Toast.makeText(getApplicationContext(), "Authentication successful for " + phoneNumber, Toast.LENGTH_LONG).show();
            }

            @Override
            public void failure(DigitsException exception) {
                Toast.makeText(getApplicationContext(), "Authentication failure", Toast.LENGTH_LONG).show();
            }
        };
    }
    DigitsAuthConfig.Builder digitsAuthConfigBuilder = new DigitsAuthConfig.Builder()
            .withAuthCallBack(authCallback)
            .withPhoneNumber("+880")
            .withEmailCollection(false)
            .withThemeResId(R.style.CustomDigitsTheme);
    Digits.authenticate(digitsAuthConfigBuilder.build());
}

public static void logoutFabricDigits(String TAG){
    try {
        Map<Long, DigitsSession> digitsSessionMap = Digits.getSessionManager().getSessionMap();
        if(digitsSessionMap != null){
            Set<Long> digitIdSet = new HashSet<>(digitsSessionMap.keySet());
            for(Long digitId : digitIdSet){
                DigitsSession digitsSession = digitsSessionMap.get(digitId);
                if(digitsSession != null){
                    Digits.getSessionManager().clearSession(digitsSession.getId());
                }
            }
        }
        Digits.getSessionManager().clearActiveSession();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
于 2016-05-08T10:28:24.097 回答
0

好的,我不知道自定义按钮和自定义 authcallback 有什么问题。通过使用 DigitsAuthButton 解决了问题。

  digitsButton = (DigitsAuthButton) findViewById(R.id.auth_button);
   // digitsButton.setAuthTheme(R.style.AppTheme2);
    digitsButton.setCallback(new AuthCallback() {
        @Override
        public void success(DigitsSession digitsSession, String s) {
            new MaterialDialog.Builder(LoginActivity.this)
                    .progress(true, 0)
                    .show();
           TwitterAuthToken token = (TwitterAuthToken)digitsSession.getAuthToken();
            DigitsOAuthSigning oauthSigning = new DigitsOAuthSigning(SapronApplication.conf(), token);
            Map<String, String> authHeaders = oauthSigning.getOAuthEchoHeadersForVerifyCredentials();
            requestToken(authHeaders.get("X-Verify-Credentials-Authorization"));
        }

        @Override
        public void failure(DigitsException e) {

        }
    });
于 2016-03-10T15:51:44.253 回答
0

正如@rallat 在这里评论的那样https://github.com/twitter/digits-android/issues/6

他们将 authcallback 更改为弱引用,以避免内存泄漏。

只需在您的上下文中创建回调变量,fe:

AuthCallback authCallback = new AuthCallback() {
    @Override
    public void success(DigitsSession session, String phoneNumber) {
        // Do something with the session
        Log.d(TAG, "success: " + phoneNumber);
    }

    @Override
    public void failure(DigitsException exception) {
        // Do something on failure
        Log.d(TAG, "failure: ");
        //show error or somthing
    }
};

并将其传递给您的构建器:

digitsButton.setCallback(authCallback);
于 2016-09-05T08:26:47.707 回答