【问题标题】:Is there any way to force Location setting in android to enable vai app有什么方法可以强制在 android 中设置位置以启用 vai 应用程序
【发布时间】:2016-02-26 10:57:13
【问题描述】:

我正在创建一个需要激活位置服务的 android 应用。有什么办法可以强制在android中启用位置服务。我不希望用户进入设置页面并启用它。

PS:该应用不适合公众使用,如果需要,可以为该应用提供 root 访问权限。

【问题讨论】:

    标签: android android-location android-settings


    【解决方案1】:

    试试这个:

    public static void locationChecker(GoogleApiClient googleApiClient, final Activity activity) {
            LocationRequest locationRequest = LocationRequest.create();
            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            locationRequest.setInterval(30 * 1000);
            locationRequest.setFastestInterval(5 * 1000);
            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();
                                             final LocationSettingsStates state = result.getLocationSettingsStates();
                                             switch (status.getStatusCode()) {
                                                 case LocationSettingsStatusCodes.SUCCESS:
                                                     // All location settings are satisfied. The client can initialize location
                                                     // requests here.
                                                     break;
                                                 case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                                                     // Location settings are not satisfied. But could be fixed by showing the user
                                                     // a dialog.
                                                     try {
    
                                                         // Show the dialog by calling startResolutionForResult(),
                                                         // and check the result in onActivityResult().
                                                         status.startResolutionForResult(
                                                                 activity, 1000);
                                                     } catch (IntentSender.SendIntentException e) {
                                                         // Ignore the error.
                                                     }
                                                     break;
                                                 case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                                                     // Location settings are not satisfied. However, we have no way to fix the
                                                     // settings so we won't show the dialog.
                                                     break;
                                             }
                                         }
                                     }
    
            );
        }
    

    在您的 Activity 中,将以下代码放入 onActivityResult():

     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
             if (requestCode == 1000) {
                if (resultCode == Activity.RESULT_CANCELED) {
                    finish();
                } else {
                    // this should not be needed, but apparently is in 8.1
                    //user granted the permission
                }
            }
    
        }
    

    【讨论】:

    • 因此,如果未启用位置设置,则用户会收到一个对话框,要求用户更改权限。我说得对吗@Mayank
    • 是的。用户可以在应用程序上下文中启用或禁用位置。
    • 不,我也不希望用户与该对话框进行交互
    • 系统权限分为几个保护级别。要了解的两个最重要的保护级别是正常权限和危险权限。所有正常权限 a​​ndroid 本身在未经用户同意的情况下授予它,但另一方面,对于危险权限,它要求用户授予权限。位置属于危险权限组,因此如果不要求用户明确执行此操作,则无法打开该位置。
    • 所以即使是 root 访问也无法帮助我解决这个问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 2019-07-12
    相关资源
    最近更新 更多