[英]Request Permission Location at Runtime for Android Marshmallow 6.0 webview
I have a WebView and some page loaded need the permission for GeolocationPermissions. 我有一个WebView,一些页面加载需要GeolocationPermissions的权限。 For that I override onGeolocationPermissionsShowPrompt().
为此,我重写了onGeolocationPermissionsShowPrompt()。
Also, my app targets SDK 23 (Android M) with the new permissions model. 此外,我的应用程序使用新的权限模型定位SDK 23(Android M)。 I use this code but can't work, can't show the dialog and can't determine current position, could someone help me ?
我使用这段代码却无法正常工作,无法显示对话框而无法确定当前位置,有人可以帮帮我吗?
thanks 谢谢
private static final int REQUEST_INTERNET = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
if (null != actionBar) {
actionBar.hide();
}
if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_INTERNET);
}
String url = "https://google.com";
WebView view = (WebView) this.findViewById(R.id.WebView);
view.setWebViewClient(new MyBrowser());
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.getSettings().setAppCacheEnabled(true);
view.getSettings().setDatabaseEnabled(true);
view.getSettings().setGeolocationEnabled(true);
view.getSettings().setGeolocationEnabled(true);
view.setWebChromeClient(new GeoWebChromeClient());
view.loadUrl(url);
}
asking request 询问请求
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (grantResults[0]== PackageManager.PERMISSION_GRANTED){
} else if(grantResults[0] == PackageManager.PERMISSION_DENIED){
if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This permission is important to Access Location.")
.setTitle("Important permission required");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_INTERNET);
}
});
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_INTERNET);
} else {
}
}
}
and 和
public class GeoWebChromeClient extends WebChromeClient {
@Override
public void onGeolocationPermissionsShowPrompt(String origin GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
}
I have solved this problem by the android arsenal location manager on github, maybe someday useful for someone else :) 我已经通过github上的android arsenal位置管理器解决了这个问题,也许有一天会对其他人有用:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.