我創建了一個尚未發佈的網站,因此我現在不會分享它。但它是一個使用PHP Framework Nette和Twitter Bootstrap作爲前端框架的網站。Android Working WebView 2016
我想創建簡單的Android和iOS應用程序。這兩個應用程序與webview。它在iOS上正常工作,但是,我無法在Android上開展工作。我瀏覽了很多類似的問題,但他們都沒有幫助我。
那麼,我究竟需要什麼?
地理位置訪問
顯示我的網站在網頁視圖
我的代碼:
package com.URL.URL;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.webkit.GeolocationPermissions;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
public class MainActivity extends Activity {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
WebView view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setDomStorageEnabled(true);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setGeolocationEnabled(true);
view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
view.getSettings().setAppCacheEnabled(true);
view.getSettings().setDatabaseEnabled(true);
//view.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U;`Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
view.setWebViewClient(new MyBrowser());
view.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
view.loadUrl("https://www.URL.com");
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://com.URL.URL/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://com.URL.URL/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
這時候我的Android模擬器(4.4試過沒有工作, 5.1,6,7),但它工作時,我在我的舊三星下載Android版本4.4.4。
我不知道我的網站是否沒有問題,因爲當我嘗試加載google.com時,它工作正常..但即使當我在我的菜單中輸入字符串「aaa」並試圖在webview中打開它,它不起作用。我在網頁視圖和瀏覽器的網站
截圖:
我還想添加地理位置訪問彈出窗口。
所有這些都適用於iOS,但我真的不知道如何在Android上執行此操作。我主要是Web開發人員,所以請原諒我亂碼的Android代碼,這實際上是我第一次使用Android Studio。感謝您的任何幫助!
PS:我想在應用程序中打開它,而不是作爲新的瀏覽器選項卡。
錯誤:
09-16 15:01:01.578 2390-2512/com.URL.URL E/libEGL: validate_display:99 error 3008 (EGL_BAD_DISPLAY)
09-16 15:01:01.972 2390-2512/com.URL.URL E/chromium: [ERROR:gles2_cmd_decoder.cc(2167)] [GroupMarkerNotSet(crbug.com/242999)!:A08E32ECFE7F0000]GL ERROR :GL_INVALID_OPERATION : BackFramebuffer::Create: <- error from previous GL command
09-16 15:01:06.059 2390-2390/com.URL.URL E/cr_LocationProvider: Caught security exception while registering for location updates from the system. The application does not have sufficient geolocation permissions.
警告:
09-16 15:01:00.562 2390-2390/com.URL.URL W/System: ClassLoader referenced unknown path: /system/app/webview/lib/x86_64
09-16 15:01:00.951 2390-2468/com.URL.URL W/cr_media: Requires BLUETOOTH permission
09-16 15:01:01.116 2390-2390/com.URL.URL W/cr_AwContents: onDetachedFromWindow called when already detached. Ignoring
09-16 15:01:01.353 2390-2390/com.URL.URL W/gralloc_ranchu: Gralloc pipe failed
09-16 15:01:01.526 2390-2390/com.URL.URL W/art: Attempt to remove non-JNI local reference, dumping thread
09-16 15:01:01.540 2390-2397/com.URL.URL W/art: Suspending all threads took: 9.484ms
09-16 15:01:02.184 2390-2390/com.URL.URL W/art: Attempt to remove non-JNI local reference, dumping thread
09-16 15:01:02.770 2390-2390/com.URL.URL W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 2390
09-16 15:01:02.933 2390-2390/com.URL.URL W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 2390
09-16 15:01:03.059 2390-2390/com.URL.URL W/art: Attempt to remove non-JNI local reference, dumping thread
09-16 15:01:03.597 2390-2390/com.URL.URL W/art: Attempt to remove non-JNI local reference, dumping thread
09-16 15:01:03.980 2390-2390/com.URL.URL W/art: Attempt to remove non-JNI local reference, dumping thread
09-16 15:01:04.058 2390-2397/com.URL.URL W/art: Suspending all threads took: 26.284ms
09-16 15:01:05.010 2390-2390/com.URL.URL W/art: Attempt to remove non-JNI local reference, dumping thread
09-16 15:01:05.346 2390-2390/com.URL.URL W/art: Attempt to remove non-JNI local reference, dumping thread
09-16 15:01:05.704 2390-2390/com.URL.URL W/art: Attempt to remove non-JNI local reference, dumping thread
09-16 15:01:06.044 2390-2397/com.URL.URL W/art: Suspending all threads took: 10.006ms
09-16 15:01:06.182 2390-2390/com.URL.URL W/art: Attempt to remove non-JNI local reference, dumping thread
09-16 15:01:06.216 2390-2401/com.URL.URL W/art: Suspending all threads took: 8.273ms
09-16 15:01:06.485 2390-2390/com.URL.URL W/art: Attempt to remove non-JNI local reference, dumping thread
09-16 15:01:06.729 2390-2390/com.URL.URL W/art: Attempt to remove non-JNI local reference, dumping thread
09-16 15:01:07.035 2390-2390/com.URL.URL W/art: Attempt to remove non-JNI local reference, dumping thread
09-16 15:01:07.277 2390-2390/com.URL.URL W/art: Attempt to remove non-JNI local reference, dumping thread
09-16 15:01:10.719 2390-2390/com.URL.URL W/art: Attempt to remove non-JNI local reference, dumping thread
清單:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
您是否也可以添加日誌以及日誌會告訴您爲什麼它失敗?同時希望您擁有清單文件中的所有權限 –
Hello Amod,感謝您的回覆,我更新了錯誤信息,警告和清單權限。 – vohratom
你可以嘗試添加 引用這個鏈接http:// stackoverflow。com/questions/18372931/simple-webview-example-caught-security-exception –