无法覆盖OnActivityResult方法

时间:2018-08-02 16:28:44

标签: java android override facebook-sdk-4.0

我对Android Studio不太满意。我启动了一个WebView项目,实际上,如果您忘记了密码,我们可以登录/注册和编辑密码。现在,我想添加从Facebook登录。我实现了Facebook SDK,在我的开发人员应用程序页面中添加了包和哈希键。我遵循该文档,并且选择从我的网页登录用户,并通过JavascriptInterface使用logInWithReadPermissions方法调用Facebook SDK。

然后我得到结果,并将结果返回到我的网页。但是,要获取此数据,我必须覆盖我的Activity类(由onActivityResult类扩展的)方法AppCompatActivity,而Android Studio对我说我无法覆盖{{1} }方法,因为该方法来自超类。 另外,如果删除onActivityResult,则永远不会调用该方法。

我从较旧的kotlin projet复制/粘贴此代码,并且可以正常工作。现在我不明白为什么要得到这个。

我的MainActivity类:

@Override

我的App Gradle文件:

package fr.versus.versus;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.login.LoginManager;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.safetynet.SafetyNet;
import com.google.android.gms.safetynet.SafetyNetApi;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    public WebView webview;

    @SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);

        this.webview = findViewById(R.id.webview);

        this.webview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
            }
        });

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WebView.setWebContentsDebuggingEnabled(true);
        }

        this.webview.addJavascriptInterface(new JsObject(this), "android");

        WebSettings settings = this.webview.getSettings();

        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);

        settings.setAllowFileAccessFromFileURLs(true);
        settings.setAllowUniversalAccessFromFileURLs(true);

        CookieManager.setAcceptFileSchemeCookies(true);

        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            cookieManager.setAcceptThirdPartyCookies(webview, true);
        }

        printHashKey(this);

        this.webview.loadUrl("file:///android_asset/main.html");
    }

    @Override
    protected void onActivityResult(Integer requestCode, Integer resultCode,Intent data) {
        CallbackManager callbackManager = CallbackManager.Factory.create();

        callbackManager.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);

        Log.e("FACEBOOK",AccessToken.getCurrentAccessToken().getToken());

        callJavascript("FACEBOOK", "{\"token\": \"" + AccessToken.getCurrentAccessToken().getToken() + "\",\"id\": " + AccessToken.getCurrentAccessToken().getUserId() + "}");
    }

    private void callJavascript(String returnType, String value) {
        final String returnTypeFormated = "\"" + returnType.replace("\"", "\\\"") + "\"";
        final String valueFormated = "\"" + value.replace("\"", "\\\"") + "\"";

        this.webview.post(new Runnable(){
            @Override
            public void run() {

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                    MainActivity.this.webview.evaluateJavascript("inter(" + returnTypeFormated + "," + valueFormated + ");",null);
                }else{
                    MainActivity.this.webview.loadUrl("javascript:inter(" + returnTypeFormated + "," + valueFormated + ");");
                }
            }
        });
    }

    private class JsObject {

        private Context context;

        JsObject(Context ctx) {
            context = ctx;
        }

        @JavascriptInterface
        void getFacebook() {
            LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "user_friends", "email", "user_birthday", "user_photos"));
        }

        @JavascriptInterface
        void getReCapcha(){
            SafetyNet.getClient(context).verifyWithRecaptcha("6LfQe2cUAAAAAFxXDxFxZlICH9TN8NV6cBJRcoNq")
                .addOnSuccessListener(MainActivity.this, new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
                    @Override
                    public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
                        if (!response.getTokenResult().isEmpty()) {
                            MainActivity.this.callJavascript("CAPCHA",response.getTokenResult());
                        }
                    }
                })
                .addOnFailureListener(MainActivity.this, new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        if (e instanceof ApiException) {
                            ApiException apiException = (ApiException) e;
                            Log.d("CAPCHA", "Error message: " +
                                    CommonStatusCodes.getStatusCodeString(apiException.getStatusCode()));
                        } else {
                            Log.d("CAPCHA", "Unknown type of error: " + e.getMessage());
                        }
                    }
                });
        }
    }
}

为什么不能覆盖此方法?

1 个答案:

答案 0 :(得分:1)

是因为以下方法不存在。

@Override
protected void onActivityResult(Integer requestCode, Integer resultCode,Intent data) {
    ...
}

使用

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    ...
}

Android Activity