【问题标题】:Android force enable GPSAndroid强制启用GPS
【发布时间】:2018-04-19 15:48:00
【问题描述】:

我的应用依赖于 GPS 信号,因此我在应用启动时要求用户启用 GPS。但是,如果用户拒绝,应用程序将继续运行。

GPS 对我的应用至关重要,因此如果用户拒绝,我想再次显示该对话框。如果不可能,请在用户未授予权限的情况下关闭应用程序。

你能帮忙吗?

非常感谢。

 protected void createLocationRequest() {
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);

    SettingsClient client = LocationServices.getSettingsClient(this);
    Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());

    task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            // All location settings are satisfied. The client can initialize
            // location requests here.
            // ...
        }
    });

    task.addOnFailureListener(this, new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            if (e instanceof ResolvableApiException) {
                // Location settings are not satisfied, but this can be fixed
                // by showing the user a dialog.
                try {
                    // Show the dialog by calling startResolutionForResult(),
                    // and check the result in onActivityResult().
                    ResolvableApiException resolvable = (ResolvableApiException) e;
                    resolvable.startResolutionForResult(MainActivity.this,
                            1);
                } catch (IntentSender.SendIntentException sendEx) {
                    // Ignore the error.
                }
            }
        }
    });
}

【问题讨论】:

  • 您可以随时再次调用权限请求。没有什么能阻止这一点。弹出一个对话框来解释没有权限应用程序无法运行可能会很好。
  • 是的,但 GPS 确实必不可少,并且在应用程序运行时一直使用它。那么如果用户不允许打开 GPS,有没有办法再次显示对话框或关闭应用程序?
  • 如果你想减少样板,我可以建议你看看这个库:github.com/tbruyelle/RxPermissions。我在生产中使用它,它就像一个魅力。
  • 如何在onActivityResult失败时再次调用startResolutionForResult,通过启动一个新线程,防止函数调用的堆栈溢出?
  • 为什么不直接使用ContextCompat.checkSelfPermission 来检查权限并阻止用户做任何事情,这是不被授予的

标签: android gps location android-6.0-marshmallow


【解决方案1】:

创建一个监听器来监听设备 GPS 上的模式变化,在您的应用程序库中实现监听器。然后相应地处理函数。如果启用了 GPS,则应用程序会继续运行,否则会显示一个警告框,要求用户启用 gps,因为您的所有活动都来自您的应用程序库。

您可以查看here 的答案,了解如何确定 GPS 是否已启用。

您还可以查看本教程here,了解如何创建监听器。

【讨论】:

    【解决方案2】:

    解决办法是重写 onActivityResult 方法,用匹配的 requestCode 处理。

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == 6) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
                // The user picked a contact.
                // The Intent's data Uri identifies which contact was selected.
    
                // Do something with the contact here (bigger example below)
            }else{
                createLocationRequest();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 2019-04-19
      • 2011-04-30
      相关资源
      最近更新 更多