我需要在用户登录之前计算用户当前的经度和纬度。我已经在我的移动设备中测试了我的代码,但它似乎不起作用。这是我的代码:
LocationManager mlocManager=null;
LocationListener mlocListener=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
}
@Override protected void onResume() {
super.onResume();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, mlocListener);
}
@Override protected void onPause() {
super.onPause();
mlocManager.removeUpdates(mlocListener); //<8>
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
Toast.makeText(getApplicationContext(),"In onchange", Toast.LENGTH_SHORT).show();
if(loc!=null){
latitude=loc.getLatitude();
longitude=loc.getLongitude();
if(loc.getLatitude()!=0.0 || loc.getLongitude()!=0.0){
Toast.makeText(getApplicationContext(),"Location not null", Toast.LENGTH_SHORT).show();
SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);
SharedPreferences.Editor e = prefsSaveLatLong.edit();
e.remove("LAT");
e.remove("LONG");
e.putString("LAT",Double.toString(loc.getLatitude()));
e.putString("LONG",Double.toString(loc.getLongitude()));
e.commit();
String Text = "My current location is: " + "Latitude = " + loc.getLatitude() + "Longitude = " + loc.getLongitude();
Toast.makeText(getApplicationContext(),Text+" "+latitude+" "+longitude, Toast.LENGTH_SHORT).show();
}else{
SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);
if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){
SharedPreferences.Editor e1 = prefsSaveLatLong.edit();
e1.remove("LAT");
e1.remove("LONG");
e1.commit();
}
}
// set latitude longitude to label
setLatLongLabel();
}else{
latLongLabel.setTextColor(Color.parseColor("#FF0000"));
latLongLabel.setText("Latitude-Longitude not available");
SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);
if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){
SharedPreferences.Editor e1 = prefsSaveLatLong.edit();
e1.remove("LAT");
e1.remove("LONG");
e1.commit();
}
}
}
@Override
public void onProviderDisabled(String provider)
{
gpsEnabled=false;
if(!gpsEnabled){
Toast.makeText( getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT ).show();
showSettingsAlert();
}
}
@Override
public void onProviderEnabled(String provider)
{
gpsEnabled=true;
Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
if(status==0){
Toast.makeText(getApplicationContext(),"OUT_OF_SERVICE",Toast.LENGTH_SHORT).show();
SharedPreferences prefsSaveLatLong = context.getSharedPreferences("prefsSaveLatLong",Context.MODE_PRIVATE);
if(prefsSaveLatLong.contains("LAT") && prefsSaveLatLong.contains("LONG")){
SharedPreferences.Editor e1 = prefsSaveLatLong.edit();
e1.remove("LAT");
e1.remove("LONG");
e1.commit();
}
}
else if(status==1){
Toast.makeText(getApplicationContext(),"TEMPORARILY_UNAVAILABLE",Toast.LENGTH_SHORT).show();
}else if(status==2){
Toast.makeText(getApplicationContext(),"AVAILABLE",Toast.LENGTH_SHORT).show();
}
}
}/* End of Class MyLocationListener */enter code here
`