1

我正在开发一个紧急短信,每当您访问活动时,都会启用 GPS,并且用户的位置将显示在文本视图中,然后每当用户按下发送时,它将被发送到目标设备。现在,如果我使用 Network_Provider,Textview 将在我访问我的活动时尽快获得纬度和经度,并且消息将显示在 textview 中。但是,GPS 将显示一个空的文本视图和空的位置。

这是我的代码:

public class SMSmyLocation extends Activity implements OnClickListener, LocationListener{

LocationManager locationManager;
LocationListener locationListner;

Context ctx = SMSmyLocation.this;

TextView tv1, tv2;
Button b1;
EditText ed;

Geocoder geo;
List<Address> addresses;

String address = "";

boolean visibility = false;

double Long = 0,Lad = 0;

SharedPreferences sp;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_smsmy_location);


    tv1 = (TextView) findViewById(R.id.ESMS);
    tv2 = (TextView) findViewById(R.id.currentLocation);


    b1 = (Button) findViewById(R.id.SendSMS);



    turnGPSOn();

    geo = new Geocoder(this, Locale.getDefault());

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);


    if (addresses != null)
    {
        address = addresses.get(0).getAddressLine(0) + " " + addresses.get(0).getAddressLine(1) + " " + addresses.get(0).getAddressLine(2);
        tv1.setText(address);
    }
    else
        tv1.setText("fail");



    b1.setOnClickListener(this);
}

public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    Log.e("LM", "Not null");

    double Lad = location.getLatitude();
    double Long = location.getLongitude();

    try {
        addresses = geo.getFromLocation(Lad, Long, 1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }
Log.e("Long", String.valueOf(Long));

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}    
}

我该怎么做才能让系统等到 LocationManger 获得位置坐标,一旦可用,以文本形式显示它们。线程是个好主意吗?如何实现?请提供教程或代码以及您的答案。感谢您的时间和考虑。

4

1 回答 1

0

像这样的东西应该工作:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_smsmy_location);

    tv1 = (TextView) findViewById(R.id.ESMS);
    tv2 = (TextView) findViewById(R.id.currentLocation);

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

}

public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

    double Lad = location.getLatitude();
    double Long = location.getLongitude();

    tv2.setText(Lad+", "+Long);
}

这主要是在onLocationChanged您设置要更新的 textView 的函数中。所以当 GPS 位置进来时(是的,它可能需要很长时间),它会触发这个功能并更新你的 textView

于 2013-10-23T02:37:54.327 回答