SoFunction
Updated on 2025-03-11

Android problem that cannot be located in WebView is solved

I encountered a problem before. I couldn't locate it when using webview. Recently, the 19 majors have no walls, so I can only go to Baidu for a walk. I found that someone said I wanted to do this.

    WebSettings settings = ();
    (true);
    (true);
    (true);
    (true);

    (true);
    String dir = ().getDir("database", Context.MODE_PRIVATE).getPath();
    (dir);
    webChromeClient = new WebChromeClient(){
      @Override
      public void onGeolocationPermissionsShowPrompt(String origin,  callback) {
        (origin, true, true);
        (origin, callback);
      }
    };
    (webChromeClient);
    ("https://xxxxxxxxxxxxxxxxxxxxxxxx");

Many places on the Internet say that after adding this code, you can locate it normally, but after I add it, it still has no use. There is no way, Baidu is not as powerful as Google.
After reading the log, I said that I did not get permission, but the (origin, true, true); in the code is an operation to get positioning permissions.

I thought about it and suddenly thought of applying for permissions dynamically after 6.0.

I'll add the code to apply for permissions dynamically

if (.SDK_INT >= 23) {
      int checkPermission = (, .ACCESS_COARSE_LOCATION);
      if (checkPermission != PackageManager.PERMISSION_GRANTED) {
        (, new String[]{.ACCESS_COARSE_LOCATION}, 1);
        (, new String[]{.ACCESS_FINE_LOCATION}, 1);
      }else {
        ("https://xxxxxxxxxxxxxxxxxxxxxxxx");
      }
    }

If you find that this way, you can get the positioning results normally. Some people may not know where to apply dynamically. In fact, it depends on your specific process. You can also apply when you jump to this page, or when you display the web page.

Finally, the method onGeolocationPermissionsShowPrompt will only be called once, just like dynamically applying for permissions, it will only be called on the first time.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.