JavaScript 界面有效,但需要调用该函数两次
JavaScript interface works but need to call the function twice
我尝试通过在我的网络应用程序中按下一个按钮来设置我的 Android 应用程序的 statusBarColor
。我得到了这个工作,但是当我按下蓝色按钮两次时,应用程序只会显示一个蓝色状态栏。我做错了什么?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.loadUrl("https://myurl.com");
}
public void setColor(String color){
if(Build.VERSION.SDK_INT >= 21){
getWindow().setStatusBarColor(Color.parseColor(color));
}
}
}
--
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void setStatusBarColor(String color) {
((MainActivity)mContext).setColor(color);
}
}
--
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="button" value="White" onClick="setStatusBarColor('#ffffff');" />
<input type="button" value="Blue" onClick="setStatusBarColor('#0073cf')" />
<script type="text/javascript">
function setStatusBarColor(color) {
Android.setStatusBarColor(color);
}
</script>
</body>
</html>
--
任何帮助将不胜感激。我在这里没有选择:-(
更新:
经过更深入的调试,我得到:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
好的,我成功了...我需要在我的 setStatusBarColor android 函数周围添加 "runOnUiThread"。所以我的 setColor 函数需要是:
public void setColor(final String color){
runOnUiThread(new Runnable() {
@Override
public void run() {
if(Build.VERSION.SDK_INT >= 21){
getWindow().setStatusBarColor(Color.parseColor(color));
}
}
});
}
我尝试通过在我的网络应用程序中按下一个按钮来设置我的 Android 应用程序的 statusBarColor
。我得到了这个工作,但是当我按下蓝色按钮两次时,应用程序只会显示一个蓝色状态栏。我做错了什么?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.loadUrl("https://myurl.com");
}
public void setColor(String color){
if(Build.VERSION.SDK_INT >= 21){
getWindow().setStatusBarColor(Color.parseColor(color));
}
}
}
--
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void setStatusBarColor(String color) {
((MainActivity)mContext).setColor(color);
}
}
--
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="button" value="White" onClick="setStatusBarColor('#ffffff');" />
<input type="button" value="Blue" onClick="setStatusBarColor('#0073cf')" />
<script type="text/javascript">
function setStatusBarColor(color) {
Android.setStatusBarColor(color);
}
</script>
</body>
</html>
--
任何帮助将不胜感激。我在这里没有选择:-(
更新:
经过更深入的调试,我得到:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
好的,我成功了...我需要在我的 setStatusBarColor android 函数周围添加 "runOnUiThread"。所以我的 setColor 函数需要是:
public void setColor(final String color){
runOnUiThread(new Runnable() {
@Override
public void run() {
if(Build.VERSION.SDK_INT >= 21){
getWindow().setStatusBarColor(Color.parseColor(color));
}
}
});
}