我对安卓工作室不是很好。
我启动了一个WebView项目,如果你忘记了,我们可以登录/注册并编辑你的密码。我想添加从Facebook登录。我实现了Facebook SDK,并添加了软件包&我的开发人员应用程序页面中的哈希密钥。我遵循了文档,并选择从我的网页登录用户,并从我的JavascriptInterface中使用logInWithReadPermissions
方法调用Facebook SDK。
我得到结果并从我的网页返回结果。要获得这些数据,我必须重写Activity
(从AppCompatActivity
扩展而来(的方法onActivityResult
。安卓工作室说我不能重写onActivityResult
方法,因为这个方法不会从超类重写。此外,如果我移除@Override
,则永远不会调用该方法。
我从一个正在运行的旧kotlin项目中复制了这段代码。我不明白为什么我现在会犯这个错误。
我的主要活动类别:
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());
}
}
});
}
}
}
我的应用程序渐变文件:
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);
...
}
安卓活动