地理位置无法使用Android<使用Webview的5版本

时间:2017-10-09 08:20:38

标签: java android xml webview

我正在将网站转换为Android应用程序。但是GeoLocation无法在Lollipop以下的版本上运行。那么解决方法应该是什么?

我在my activity_main.xml中有一个简单的WeBView

我的MainActivity.java代码:

public class MainActivity extends ActionBarActivity {

private static final String TAG = MainActivity.class.getSimpleName();
private WebView myWebView;
private WebSettings webSettings;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myWebView = (WebView) findViewById(R.id.webView);
    webSettings = myWebView.getSettings();

    myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    myWebView.getSettings().setBuiltInZoomControls(true);
    myWebView.setWebViewClient(new GeoWebViewClient());
    myWebView.setWebViewClient(new Client());
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.getSettings().setGeolocationEnabled(true);
    myWebView.setWebChromeClient(new GeoWebChromeClient());
    webSettings.setGeolocationEnabled(true);
    if (Build.VERSION.SDK_INT >= 19) {
        myWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    } else if (Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT < 19) {
        myWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

   myWebView.loadUrl("http://webrivers.co.in/green_medic/lock_screen.html"); //change with your website
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
    }

    public class GeoWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // When user clicks a hyperlink, load in the existing WebView
        view.loadUrl(url);
        return true;
    }
    }

    public class GeoWebChromeClient extends WebChromeClient {
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin,

  GeolocationPermissions.Callback callback) {
        // Always grant permission since the app itself requires location
        // permission and the user has therefore already granted it
        callback.invoke(origin, true, false);
    }
   }

   public class Client extends WebViewClient {
    ProgressDialog progressDialog;

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // If url contains mailto link then open Mail Intent
        if (url.contains("mailto:")) {
            // Could be cleverer and use a regex
            //Open links in new browser
            view.getContext().startActivity(
                    new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            // Here we can open new activity
            return true;
        } else {
            // Stay within this webview and load url
            view.loadUrl(url);
            return true;
        }
    }

    //Show loader on url load
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // Then show progress  Dialog
        // in standard case YourActivity.this
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Loading...");
            progressDialog.show();
        }
    }

    // Called when all page resources loaded
    public void onPageFinished(WebView view, String url) {
        try {
            // Close progressDialog
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
                progressDialog = null;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
   }
   }

Android 5以下版本Geo位置工作正常,但无法自动获取地理位置,一旦加载谷歌地图然后工作地理位置请帮帮我。

1 个答案:

答案 0 :(得分:0)

请检查您的网址可能是您的网址问题, 我检查你的代码不同的URL它在Lollipop设备下正常工作