- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下内容成功地正确修改了 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
作为参数。
您传递的this
是WebViewClient
。
有一个接受 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/
我正在为一个 Activity 打包,有时我的 boolean isLive 为 null。当我执行以下操作时。 Bundle b = new Bundle(); b.putBoolean("isLi
我有以下内容成功地正确修改了 boolean 值的状态: protected void onCreate(Bundle savedInstanceState) { super.onCreate
当 sharedpreferences 中的 chebkox 更改状态时,我有一个 if 循环。但是当状态改变时,我无法设置 putBoolean("blabla", false);。例如:用户点击复
我是一名优秀的程序员,十分优秀!