【问题标题】:Turn on Location Providers Programmatically in Android在 Android 中以编程方式打开位置提供程序
【发布时间】:2012-02-17 12:27:25
【问题描述】:

如果已关闭,是否可以通过编程方式打开设备上的 LocationProviders(GPS 提供商/网络提供商)?

【问题讨论】:

    标签: android location-provider


    【解决方案1】:

    不,不是, 但您可以打开定位服务设置窗口:

    context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
    

    【讨论】:

      【解决方案2】:

      以高精度或省电模式启用定位,无需用户访问设置

      https://android-developers.googleblog.com/2015/03/google-play-services-70-places-everyone.html

      【讨论】:

      • 从 Google Play Services 7.0 开始,这应该是公认的答案。您可以在不离开应用程序的情况下进行应用程序更新设置。见上面的链接。
      • 链接并没有说太多关于实现的内容,我错过了什么吗?
      • 话虽如此,这不会按要求以编程方式打开位置服务或特定提供商。它向用户显示一个对话框,其中包含打开位置服务的选项。
      • @MikeL 是的,您不能在没有用户干预的情况下自动打开位置,因为这会消耗用户的数据和电池
      【解决方案3】:
      private void turnGPSOn(){
      String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
      
      if(!provider.contains("gps")){ //if gps is disabled
          final Intent poke = new Intent();
          poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
          poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
          poke.setData(Uri.parse("3")); 
          sendBroadcast(poke);
      }
      }
      

      但此代码仅支持 APK 8。

      【讨论】:

        【解决方案4】:

        是的,但您必须是超级用户 (sudo),您的设备必须是 root。

        【讨论】:

          【解决方案5】:

          在最近的 Marshmallow 更新中,即使启用了位置设置,您的应用也需要明确请求许可。推荐的方法是显示应用程序的权限部分,用户可以在其中根据需要切换权限。执行此操作的代码 sn-p 如下:

          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    
              if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    
                  final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                  builder.setTitle("Location Permission");
                  builder.setMessage("The app needs location permissions. Please grant this permission to continue using the features of the app.");
                  builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
                          requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
    
                      }
                  });
                  builder.setNegativeButton(android.R.string.no, null);
                  builder.show();
              }
          } else {
              LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
              boolean isGpsProviderEnabled, isNetworkProviderEnabled;
              isGpsProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
              isNetworkProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
          
              if(!isGpsProviderEnabled && !isNetworkProviderEnabled) {
                  final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                  builder.setTitle("Location Permission");
                  builder.setMessage("The app needs location permissions. Please grant this permission to continue using the features of the app.");
                  builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
                          Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                          startActivity(intent);
                      }
                  });
                  builder.setNegativeButton(android.R.string.no, null);
                  builder.show();
              }
          }
          

          并重写onRequestPermissionsResult方法如下:

          @Override
          public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
              switch (requestCode) {
                  case PERMISSION_REQUEST_COARSE_LOCATION: {
                      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                          Log.d(TAG, "coarse location permission granted");
                      } else {
                          Intent intent = new Intent();
                          intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                          Uri uri = Uri.fromParts("package", getPackageName(), null);
                          intent.setData(uri);
                          startActivity(intent);
                      }
                  }
              }
          }
          

          另一种方法是您也可以使用SettingsApi 来查询启用了哪些位置提供程序。如果没有启用,您可以提示一个对话框从应用程序内更改设置。

          【讨论】:

            【解决方案6】:

            使用以下代码启动 GPS

            locationManager = ( LocationManager ) getSystemService ( Context.LOCATION_SERVICE );
            locationListener = new LocationListener();
            locationManager.requestLocationUpdates ( LocationManager.GPS_PROVIDER, 0, 0, locationListener );
            

            和下面的代码来停止 GPS

            locationManager.removeUpdates( locationListener );
            

            更多详情请参阅documentations for LocationManagerfor LocationListener

            【讨论】:

            • 我正在谈论以编程方式打开安卓设备的(设置-->位置和安全-->使用无线网络/使用 GPS 卫星)选项。
            猜你喜欢
            • 2023-04-05
            • 2013-10-31
            • 1970-01-01
            • 1970-01-01
            • 2022-11-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多