简体   繁体   中英

Request Permission Location at Runtime for Android Marshmallow 6.0 webview

I have a WebView and some page loaded need the permission for GeolocationPermissions. For that I override onGeolocationPermissionsShowPrompt().

Also, my app targets SDK 23 (Android M) with the new permissions model. 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 :)

LocationManager

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM