gps无法获得用户位置

时间:2011-06-02 04:48:35

标签: android

我正在尝试获取当前用户位置(纬度和经度),但onLocationChanged方法未被调用 但其他方法如public void onProviderDisabled(String provider)。请帮忙,可能是什么问题? 我有一节课:

package com.javacodegeeks.android.lbs;   

import android.app.Activity;   
import android.os.Bundle;   

 import android.app.Activity;     

 import android.content.Context;     

 import android.location.Location;     

 import android.location.LocationListener;     

 import android.location.LocationManager;     

 import android.os.Bundle;     

 import android.view.View;     

 import android.view.View.OnClickListener;     

 import android.widget.Button;     

 import android.widget.Toast;     



 public class LbsGeocodingActivity extends Activity {     



     private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters     

     private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds     



     protected LocationManager locationManager;     



     protected Button retrieveLocationButton;     



     @Override    

     public void onCreate(Bundle savedInstanceState) {     




     super.onCreate(savedInstanceState);     

     setContentView(R.layout.main);     



     retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);     



     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);     



     locationManager.requestLocationUpdates(     

             LocationManager.GPS_PROVIDER,      

             MINIMUM_TIME_BETWEEN_UPDATES,      

             MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,     

             new MyLocationListener()     

     );     



 retrieveLocationButton.setOnClickListener(new OnClickListener() {     

         @Override    

         public void onClick(View v) {     

             showCurrentLocation();     

         }     

 });             



 }         



 protected void showCurrentLocation() {     



     Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);     



     if (location != null) {     

         String message = String.format(     

                 "Current Location \n Longitude: %1$s \n Latitude: %2$s",     

                 location.getLongitude(), location.getLatitude()     

         );     

         Toast.makeText(LbsGeocodingActivity.this, message,     

                 Toast.LENGTH_LONG).show();     

     }     



 }        



 private class MyLocationListener implements LocationListener {     



     public void onLocationChanged(Location location) {     

         String message = String.format(     

                 "New Location \n Longitude: %1$s \n Latitude: %2$s",     

                 location.getLongitude(), location.getLatitude()     

         );     

         Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();     

     }     



     public void onStatusChanged(String s, int i, Bundle b) {     

         Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",     

                 Toast.LENGTH_LONG).show();     

     }     



     public void onProviderDisabled(String s) {     
         Toast.makeText(LbsGeocodingActivity.this,     

                 "Provider disabled by the user. GPS turned off",     

                 Toast.LENGTH_LONG).show();     

     }     



     public void onProviderEnabled(String s) {     

         Toast.makeText(LbsGeocodingActivity.this,     

                 "Provider enabled by the user. GPS turned on",     

                 Toast.LENGTH_LONG).show();     

     }   
 }   
 }  

3 个答案:

答案 0 :(得分:0)

public class HomeActivity extends Activity implements LocationListener{
public static Context mContext;
private double latitude, longitude;
 public LocationManager mLocManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    mContext=this;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.homelayout);


    mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            this);
    mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
            0, this);
    locationUpdate();
    ((Button) this.findViewById(R.id.ButtonHome))
            .setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {

                        startActivity(new Intent(HomeActivity.this,
                                DefaultDisplay.class));

                }
            });

    ((Button) this.findViewById(R.id.ButtonProfile))
            .setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    if (GUIStatics.boolLoginStatus) {
                        startActivity(new Intent(HomeActivity.this,
                                MyProfile.class));
                    } else {
                        Intent intent=new Intent(HomeActivity.this,
                                Login.class);
                        intent.putExtra("moveTo","MyProfile");
                        startActivity(intent);
                    }
                }
            });

    ((Button) this.findViewById(R.id.ButtonNotifications))
            .setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    if (GUIStatics.boolLoginStatus) {
                        startActivity(new Intent(HomeActivity.this,
                                ShowAllNotificationActiviry.class));
                    } else {
                        Intent intent=new Intent(HomeActivity.this,
                                Login.class);
                        intent.putExtra("moveTo","ShowAllNotificationActiviry");
                        startActivity(intent);
                    }
                }
            });

    ((Button) this.findViewById(R.id.ButtonFavorites))
            .setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    if (GUIStatics.boolLoginStatus) {
                        startActivity(new Intent(HomeActivity.this,
                                FavoritesActivity.class));
                    } else {
                        Intent intent=new Intent(HomeActivity.this,
                                Login.class);
                        intent.putExtra("moveTo","FavoritesActivity");
                        startActivity(intent);
                    }
                }
            });

            ((Button) this.findViewById(R.id.ButtonMore))
            .setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                        startActivity(new Intent(HomeActivity.this,
                                MoreListActivity.class));
                }
            });

}

public void locationUpdate()
{
    CellLocation.requestLocationUpdate();
}


public void getAddress(double lat, double lng) {
    Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);
        String add = obj.getAddressLine(0);
        GUIStatics.currentAddress = obj.getSubAdminArea() + ","
                + obj.getAdminArea();
        GUIStatics.latitude = obj.getLatitude();
        GUIStatics.longitude = obj.getLongitude();
        GUIStatics.currentCity= obj.getSubAdminArea();
        GUIStatics.currentState= obj.getAdminArea();
        add = add + "\n" + obj.getCountryName();
        add = add + "\n" + obj.getCountryCode();
        add = add + "\n" + obj.getAdminArea();
        add = add + "\n" + obj.getPostalCode();
        add = add + "\n" + obj.getSubAdminArea();
        add = add + "\n" + obj.getLocality();
        add = add + "\n" + obj.getSubThoroughfare();

        Log.v("IGA", "Address" + add);
        // Toast.makeText(this, "Address=>" + add,
        // Toast.LENGTH_SHORT).show();

        // TennisAppActivity.showDialog(add);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}



public void onLocationChanged(Location location) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    GUIStatics.latitude=location.getLatitude();
    GUIStatics.longitude= location.getLongitude();
    Log.v("Test", "IGA" + "Lat" + latitude + "   Lng" + longitude);
    //mLocManager.r

    getAddress(latitude, longitude);
    if(location!=null)
    {

    mLocManager.removeUpdates(this);
    }
    // Toast.makeText(this, "Lat" + latitude + "   Lng" + longitude,
    // Toast.LENGTH_SHORT).show();
}


public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(HomeActivity.this, "Gps Disabled", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(
            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(intent);
}


public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}


public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
     if(arg1 == 
            LocationProvider.TEMPORARILY_UNAVAILABLE) { 
                                    Toast.makeText(HomeActivity.this, 
            "LocationProvider.TEMPORARILY_UNAVAILABLE", 
            Toast.LENGTH_SHORT).show(); 
                        } 
                        else if(arg1== LocationProvider.OUT_OF_SERVICE) { 
                                    Toast.makeText(HomeActivity.this, 
            "LocationProvider.OUT_OF_SERVICE", Toast.LENGTH_SHORT).show(); 
                        } 

}

} 这是我用来获取用户当前位置的代码。 请仔细阅读,检查您在代码中所犯的错误。 并检查与gps相关的mainfest文件中的所有权限。

<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

我希望这是有帮助的。

答案 1 :(得分:0)

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; //在Meters中

将此更改为0 ....

答案 2 :(得分:0)

只需在您的活动中扩展GpsStatus.Listener即可。 如下: ublic类MyActivity实现LocationListener,         GpsStatus.Listener

并检查您的许可: