【发布时间】:2018-03-02 05:29:24
【问题描述】:
我使用下面的代码来获取 Long Lat,它在 GPS 定位打开时工作正常。但问题是。当我关闭 GPS 位置然后再次打开时。它没有向我显示任何日志纬度。
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 定位?
【问题讨论】: