在Android上获取位置的问题

时间:2017-05-03 04:22:53

标签: java android android-location locationmanager

我是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

并且我还使用手机测试此应用

3 个答案:

答案 0 :(得分:0)

尝试在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;
    }

答案 1 :(得分:0)

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

     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;
        }
    }
}

答案 2 :(得分:0)

创建服务时,不要在其上调用构造函数,也不要覆盖它。您可以使用以下命令,而不是在活动中或在尝试启动此服务的任何位置使用command1 && command2 && command3; command4 & command5 & command6 &

command 5

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