我有以下成功修正布尔值的状态:
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中的布尔状态时出错:
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();
}
});
}
}
}
最终的游戏是拥有&#34; Notify1&#34;和&#34; Notify2&#34;设置布尔值的状态。但第一步是在onPageFinished运行后设置布尔值的状态。
我希望我已经正确地解释了自己,任何帮助都会受到赞赏。非常感谢,艾伦。
完整代码:
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
}
答案 0 :(得分:1)
您应该在context
中传递PreferenceManager.getDefaultSharedPreferences(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));