[Switch] Android use WebView Orientation

Article from: https: //www.jianshu.com/p/d32d3641741f

Recently encountered a problem, there is a demand for the use of WebView to load a web page url, H5 to obtain position location information via js. H5 functions previously carried out development needs of location information, but past programs are using the native Android positioning (integrated High German / Baidu SDK), then the location information to the H5 to achieve, but this time taking into consideration the size of apk problem (not integrated high German SDK) as well as "lazy" (do not write code positioning), so we are ready to let H5 do. I would have thought that this program should be very simple, did not expect to encounter a big bug - H5 can not get to the location. At this time wanted to throw the first pot to H5 (how my code may be a problem? !!), but the iOS so that, mobile phones native browser to open the url Ye Hao, suddenly forced to look ignorant, and instantly hit the face . . . Okay, ready to fill in the pit it ~

1. First, you want to get location information, be sure to point to locate permission! See Code

<!-- 网络权限,加载网络网页需要联网 -->
<uses-permission android:name="android.permission.INTERNET" /> <!-- 粗略定位权限,允许一个程序通过网络获取粗略的位置 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 精确定位权限,允许一个程序访问精确位置(GPS定位) --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

2. when you enter the page, usually onCreate () method, the dynamic application permission (I do not believe your app does not fit Android 6.0+). After obtaining permission, and then call webview of loadUrl () relatively safe ~

3. webview relevant code

    WebSettings webSettings = mWebView.getSettings();
    // 允许调用 JS,因为网页地图使用的是 JS 定位
    webSettings.setJavaScriptEnabled(true);
    //启用数据库
    webSettings.setDatabaseEnabled(true);
    //启用地理定位,默认为true webSettings.setGeolocationEnabled(true); //设置定位的数据库路径 String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); webSettings.setGeolocationDatabasePath(dir); //开启DomStorage缓存 webSettings.setDomStorageEnabled(true); mWebView.setWebChromeClient(new WebChromeClient() { @Override public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { callback.invoke(origin, true, false); super.onGeolocationPermissionsShowPrompt(origin, callback); } }); 

The above code, feel setJavaScriptEnabled (), setGeolocationEnabled (), setWebChromeClient () is very important, and the rest should be written not write it, but not tested

4. preliminary summary

Step 1 and step 2, for guiding the user turns on the main device location authority; Step 3 of the guide is positioned to open a browser user permissions.
(Focus here !!!)
can however! After break, the positioning device permission to get my code, why not go onGeolocationPermissionsShowPrompt WebChromeClient in () method? ! ! why? ? (Question mark black face)

5. access to information

So, I'm all kinds of Baidu google, variety recommended targetSdkVersion to 23, but who can be killed can not be humiliated, if not to shame, at least tell me why disgrace! Finally, I was the official explanation found in:

 
Official information

 

Key here:

Note that for applications targeting Android N and later SDKs (API level > [Build.VERSION_CODES.M](https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#M)) this method is only called for requests originating from secure origins such as https. On non-secure origins geolocation requests are automatically denied.

Translation is as follows:

Note that, for the application for Android N and subsequent SDKs (API level> Build.VERSION_CODES.M) This method only requests the secure source (e.g., https) from the call. On the non-safe source, the request will be automatically rejected location.

Well, my url H5 is the beginning of "http", it is automatically blocked. How, does not surprise surprise, meaning not an accident? !

6. Summary

WebView positioning solutions:
Option One: webview positioning, obediently positioning methods using native, after passing location information to the H5 ~
Option II: the use of high moral map of assisted H5 page positioning, has been tested, feasible!
Option Three: Reduce to 23 targetSdkVersion version, lazy law requires lower
Option 4: Upgrade http to https



Author: Typist husband less
link: https: //www.jianshu.com/p/d32d3641741f
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin www.cnblogs.com/sungong1987/p/11130292.html
Recommended