在Android Marshmallow 6.0 webview的运行时请求权限位置

时间:2016-09-23 14:51:37

标签: java android webview permissions

我有一个WebView,加载了一些页面需要GeolocationPermissions的权限。为此,我重写了onGeolocationPermissionsShowPrompt()。

此外,我的应用程序使用新的权限模型定位SDK 23(Android M)。我使用此代码但无法正常工作,无法显示对话框且无法确定当前位置,有人可以帮助我吗?

感谢

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);
}

询问请求

@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 {
        }
    }
}

public class GeoWebChromeClient extends WebChromeClient {
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin GeolocationPermissions.Callback callback) {
                   callback.invoke(origin, true, false);
    }
}

1 个答案:

答案 0 :(得分:0)

我已经通过github上的android arsenal位置管理器解决了这个问题,也许有一天会对其他人有用:)

LocationManager