我试图通过按下按钮将我的 GPS 位置放入编辑框
当我单击按钮时,GPS 图标会亮起并开始闪烁,但直到稳定后才会用位置填充编辑框,所以我必须按下按钮,等到 GPS 图标稳定后再按下按钮再次将位置放入编辑框中
有没有一种方法可以通过按下一个按钮来完成,这样它就会等到有 GPS 信号,然后才用位置填充编辑框
我的代码如下
public void pickbtn(View view) {
LocationManager mlocManager=null;
LocationListener mlocListener;
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new locapp();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if(locapp.latitude>0)
{
Calendar c = Calendar.getInstance();
int dd = c.get(Calendar.SHORT);
Time t = new Time(Time.getCurrentTimezone());
t.setToNow();
String date = t.format("%d/%m/%y");
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
EditText editText1 = (EditText)findViewById(R.id.LatText);
editText1.setText(mydate + ","+locapp.latitude + "," +locapp.longitude, TextView.BufferType.EDITABLE);
}
else
{
}
} else {
}
}
locapp.java代码如下
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.Toast;
public class locapp implements LocationListener {
public static double latitude;
public static double longitude;
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
latitude=loc.getLatitude();
longitude=loc.getLongitude();
}
@Override
public void onProviderDisabled(String provider)
{
//print "Currently GPS is Disabled";
}
@Override
public void onProviderEnabled(String provider)
{
//print "GPS got Enabled";
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
感谢任何帮助
标记
最佳答案
基于 Will Jamieson 的回答。我正在添加一个类以从 GPS 获取位置。在您的项目中添加此 GPSTracker 类:http://pastie.org/9300205
将其全局化:
GPSTracker gps;
在 onCreate 的 Activity 中使用此代码:
gps = new GPSTracker(this);
在你的类中定义这个异步:
private class getGPSstuff extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
Location l=gps.getLocation();
latitude=l.getLatitude();
longitude=l.getLongitude();
}
@Override
protected void onPostExecute(String result) {
// Set the text here
}
@Override
protected void onPreExecute() {
// you can make sure gps is available here and handle it if its not available here
// optional tho
}
}
在按钮点击时使用它:
new getGpsStuff().execute();
关于android获取GPS位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24272683/