gpt4 book ai didi

java - onPageFinished 内的 prefs.putBoolean

转载 作者:行者123 更新时间:2023-12-02 12:24:21 26 4
gpt4 key购买 nike

我有以下内容成功地正确修改了 boolean 值的状态:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

wv = (WebView) findViewById(R.id.wv);
//Enable JavaScript
wv.getSettings().setJavaScriptEnabled(true);
wv.setFocusable(true);
wv.setFocusableInTouchMode(true);
//Set Render Priority To High
wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wv.getSettings().setDomStorageEnabled(true);
wv.getSettings().setDatabaseEnabled(true);
wv.getSettings().setAppCacheEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

//Load Url
wv.loadUrl("https://str8red.com/");
wv.setWebViewClient(new myWebClient());

SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
prefs.putBoolean("notifications_team_pick",false);
prefs.putBoolean("notifications_results", false);

prefs.commit();

}

但是,当我希望在下面的代码中的 onPageFinished 中设置 boolean 值的状态时,会出现错误:

public class myWebClient extends WebViewClient {

@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);

String CurrentURL = wv.getUrl();

if (CurrentURL == "https://str8red.com/") {
wv.evaluateJavascript("fromAndroid()", new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
String[] separated = value.split(" ");
//separated[0]; // logged in True Or False
//separated[1]; // Notifications 1 or 0
//separated[2]; // More Notifications or 1 or 0
String loggedIn = separated[0].replace("\"", "");
String Notify1 = separated[1].replace("\"", "");
String Notify2 = separated[2].replace("\"", "");

SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
prefs.putBoolean("notifications_team_pick",false);
prefs.putBoolean("notifications_results", true);

prefs.commit();
}
});
}
}
}

最终的结果是让“Notify1”和“Notify2”设置 boolean 值的状态。但第一步只是在 onPageFinished 运行后设置 boolean 值的状态。

我希望我已经正确解释了自己,任何帮助将不胜感激。非常感谢,艾伦。

完整代码:

package com.str8red.str8red;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends AppCompatActivity {
WebView wv;
Boolean fish;
Boolean shark;

// When Back Pressed Go Back
@Override
public void onBackPressed() {
if (wv.canGoBack()) {
wv.goBack();
} else {
super.onBackPressed();
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

wv = (WebView) findViewById(R.id.wv);
//Enable JavaScript
wv.getSettings().setJavaScriptEnabled(true);
wv.setFocusable(true);
wv.setFocusableInTouchMode(true);
//Set Render Priority To High
wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wv.getSettings().setDomStorageEnabled(true);
wv.getSettings().setDatabaseEnabled(true);
wv.getSettings().setAppCacheEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

//Load Url
wv.loadUrl("https://str8red.com/");
// wv.setWebViewClient(new myWebClient());
wv.setWebViewClient(new myWebClient(this));

SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
prefs.putBoolean("notifications_team_pick",false);
prefs.putBoolean("notifications_results", false);

prefs.commit();


}



public class myWebClient extends WebViewClient {

private Context context;

public myWebClient(Context context) {
this.context = context;
}

@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);

String CurrentURL = wv.getUrl();

if (CurrentURL == "https://str8red.com/") {
wv.evaluateJavascript("fromAndroid()", new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
String[] separated = value.split(" ");
//separated[0]; // logged in True Or False
//separated[1]; // Notifications 1 or 0
//separated[2]; // More Notifications or 1 or 0
String loggedIn = separated[0].replace("\"", "");
String Notify1 = separated[1].replace("\"", "");
String Notify2 = separated[2].replace("\"", "");

SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();
prefs.putBoolean("notifications_team_pick",false);
prefs.putBoolean("notifications_results", true);

prefs.commit();
}
});
}
}
}

//Settings Button
public void btnSettings_onClick(View view) {
Intent intent=new Intent(this,SettingsActivity.class);
startActvity(intent);
}

private void startActvity(Intent intent) {
startActivity(intent);
}
//End of Settings Button

//Play Button

public void btnPlay_onClick(View view) {
wv.loadUrl("https://str8red.com/selectteams/0/0");
wv.setWebViewClient(new myWebClient());
}
//End of Play Button

}

最佳答案

您应该在 PreferenceManager.getDefaultSharedPreferences(context) 中传递一个 context 作为参数。

您传递的thisWebViewClient

有一个接受 myWebClient 中的 Context 的构造函数。

private Context context;

public myWebClient(Context context) {
this.context = context;
}

@Override
public void onPageFinished(WebView view, String url) {
.....

SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();

}

并在创建 WebViewClient 时传递上下文:

wv.setWebViewClient(new myWebClient(this));

关于java - onPageFinished 内的 prefs.putBoolean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45553158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com