Android 在LocationManager获得纬度和经度后立即显示

Android 在LocationManager获得纬度和经度后立即显示,android,geolocation,locationmanager,Android,Geolocation,Locationmanager,我正在开发一个紧急短信,每当你访问活动时,GPS将被启用,用户的位置将显示在文本视图中,然后每当用户按下send键,它将被发送到目标设备。现在,如果我使用网络提供商,Textview将在我访问我的活动时尽快获得纬度和经度,消息将显示在Textview中。但是,GPS将显示空文本视图和空位置 这是我的密码: public class SMSmyLocation extends Activity implements OnClickListener, LocationListener{ Locat

我正在开发一个紧急短信,每当你访问活动时,GPS将被启用,用户的位置将显示在文本视图中,然后每当用户按下send键,它将被发送到目标设备。现在,如果我使用网络提供商,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

}    
}
公共类SMSmyLocation扩展活动实现OnClickListener、LocationListener{
地点经理地点经理;
LocationListener locationListner;
上下文ctx=SMSmyLocation.this;
文本视图tv1、tv2;
按钮b1;
编辑文本;
地理编码器;
列出地址;
字符串地址=”;
布尔可见性=false;
双长=0,Lad=0;
共享参考sp;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u smy\u位置);
tv1=(TextView)findViewById(R.id.ESMS);
tv2=(TextView)findViewById(R.id.currentLocation);
b1=(按钮)findViewById(R.id.SendSMS);
turnGPSOn();
geo=新的地理编码器(这个,Locale.getDefault());
locationManager=(locationManager)this.getSystemService(Context.LOCATION\u服务);
locationManager.RequestLocationUpdate(locationManager.GPS\提供程序,0,0,此);
如果(地址!=null)
{
地址=地址。get(0)。getAddressLine(0)+“”+地址。get(0)。getAddressLine(1)+“”+地址。get(0)。getAddressLine(2);
tv1.setText(地址);
}
其他的
tv1.setText(“失败”);
b1.setOnClickListener(本);
}
已更改位置上的公共无效(位置){
//TODO自动生成的方法存根
Log.e(“LM”,“非空”);
double Lad=location.getLatitude();
double Long=location.getLongitude();
试一试{
地址=geo.getFromLocation(Lad,Long,1);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
Log.e(“Long”,String.valueOf(Long));
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
//TODO自动生成的方法存根
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
//TODO自动生成的方法存根
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
//TODO自动生成的方法存根
}    
}

如何使系统等待LocationManager获得位置坐标,并在可用时以文本形式显示它们。线程是个好主意吗?如何实现它?请在回答时提供教程或代码。感谢您的时间和考虑。

类似的方法应该可以:

@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
函数中设置要更新的文本视图。因此,当GPS位置出现时(是的,可能需要很长时间),它将触发此功能并更新您的文本视图

,因为GPS检索坐标需要时间,因此需要一些时间。。NETWorksValpor在快速定位访问和电池消耗方面非常有效,有时当我关闭无线网络时,NETWorksIdvor没有给我位置,所以我也需要考虑GPSO只有在NETWorkJoDor关闭时才使用GPS,但这需要时间。问题是,当我使用GPS时,获取Lad和Long需要时间,到那时,textview已经设置为“失败”,因为GPS没有快速获取坐标。因此,我需要一种方法使系统等待GPS获得坐标,然后将其添加到文本中,将lat和long值初始化为0.0,一旦收到坐标(意味着如果lat和long值发生变化),如果我在一个位置停留很长时间,请在文本视图中打印它们,当GPS获得坐标时,onLoctionChanged功能仍然会触发?!谢谢你的回复