gpt4 book ai didi

JavascriptInterface 函数导致网页上的 javascript 崩溃

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

我的 Android 应用程序中有一个 WebView 。我只想在浏览器上查看网页时才显示横幅。我创建了一个界面,它测试得很好,但由于网页无法识别该功能,因此页面上的其他 javascript 无法加载。这是我的代码:

接口(interface)类

import android.content.Context;
import android.webkit.JavascriptInterface;


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 Boolean getUser() {

return true;
}
}

Activity 类别

public class MainActivity extends Activity {


ProgressBar progressBar;
WebView webview;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);





setContentView(R.layout.activity_main);

webview = (WebView)findViewById(R.id.webview);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
webview.addJavascriptInterface(new WebAppInterface(this),"Android");
this.webview.getSettings().setUserAgentString(
this.webview.getSettings().getUserAgentString()
+ " "
+ getString(R.string.user_agent_suffix)
);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);


webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);

} else {
progressBar.setVisibility(View.VISIBLE);

}
}
});





webview.setWebViewClient(new WebViewClient() {

public boolean shouldOverrideUrlLoading(WebView view, String url) {

view.loadUrl(url);
return true;
}


});
webview.loadUrl("http://www.getonfleek.com");
ParsePush.subscribeInBackground("test");
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
} else {
super.onBackPressed();
}
}




}

HTML

</script>
<script type="text/javascript">
$(document).ready(function(){
var user = Android.getUser();
if(user == null){
//do something on web
}else{
//do something in app
}
});
</script>

最佳答案

在尝试调用接口(interface)之前检查并确保该接口(interface)存在。

<script type="text/javascript">
$(document).ready(function(){
var user = null;
if(typeof Android !== 'undefined'){
user = Android.getUser();
//do something in app
}else{
//do something on web
}
});
</script>

关于JavascriptInterface 函数导致网页上的 javascript 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33528206/

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