0
我有成功的正確修正一個布爾值的狀態如下:prefs.putBoolean內onPageFinished
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();
}
});
}
}
}
結束遊戲是讓「Notify1」和「Notify2」設置布爾狀態。但是第一步只是在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
}
的迅速反應非常感謝。請您詳細說明,因爲我不完全明白。 –
我假設你在這行收到錯誤:'SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();',不是嗎? – Bob
這是正確的先生。 –