android获取GPS定位

android获取GPS定位,android,gps,android-edittext,Android,Gps,Android Edittext,我试着按下一个按钮就把我的GPS位置输入编辑框 当我点击按钮时,GPS图标会亮起并开始闪烁,但直到稳定后,它才会将位置填充到编辑框中,因此我必须按下按钮,等待GPS图标稳定后,再次按下按钮,它会将位置放入编辑框中 有没有一种方法可以只按一个按钮,让它等待GPS信号,然后在编辑框中填入位置 我的代码如下 public void pickbtn(View view) { LocationManager mlocManager=null; LocationList

我试着按下一个按钮就把我的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 {
          }
      }
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)
{
}
}
java代码如下所示

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 {
          }
      }
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)
{
}
}
谢谢你的帮助


标记我猜您可以将GPS代码放入
async
块中,并在
onPostExecute()
方法中更新
EditText
。有关
AsyncTask

的详细信息,此代码适用于您:

private class getGPSstuff extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        //Run your GPS code here
    }

    @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
    }
  }
私有类getGPSstuff扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
//在这里运行您的GPS代码
}
@凌驾
受保护的void onPostExecute(字符串结果){
//在这里设置文本
}
@凌驾
受保护的void onPreExecute(){
//您可以确保gps在此处可用,如果在此处不可用,请进行处理
//可选tho
}
}

基于威尔·杰米森的回答。我正在添加一个类以从GPS获取位置。在项目中添加此GPSTracker类:

放眼全球:

GPSTracker gps;
在onCreate中的活动中使用此代码:

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
}
}

抱歉,我有点不明白,那么我应该把getGPSstuff类放在locapp.java代码或活动的主代码中的什么地方呢?我建议检查一下这个tut:然后在doInBackground中调用getLatitude()和getLatitude()