必须使用Android的活动等待,直到GPS位置获得位置、Android、GPS

由网友(残留的自尊)分享简介:对不起,我的英语水平。我试图从GPS获得一个位置放在全局变量纬度,经度。全球定位系统导通,但活动的推移之前数据从GPS检索我的,换句话说需要...方法getCurrentLocation()只能完成,如果位置已经发现,经度和纬度的变量填补,所以我可以用它们在其他的方法。我知道......用户必须等待...我会解决...

对不起,我的英语水平。 我试图从GPS获得一个位置放在全局变量纬度,经度。 全球定位系统导通,但活动的推移之前数据从GPS检索

我的,换句话说需要...方法getCurrentLocation()只能完成,如果位置已经发现,经度和纬度的变量填补,所以我可以用它们在其他的方法。 我知道......用户必须等待...我会解决这个问题向前显示的东西在屏幕上。 我该怎么办? 谢谢

我觉得我跳过停在某个地方听GPS。哪里比较好?

code如下:

  //方法来检索坐标
公共无效getCurrentLocation(){
    //使用GPS如果可能的话
    如果(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        //指定监听器GPS
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,侦听);
        Toast.makeText(这一点,LocationManager.GPS_PROVIDER,Toast.LENGTH_SHORT).show();
    }
    否则,如果(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){// toherwise,使用网络
        //指定监听网络
        manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,侦听);
        Toast.makeText(这一点,LocationManager.NETWORK_PROVIDER,Toast.LENGTH_SHORT).show();
    }
}

在两个变量//类存储位置recived
最后一类MyLocationListener实现LocationListener的{

    @覆盖
    公共无效onLocationChanged(位置定位){
        //坐标存储
        纬度=将String.valueOf(location.getLatitude());
        经度=将String.valueOf(location.getLongitude());
        Toast.makeText(getApplicationContext(),纬度+经度,Toast.LENGTH_LONG).show();
    }

    @覆盖
    公共无效onProviderDisabled(字符串提供商){
        // TODO自动生成方法存根
    }

    @覆盖
    公共无效onProviderEnabled(字符串提供商){
        // TODO自动生成方法存根
    }

    @覆盖
    公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
        // TODO自动生成方法存根
    }
}
 

解决方案

如果你想机器人活动要等到GPS定位获得的,你可以尝试使用的AsyncTask 为。请参阅Android寻找GPS定位一次,显示加载对话框的答案。基本上在在preExecute 你可以(它会启动 doInBackground 调用之前)开始对话。这意味着你等到的时候,你可以定位并显示该对话框。然后,在 doInBackground ,你可以得到的位置。这完成后 onPostExecute 被调用。您可以停止从里面 onPostExecute 。您可以检查是否该位置不为空,然后调用由内 onPostExecute 一些其他的功能,另外,如果你想要的。

Android Fingerprint Enroll流程

此可能是一种方法。您可以了解从的AsyncTask的Andr​​oid例如的一个基本的例子。您也可以通过这里阅读文档启动。

其他一些类似的有用的问题:

等待当前的位置 - 全球定位系统 - Android开发

立即凑安卓位置

希望这有助于。

Sorry for my english. I'm trying to get a single location from GPS to put on global variables latitude, longitude. GPS turns on, but the activity goes on before data is retrieved from GPS.

My needs in other words... method getCurrentLocation() must finish only if a location has been found and the longitude and latitude variables are filled, so I could use them in other method. I know... user has to wait... I will solve this forward showing something on screen. What should I do? Thank you

I think I'm skipping stop listening GPS at some place. Where is better?

Code follows:

//Method to retrieve coordinates
public void getCurrentLocation() {
    //Use GPS if possible
    if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        //assign Listener to GPS
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
        Toast.makeText(this, LocationManager.GPS_PROVIDER, Toast.LENGTH_SHORT).show();
    }
    else if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){//toherwise, use NETWORK
        //assign Listener to NETWORK
        manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
        Toast.makeText(this, LocationManager.NETWORK_PROVIDER, Toast.LENGTH_SHORT).show();
    }
}

//Class to store the location recived in two variables
final class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        //coordinates storing
        latitude = String.valueOf(location.getLatitude());
        longitude = String.valueOf(location.getLongitude());
        Toast.makeText(getApplicationContext(), latitude + longitude, Toast.LENGTH_LONG).show();
    }

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

解决方案

If you want android activity to wait until GPS location obtained, you might try to use AsyncTask for that. See Android find GPS location once, show loading dialog for the answers. Basically in onPreExecute you can start dialog( it starts before the doInBackground is called). It means you are waiting till the time you can location and showing the dialog. Then in doInBackground you can get the location. After that finishes onPostExecute is called. You can stop is from inside onPostExecute. You can check if the location is not null and then call some other function from inside onPostExecute also if you want.

This might be one way. You can learn a basic example from AsyncTask Android example . You can also start by reading the documentation here.

Some other similar helpful questions:

Wait for current location - GPS - Android Dev

getting location instantly in android

Hope this helps.

阅读全文

相关推荐

最新文章