Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获取空的当前位置_Android_Location - Fatal编程技术网

Android 获取空的当前位置

Android 获取空的当前位置,android,location,Android,Location,我第一次在android studio中使用location。问题是我得到的是空位置。我不明白为什么?我已经授予了所有权限,如访问课程、良好位置。在我的模拟器上,它给我虚拟的位置(在其他大陆),在我的真实设备上,它给我显示0.0,0.0坐标。我的位置类别代码如下: public class LocationClass extends Service implements LocationListener { private final Context mContext; boolean che

我第一次在android studio中使用location。问题是我得到的是空位置。我不明白为什么?我已经授予了所有权限,如访问课程、良好位置。在我的模拟器上,它给我虚拟的位置(在其他大陆),在我的真实设备上,它给我显示0.0,0.0坐标。我的位置类别代码如下:

public class LocationClass extends Service implements LocationListener {

private final Context mContext;
boolean checkGPS = false;


boolean checkNetwork = false;

boolean canGetLocation = false;

Location loc;
double latitude;
double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;


private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
protected LocationManager locationManager;

public LocationClass(Context mContext) {
    this.mContext = mContext;
    getLocation();
}

private Location getLocation() {

    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // get GPS status
        checkGPS = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // get network provider status
        checkNetwork = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!checkGPS && !checkNetwork) {
            Toast.makeText(mContext, "No Service Provider is available", Toast.LENGTH_SHORT).show();
        } else {
            this.canGetLocation = true;
            // if GPS Enabled get lat/long using GPS Services
            if (checkGPS) {
                if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, 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.
                }
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) {
                    loc = locationManager
                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (loc != null) {
                        latitude = loc.getLatitude();
                        longitude = loc.getLongitude();
                    }
                }

            }
            if (checkNetwork) {


                if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, 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.
                }
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                if (locationManager != null) {
                    loc = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                }

                if (loc != null) {
                    latitude = loc.getLatitude();
                    longitude = loc.getLongitude();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return loc;

}
public double getLongitude() {
    if (loc != null) {
        longitude = loc.getLongitude();
    }
    return longitude;
}

public double getLatitude() {
    if (loc != null) {
        latitude = loc.getLatitude();
    }
    return latitude;
}

public boolean canGetLocation() {
    return this.canGetLocation;
}

public void showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);


    alertDialog.setTitle("GPS is not Enabled!");

    alertDialog.setMessage("Do you want to turn on GPS?");


    alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
        }
    });


    alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });


    alertDialog.show();
}
public void stopListener() {
    if (locationManager != null) {

        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, 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.removeUpdates(LocationClass.this);
    }
}
和其他覆盖方法等。我通过广播发送我的位置,因此我的广播代码为:

            if (messageReceived.equals(location))
             {
            LocationClass locationClass;
            locationClass = new LocationClass(context);
            if (locationClass.canGetLocation()) {

                double longitude = locationClass.getLongitude();
                double latitude = locationClass.getLatitude();

                String message = "https://maps.google.com/maps?q="+Double.toString(latitude)+","+Double.toString(longitude);

                SmsManager smsManager = SmsManager. getDefault();
                smsManager.sendTextMessage(contact, null, message , null, null);

            } else {

                locationClass.showSettingsAlert();
            }

请阅读-总结是,这不是一个理想的方式来解决志愿者,可能会适得其反获得答案。请不要将此添加到您的问题中。O!我不知道…谢谢你告诉我和编辑我的问题。