1

当我按下按钮开始获取位置时,立即出现 toast 消息并显示 lat:0 lon:0 (初始化值)。

几秒钟后,我必须再次按下按钮以获取位置,然后它会显示位置。

我能为此做些什么?

(据我了解,如果我使用延迟并不能解决问题。我希望 toast 消息等到我得到位置)

 if(gps.canGetLocation()){

 latitude = gps.getLatitude();
 longitude = gps.getLongitude();

    Toast.makeText(getApplicationContext(), "Your Location is  \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                    ...

我用过类似的东西:

 private void showToast() {
               new Thread() {
                   public void run() {
                    try {   
                       while (message) {
                           runOnUiThread(new Runnable() {
                                @Override
                                public void run() {

                           if(gps.canGetLocation()){

                                latitude = gps.getLatitude();
                    longitude = gps.getLongitude();

        Toast.makeText(getApplicationContext(), "Your Location is \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                                 message=false;
                                  }else{

                                    gps.showSettingsAlert();
                                }
                           });
                           Thread.sleep(1000);
                       } 
                   }   catch (InterruptedException e) {
                        e.printStackTrace();
                   }
                   }
               }.start();
               }        

但这似乎不起作用。另外,有没有办法在找到位置之前显示另一条消息?

4

2 回答 2

0

正如codeMagic所说,使用不同的thead来继续检查。GPS需要时间来修复。

也许您可以查看Service在您的应用程序运行时管理 GPS 定位的方法。

于 2013-05-04T13:49:14.093 回答
0

对于这样的事情,最好使用AsyncTask,恕我直言。Actviity如果您想在另一个或应用程序中使用它,这将使其更加通用和便携。这是一个如何工作的简单示例

public class GetGPSData extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected void onProgressUpdate(String... values) {
    super.onProgressUpdate(values);

}

@Override
protected String doInBackground(String... params) {
//do your work here
  if(gps.canGetLocation())
        {
             latitude = gps.getLatitude();
             longitude = gps.getLongitude();
        }
while (latitude == 0 || longitude ==0)
{

    Thread.sleep(300);  // let the background thread sleep 
}
 return something;
}
@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
       // do something with data here-display it or send to mainactivity
Toast.makeText(context, "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();

}

其中一些可能需要根据您放置它的位置以及您对它的处理方式进行修改,但这样的事情会起作用。您还可以显示一段progress dialog时间来获取数据,以便用户知道它正在加载

编辑

这只是一个粗略的例子。您可以根据需要更改参数。doInBackground()可以不params。只需将其更改为

Void doInBackground(Void... params) {

并将您的班级声明更改为

extends AsyncTask<Void,Void,Void>

而且您不会直接调用该方法。你做类似的事情

GetGPSData gps = new GetGPSData();   could send params to a constructor if you needed
gps.execute();  // could put params here if you needed for doInBackground()

一定要仔细阅读AsyncTask Docs,那里有你需要了解的信息,但是一旦你得到它,生活就会容易得多。

于 2013-05-04T14:27:02.947 回答