开启GPS功能的网页检视 [英] Turn on GPS function webview

查看:59
本文介绍了开启GPS功能的网页检视的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站有一个webView,需要访问用户位置,它可以通过按下按钮并在表单中显示位置来实现.我需要一种从webView询问用户在单击按钮时关闭gps的方法(它具有 id ="locate button" ).我该如何在Android Studio中做到这一点?

I've got a webView for my website that needs to access the user location, and it does that by pressing a button and displaying the position in a form. I need a way from the webView to ask the user to turn on the gps if it's off when clicking on the button (it has id="locate button"). How can I do that from Android studio?

我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {

private WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActivityCompat.requestPermissions(this, new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
    }, 0);

    webview = (WebView) findViewById(R.id.webView);

    webview.setWebViewClient(new WebViewClient());
    webview.getSettings().setAppCacheEnabled(true);
    webview.getSettings().setDatabaseEnabled(true);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setDomStorageEnabled(true);
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webview.getSettings().setGeolocationEnabled(true);
    webview.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
        }
    });
    webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url)
        {
            // hide element by class name
            webview.loadUrl("javascript:(function() { " + "document.getElementsByClassName('menu-icone-container')[0].style.display='none';  })()");
        }
    });
    webview.loadUrl("http://www.example.it");
}

我找到了一种方法来检查gps是否已关闭并提示用户进行gps设置,但是我只能在应用启动时调用此方法,而不能在按下按钮时调用

I found a way to check if gps is turned off and prompt the user to gps settings, but I am only able to call this method when the app launches, not when i press the button

 public void CheckEnableGPS() {
    String provider = Settings.Secure.getString(getContentResolver(),
            Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if (!provider.contains("gps")){
        showSettingAlert();
    }

}

public void showSettingAlert()
{
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    alertDialog.setTitle("Impostazioni GPS");
    alertDialog.setMessage("Per ottenere la posizione corrente il GPS deve essere attivo, attivarlo?");
    alertDialog.setPositiveButton("Impostazioni", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    });
    alertDialog.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    alertDialog.show();
}

推荐答案

使用以下代码向用户显示启用位置设置的提示,不需要用户打开手机的设置窗口:

Use this code to show a prompt for the user to enable Location settings, it doesn't require the user to open the settings window of the phone:

private void displayLocationSettingsRequest(Context context) {
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API).build();
    googleApiClient.connect();
    final String TAG = "YOUR-TAG-NAME";
    final int REQUEST_CHECK_SETTINGS = 0x1;

    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(10000);
    locationRequest.setFastestInterval(10000 / 2);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
    builder.setAlwaysShow(true);

    PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    Log.i(TAG, "All location settings are satisfied.");
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");

                    try {
                        // Show the dialog by calling startResolutionForResult(), and check the result
                        // in onActivityResult().
                        status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e) {
                        Log.i(TAG, "PendingIntent unable to execute request.");
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
                    break;
            }
        }
    });
}

这篇关于开启GPS功能的网页检视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆