GPS位置已启用但无法找到坐标
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 Listener上使用此代码...
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?
" +
"DocID: " +DocID.getText().toString()+ "
"+
"EmpName: " + empName.getText().toString() + "
"+
"DateTime: " + date +"
" +
"Time: "+time+"
"+
"Status : " + spinner.getSelectedItem()+"
"+
"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定位?
答案
您可以使用广播接收器收听提供商(即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
}
}
};
以上是关于GPS位置已启用但无法找到坐标的主要内容,如果未能解决你的问题,请参考以下文章
Android中QPython的位置不返回GPS坐标 - 仅“网络”