无法覆盖 OnActivityResult 方法
Can't override OnActivityResult method
我不太擅长 Android Studio。
我启动了一个 WebView 项目,如果您忘记了密码,我们可以登录/注册并编辑您的密码。我想添加从 Facebook 登录。我实施了 Facebook SDK 并在我的开发者应用程序页面中添加了包和哈希键。我按照文档进行操作,并选择让用户从我的网页登录,并使用我的 JavascriptInterface 中的 logInWithReadPermissions
方法调用 Facebook SDK。
我从我的网页上得到结果,return结果。要获取此数据,我必须重写 Activity
的方法 onActivityResult
(从 AppCompatActivity
扩展而来)。 Android Studio 说我无法覆盖 onActivityResult
方法,因为此方法不会从超类中覆盖。
此外,如果我删除 @Override
,则永远不会调用该方法。
我从一个正在运行的旧 kotlin 项目复制了这段代码。我不明白为什么我现在收到这个错误。
我的 MainActivity Class:
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());
}
}
});
}
}
}
我的应用Gradle 文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "fr.versus.versus"
minSdkVersion 17
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.facebook.android:facebook-login:4.35.0'
implementation 'com.google.android.gms:play-services-safetynet:15.0.1'
implementation 'com.stripe:stripe-android:6.1.2'
}
为什么我不能覆盖这个方法?
是因为follow的方法不存在
@Override
protected void onActivityResult(Integer requestCode, Integer resultCode,Intent data) {
...
}
使用
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
...
}
我不太擅长 Android Studio。
我启动了一个 WebView 项目,如果您忘记了密码,我们可以登录/注册并编辑您的密码。我想添加从 Facebook 登录。我实施了 Facebook SDK 并在我的开发者应用程序页面中添加了包和哈希键。我按照文档进行操作,并选择让用户从我的网页登录,并使用我的 JavascriptInterface 中的 logInWithReadPermissions
方法调用 Facebook SDK。
我从我的网页上得到结果,return结果。要获取此数据,我必须重写 Activity
的方法 onActivityResult
(从 AppCompatActivity
扩展而来)。 Android Studio 说我无法覆盖 onActivityResult
方法,因为此方法不会从超类中覆盖。
此外,如果我删除 @Override
,则永远不会调用该方法。
我从一个正在运行的旧 kotlin 项目复制了这段代码。我不明白为什么我现在收到这个错误。
我的 MainActivity Class:
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());
}
}
});
}
}
}
我的应用Gradle 文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "fr.versus.versus"
minSdkVersion 17
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.facebook.android:facebook-login:4.35.0'
implementation 'com.google.android.gms:play-services-safetynet:15.0.1'
implementation 'com.stripe:stripe-android:6.1.2'
}
为什么我不能覆盖这个方法?
是因为follow的方法不存在
@Override
protected void onActivityResult(Integer requestCode, Integer resultCode,Intent data) {
...
}
使用
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
...
}