<i id='kSrc7'><tr id='kSrc7'><dt id='kSrc7'><q id='kSrc7'><span id='kSrc7'><b id='kSrc7'><form id='kSrc7'><ins id='kSrc7'></ins><ul id='kSrc7'></ul><sub id='kSrc7'></sub></form><legend id='kSrc7'></legend><bdo id='kSrc7'><pre id='kSrc7'><center id='kSrc7'></center></pre></bdo></b><th id='kSrc7'></th></span></q></dt></tr></i><div id='kSrc7'><tfoot id='kSrc7'></tfoot><dl id='kSrc7'><fieldset id='kSrc7'></fieldset></dl></div>

    • <bdo id='kSrc7'></bdo><ul id='kSrc7'></ul>
  • <small id='kSrc7'></small><noframes id='kSrc7'>

  • <tfoot id='kSrc7'></tfoot>
    <legend id='kSrc7'><style id='kSrc7'><dir id='kSrc7'><q id='kSrc7'></q></dir></style></legend>

        如果禁用,Android 获取位置或提示启用位置服务

        时间:2023-09-02

        • <legend id='o0fDa'><style id='o0fDa'><dir id='o0fDa'><q id='o0fDa'></q></dir></style></legend>

          <small id='o0fDa'></small><noframes id='o0fDa'>

            <tfoot id='o0fDa'></tfoot>
            <i id='o0fDa'><tr id='o0fDa'><dt id='o0fDa'><q id='o0fDa'><span id='o0fDa'><b id='o0fDa'><form id='o0fDa'><ins id='o0fDa'></ins><ul id='o0fDa'></ul><sub id='o0fDa'></sub></form><legend id='o0fDa'></legend><bdo id='o0fDa'><pre id='o0fDa'><center id='o0fDa'></center></pre></bdo></b><th id='o0fDa'></th></span></q></dt></tr></i><div id='o0fDa'><tfoot id='o0fDa'></tfoot><dl id='o0fDa'><fieldset id='o0fDa'></fieldset></dl></div>
              <bdo id='o0fDa'></bdo><ul id='o0fDa'></ul>
                <tbody id='o0fDa'></tbody>
                  本文介绍了如果禁用,Android 获取位置或提示启用位置服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在其他帖子中发现了这个答案的零碎,但我想在这里记录下来以供其他人使用.

                  I've found bits and pieces of this answer scattered through other posts, but I wanted to record it here for others.

                  我怎样才能简单地请求用户的 GPS 和/或网络位置,并且如果他们尚未启用该服务,则提示他们这样做?

                  How can I simply request the user's GPS and/or Network location and, if they haven't enabled the service, prompt them to do so?

                  推荐答案

                  如果您想在按钮按下时捕获位置,请按照以下步骤操作.如果用户没有启用定位服务,这会将他们发送到设置菜单以启用它.

                  If you want to capture the location on a button push, here's how you'd do it. If the user does not have a location service enabled, this will send them to the settings menu to enable it.

                  首先,您必须在清单中添加权限android.permission.ACCESS_COARSE_LOCATION".如果需要GPS(网络定位不够灵敏),添加权限android.permission.ACCESS_FINE_LOCATION",将Criteria.ACCURACY_COARSE"改为Criteria.ACCURACY_FINE"

                  First, you must add the permission "android.permission.ACCESS_COARSE_LOCATION" to your manifest. If you need GPS (network location isn't sensitive enough), add the permission "android.permission.ACCESS_FINE_LOCATION" instead, and change the "Criteria.ACCURACY_COARSE" to "Criteria.ACCURACY_FINE"

                  Button gpsButton = (Button)this.findViewById(R.id.buttonGPSLocation);
                  gpsButton.setOnClickListener(new OnClickListener() {
                  
                      @Override
                      public void onClick(View v) {
                          // Start loction service
                          LocationManager locationManager = (LocationManager)[OUTERCLASS].this.getSystemService(Context.LOCATION_SERVICE);
                  
                          Criteria locationCritera = new Criteria();
                          locationCritera.setAccuracy(Criteria.ACCURACY_COARSE);
                          locationCritera.setAltitudeRequired(false);
                          locationCritera.setBearingRequired(false);
                          locationCritera.setCostAllowed(true);
                          locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);
                  
                          String providerName = locationManager.getBestProvider(locationCritera, true);
                  
                          if (providerName != null && locationManager.isProviderEnabled(providerName)) {
                              // Provider is enabled
                              locationManager.requestLocationUpdates(providerName, 20000, 100, [OUTERCLASS].this.locationListener);
                          } else {
                              // Provider not enabled, prompt user to enable it
                              Toast.makeText([OUTERCLASS].this, R.string.please_turn_on_gps, Toast.LENGTH_LONG).show();
                              Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                              [OUTERCLASS].this.startActivity(myIntent);
                          }
                      }
                  });
                  

                  我的外部班级设置了这个监听器

                  My outer class has this listener set up

                  private final LocationListener locationListener = new LocationListener() {
                  
                      @Override
                      public void onLocationChanged(Location location) {
                          [OUTERCLASS].this.gpsLocationReceived(location);
                      }
                  
                      @Override
                      public void onProviderDisabled(String provider) {}
                  
                      @Override
                      public void onProviderEnabled(String provider) {}
                  
                      @Override
                      public void onStatusChanged(String provider, int status, Bundle extras) {}
                  
                  };
                  

                  然后,每当您想停止收听时,请调用它.您至少应该在活动的 onStop 方法期间进行此调用.

                  Then, whenever you want to stop listening call this. You should at least make this call during your activity's onStop method.

                  LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
                  locationManager.removeUpdates(this.locationListener);
                  

                  这篇关于如果禁用,Android 获取位置或提示启用位置服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何持续追踪安卓手机的位置? 下一篇:确定 iPhone 用户所在的国家

                  相关文章

                  最新文章

                    • <bdo id='qXxcg'></bdo><ul id='qXxcg'></ul>

                      <i id='qXxcg'><tr id='qXxcg'><dt id='qXxcg'><q id='qXxcg'><span id='qXxcg'><b id='qXxcg'><form id='qXxcg'><ins id='qXxcg'></ins><ul id='qXxcg'></ul><sub id='qXxcg'></sub></form><legend id='qXxcg'></legend><bdo id='qXxcg'><pre id='qXxcg'><center id='qXxcg'></center></pre></bdo></b><th id='qXxcg'></th></span></q></dt></tr></i><div id='qXxcg'><tfoot id='qXxcg'></tfoot><dl id='qXxcg'><fieldset id='qXxcg'></fieldset></dl></div>
                    1. <small id='qXxcg'></small><noframes id='qXxcg'>

                      <legend id='qXxcg'><style id='qXxcg'><dir id='qXxcg'><q id='qXxcg'></q></dir></style></legend>

                      <tfoot id='qXxcg'></tfoot>