【发布时间】:2015-10-14 21:39:50
【问题描述】:
我正在尝试使用 Android Studio Emulator(Nexus 5) 将页面加载到 android 应用 webview 中。但是,页面未正确加载并卡在加载屏幕上。该页面确实需要 GeoLocation 权限,但是我已经启用了它们。
下面是我的代码和应用程序的 XML。
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview;
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.help_webview);
//webview use to call own site
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setAllowContentAccess(true);
webview.getSettings().setAllowFileAccess(true);
webview.getSettings().setAppCacheEnabled(true);
webview.getSettings().setAllowUniversalAccessFromFileURLs(true);
webview.getSettings().setAllowContentAccess(true);
webview.getSettings().setGeolocationEnabled(true);
webview.getSettings().setDatabaseEnabled(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
// callback.invoke(String origin, boolean allow, boolean remember);
callback.invoke(origin, true, false);
}
});
webview.loadUrl("https://lit-citadel-6989.herokuapp.com");
}
activity_main.xml
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/help_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:largeHeap="true" />
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example.example" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<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" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
更新: 这肯定是地理定位问题。我没有 gps.config 文件。我似乎找不到一种具体的方法来创建一个或我需要的东西。网站上获取位置的javascript是
navigator.geolocation.getCurrentPosition(function(location){
setLatLng(location);
locationLoaded = true;
callback(lat,lng);
});
然后如何在 android 中设置 webview 以传递用户的当前位置?
【问题讨论】:
-
从 0 开始向 javascript 提供显式静态 lat,lng 值,然后在默认模拟器浏览器中打开页面查看其行为,如果可能,然后尝试获取设备而不是模拟器或至少使用genymotion 用于这类事情,因为它更多地依赖于硬件..顺便说一句,你能粘贴整个 java 类和 javascript 代码吗?