【问题标题】:Problems at getting location on Android在 Android 上获取位置的问题
【发布时间】:2017-09-30 18:03:25
【问题描述】:

我是 Java 编程和 Android 编程的新手,我正在尝试获取我的位置,但遇到了一些麻烦。

这是我写的所有代码

public class MiServicio extends Service implements LocationListener{private final Context context;
double latitud;
double longitud;
Location location;
boolean gpsActivo;
TextView texto;
LocationManager locationManager;


public MiServicio() {
    super();
    this.context = this.getApplicationContext();
}

public MiServicio(Context c) {
    super();
    this.context = c;
    getLocation();
}

public void setView(View v) {
    texto = (TextView) v;
    texto.setText("Coordenadas: " + latitud + ", " + longitud);
}

public void getLocation() {
    try {
        locationManager = (LocationManager) this.context.getSystemService(LOCATION_SERVICE);
        gpsActivo = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ignored) {
    }

    if (gpsActivo) {


        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60, 10, this);

        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        latitud = location.getLatitude();
        longitud = location.getLongitude();
    }
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onLocationChanged(Location location) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

}

我通过调试发现,在这部分代码到达if语句时会崩溃

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60, 10, this);

那么有人可以解释一下如何解决这个问题吗?

问候。

这也是我在清单中设置的权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

编辑

These are all the errors I got

我还用我的手机来测试这个应用程序

【问题讨论】:

  • 您遇到了什么错误?显示日志猫
  • 你的模拟器是什么?或设备位置是否开启?
  • 参考This Link。我希望这将有助于解决您的问题。
  • 使用LocationManager获取位置是老式的,你可以使用GoogleApiClient。这是示例代码:stackoverflow.com/a/33419346/3073945 要从 Android 6.0 获得位置权限,您必须获得运行时权限。这里有获取运行时权限的代码stackoverflow.com/a/38142728/3073945

标签: java android android-location locationmanager


【解决方案1】:

当您创建服务时,您不会调用它的构造函数,也不会覆盖它。不要在您的活动中或尝试启动此服务的任何地方使用MiServicio servicio = new MiServicio(Context c),而是使用以下命令:

context.startService(new Intent(context, MiServicio.class));

上下文必须由 Android 实现并在您的清单中声明。有关如何使用 Android 服务的完整教程,请查看 Android Services tutorial

【讨论】:

    【解决方案2】:

    尝试在 if() 语句中使用以下代码:

      if (context.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
    

    【讨论】:

      【解决方案3】:

      请用以下代码替换您的代码以检查和授予权限

           int permissionCheck = ContextCompat.checkSelfPermission(this,
                      Manifest.permission.ACCESS_FINE_LOCATION);
              if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                  checkPermission();
              }
      
      
      
      private void checkPermission() {
          // Here, thisActivity is the current activity
          if (ContextCompat.checkSelfPermission(this,
                  Manifest.permission.ACCESS_FINE_LOCATION)
                  != PackageManager.PERMISSION_GRANTED) {
      
      
              // Should we show an explanation?
              if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                      Manifest.permission.ACCESS_FINE_LOCATION)) {
                  // Show an explanation to the user asynchronously -- don't block
                  // this thread waiting for the user's response! After the user
                  // sees the explanation, try again to request the permission.
                  return;
              } else {
                  // No explanation needed, we can request the permission.
                  ActivityCompat.requestPermissions(this,
                          new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                          MY_PERMISSIONS_REQUEST_LOCATION);
                  // MY_PERMISSIONS_REQUEST_READ_PHONE_STATE is an
                  // app-defined int constant. The callback method gets the
                  // result of the request.
                  return;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 2019-12-24
        相关资源
        最近更新 更多