它在 android 8 中工作正常,页面正在重定向,但在更高的 SDK 版本(如 android 2.3.3 API 级别 10)中,应用程序只需单击重定向链接即可退出。
xml文件:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" >
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_marginTop="80dp"
android:layout_height="189dp" />
</LinearLayout>
代码:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private WebView mainWebView = null;
Boolean javascriptInterfaceBroken;
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xml);
mainWebView = (WebView) findViewById(R.id.webView1);
mainWebView.getSettings().setJavaScriptEnabled(true);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mainWebView.loadUrl("file:///android_asset/www/index.html");
mainWebView.addJavascriptInterface(new nextpage(this), "Android");
}
private class MyCustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
.html 文件:
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening" >Connecting to Device</p>
<p class="event received">Device is Ready</p>
<a href="javascript:Android.launchActivity()">cordova</a>
</div>
</div>
<script type="text/javascript" src="cordova-2.2.0.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
另一类:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
public class nextpage {
private Context context;
public nextpage(Context context) {
this.context = context;
}
public void launchActivity() {
context.startActivity(new Intent((Activity)context,deal.class));
}
}
日志错误:
01-28 11:48:58.287: D/dalvikvm(4093): GC_EXTERNAL_ALLOC freed 51K, 53% free 2576K/5379K, external 716K/1038K, paused 47ms
01-28 11:48:59.347: D/szipinf(4093): Initializing inflate state
01-28 11:48:59.347: D/szipinf(4093): Initializing zlib to inflate
01-28 11:49:06.657: W/dalvikvm(4093): JNI WARNING: jarray 0x4058fa70 points to non-array object (Ljava/lang/String;)
01-28 11:49:06.667: I/dalvikvm(4093): "WebViewCoreThread" prio=5 tid=9 NATIVE
01-28 11:49:06.667: I/dalvikvm(4093): | group="main" sCount=0 dsCount=0 obj=0x40510790 self=0x1f9650
01-28 11:49:06.667: I/dalvikvm(4093): | sysTid=4102 nice=0 sched=0/0 cgrp=default handle=2071192
01-28 11:49:06.667: I/dalvikvm(4093): | schedstat=( 1354713686 909395665 294 )
01-28 11:49:06.667: I/dalvikvm(4093): at android.webkit.WebViewCore.nativeTouchUp(Native Method)
01-28 11:49:06.667: I/dalvikvm(4093): at android.webkit.WebViewCore.nativeTouchUp(Native Method)
01-28 11:49:06.667: I/dalvikvm(4093): at android.webkit.WebViewCore.access$3300(WebViewCore.java:53)
01-28 11:49:06.677: I/dalvikvm(4093): at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1158)
01-28 11:49:06.677: I/dalvikvm(4093): at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 11:49:06.677: I/dalvikvm(4093): at android.os.Looper.loop(Looper.java:123)
01-28 11:49:06.677: I/dalvikvm(4093): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:629)
01-28 11:49:06.677: I/dalvikvm(4093): at java.lang.Thread.run(Thread.java:1019)
01-28 11:49:06.677: E/dalvikvm(4093): VM aborting