GPS位置已启用但无法找到坐标

时间:2018-03-02 05:29:25

标签: android gps

我使用下面的代码来获取Long Lat,当GPS位置打开时,它的工作正常。但问题是。当我关闭GPS位置,然后再打开它。它没有给我看任何Log Lat。

public class GetGPSlocation implements LocationListener {
Context context;
public GetGPSlocation(Context c) {
    context = c;
}
public Location getLocation() {
    if (ActivityCompat.checkSelfPermission(this.context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(context, "Permision Not Granted", Toast.LENGTH_SHORT).show();
        return null;
    }
    LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    boolean isGPSenabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (isGPSenabled) {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10, this);
        Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        return l;
    } else {
        Toast.makeText(context, "Please Enable GPS", Toast.LENGTH_LONG).show();
    }
    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) {

}

我在CLick Listner按钮上使用此代码...

     btnSaveAttd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                GetGPSlocation g = new GetGPSlocation(v.getContext());
                Location l = g.getLocation();
                if (l!=null){
                    LAT = l.getLatitude();
                    LON = l.getLongitude();
                }
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
                alertDialog.setTitle("Confirmation");
                alertDialog.setMessage("Are you sure you want to Call this doctor?\n\n" +
                                        "DocID: " +DocID.getText().toString()+ "\n"+
                                        "EmpName: " + empName.getText().toString() + "\n"+
                                        "DateTime: " + date +"\n" +
                                        "Time: "+time+"\n"+
                                        "Status : " + spinner.getSelectedItem()+"\n"+
                                        "Location: "+LON +" , " +LAT);
                alertDialog.setIcon(R.drawable.ic_menu_camera);
                alertDialog.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                //endregion
                alertDialog.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                alertDialog.show();

           }
        });

任何人都可以告诉我,如果禁用GPS,有没有办法自动启用GPS定位?

1 个答案:

答案 0 :(得分:0)

您可以使用广播接收器收听提供商(即GPS)的更改:

registerReceiver(mReceiverCallback, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));

private BroadcastReceiver mReceiverCallback = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
            // Do whatever you want here
        }
    }
};