0

我有个问题。当我测试这个。我要求它总是返回 null 的 showCurrentLocation 函数。当我之后发送位置时,它在模拟器中工作。但是我需要这个才能在手机上工作,而且你不能像在 de DDNS 窗口中那样发送位置。

这是我的代码

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 = 1; // in Milliseconds

protected LocationManager locationManager;

protected Button retrieveLocationButton;
protected Button stopLocationButton;


@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();
        }
    });  
    /*stopLocationButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //locationManager.removeUpdates(MyLocationListener)  ;          
        }
    }); */ 

}    

public String getMyPhoneNumber(){
    TelephonyManager mTelephonyMgr;
    mTelephonyMgr = (TelephonyManager)
    getSystemService(Context.TELEPHONY_SERVICE); 
    return mTelephonyMgr.getLine1Number();
}

protected void showCurrentLocation() {

    Criteria crit = new Criteria();
    crit.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(crit, true);
    Location loc = locationManager.getLastKnownLocation(provider);


    if (loc != null) {
        String longi = "" + loc.getLongitude();
        String lat = "" + loc.getLatitude();
        String num = getMyPhoneNumber();
        String message = String.format(
                "Current Location \n  Longitude: %1$s \n Latitude: %2$s \n %3$s ",
                longi,
                lat,
                num         );
        Toast.makeText(LbsGeocodingActivity.this, message,
                Toast.LENGTH_LONG).show();
    }
    if (loc == null)Toast.makeText(LbsGeocodingActivity.this, "Null ",
            Toast.LENGTH_LONG).show();
}   

private class MyLocationListener implements LocationListener {



    public void onLocationChanged(Location loc) {
        String longi = "" + loc.getLongitude();
        String lat = "" + loc.getLatitude();
        String num = getMyPhoneNumber();
        String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s \n %3$s ",
                longi,
                lat,
                num                 );
        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();
    }

}

}

我的权限是:FINE_LOCATION COURSE_LOCATION。

我真的只是想要它跟踪用户的位置,即使在后台..

4

2 回答 2

0

您也可以将模拟位置发送到您的 Android 设备查看设备 上的 Android 模拟位置?

然后除此之外我也遇到了这个问题,它似乎在您的设备上尝试打开谷歌地图并确保您找到然后再试一次之前可能从未有过位置。此外,我怀疑第一次使用该应用程序时,您还无法访问最后知道的位置,因为您从未使用过它,因此您首先需要定位,下次启动该应用程序时它会起作用。如果您想快速定位,请尝试通过 wifi 或手机信号塔定位

并确保设置了权限!

于 2012-07-11T08:49:17.653 回答
0

您是否检查过手机中的 GPS 是否已打开,打开 GPS 后需要一些时间才能获取位置更新。

于 2012-07-11T08:57:53.760 回答