当前位置:主页 > 移动开发 > Android代码 >

android 6.0下webview的定位权限设置方法

时间:2023-02-28 11:47:44 | 栏目:Android代码 | 点击:

如下所示:

WebView webView = (WebView)findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
 
//webview支持js脚本
webSettings.setJavaScriptEnabled(true);
 
//启用数据库 
webSettings.setDatabaseEnabled(true); 
 
//设置定位的数据库路径 
String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); 
webSettings.setGeolocationDatabasePath(dir); 
 
//启用地理定位
webSettings.setGeolocationEnabled(true); 
 
//开启DomStorage缓存
webSettings.setDomStorageEnabled(true) 
 
//配置权限
webView.setWebChromeClient(new WebChromeClient() {
 @Override
 public void onReceivedIcon(WebView view, Bitmap icon) {
  super.onReceivedIcon(view, icon);
 
}
 
@Override
public void onGeolocationPermissionsShowPrompt(String origin,Callback callback) {
 callback.invoke(origin, true, false); 
 super.onGeolocationPermissionsShowPrompt(origin, callback);
 
}
});
//在Androidmanifest.xml中开启权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Android6.0版本需要加上:

   if (Build.VERSION.SDK_INT >= 23) {
    int checkPermission = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION);
    if (checkPermission != PackageManager.PERMISSION_GRANTED) {
     ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
     ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
     Log.d("TTTT", "弹出提示");
    }
   }

您可能感兴趣的文章:

相关文章