0

我知道让主 UI 线程等待确实是一个非常糟糕的主意,因为它会导致“应用程序无响应”消息。

但这是我的场景。

我正在从我的 android 应用程序调用我的服务器,在该应用程序中我发送用户的坐标,服务器使用该坐标相应地返回结果。问题是在应用程序的第一次运行期间,获取位置需要很长时间。我正在寻找 GPS 和 NETWORK 提供商来检索位置,并且在从 GPS 和 NETWORK 提供商获取位置之前运行 20 秒计时器。

这是我查找用户位置的课程。

public class MyLocation {
    Timer timer1;
    LocationManager lm;
    LocationResult locationResult;
    boolean gps_enabled=false;
    boolean network_enabled=false;

    public boolean getLocation(Context context, LocationResult result)
    {
        //use LocationResult callback class to pass location value from MyLocation to user code.
        locationResult=result;
        if(lm==null)
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //exceptions will be thrown if provider is not permitted.
        try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
        try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

        //don't start listeners if no provider is enabled
        if(!gps_enabled && !network_enabled)
            return false;

        if(gps_enabled)
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        if(network_enabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        timer1=new Timer();
        timer1.schedule(new GetLastLocation(), 20000); 
        return true;
    }

    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    class GetLastLocation extends TimerTask {
        @Override
        public void run() {
            lm.removeUpdates(locationListenerGps);
            lm.removeUpdates(locationListenerNetwork);

            Location net_loc=null, gps_loc=null;
            if(gps_enabled)
                gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(network_enabled)
                net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            //if there are both values use the latest one
            if(gps_loc!=null && net_loc!=null){
                if(gps_loc.getTime()>net_loc.getTime())
                    locationResult.gotLocation(gps_loc);
                else
                    locationResult.gotLocation(net_loc);
                return;
            }

            if(gps_loc!=null){
                locationResult.gotLocation(gps_loc);
                return;
            }
            if(net_loc!=null){
                locationResult.gotLocation(net_loc);
                return;
            }
            locationResult.gotLocation(null);
        }
    }

    public void cancelTimer() { 
        timer1.cancel();
        lm.removeUpdates(locationListenerGps);
        lm.removeUpdates(locationListenerNetwork); 
    }

    public static abstract class LocationResult{
        public abstract void gotLocation(Location location);
    }
}

在我的主要活动中,

onCreate(...)
{
.
.
.
.
myLocation.getLocation(context, locationResult); // my call to the MyLocation class to get the user location

sendRequestToServer(); // calls an AsyncTask which uses the coordinates returned by the MyLocation class

}

getLocation(context, locationResult)将上下文传递给 MyLocation 类,结果存储在LocationResult callback class.

所以,在我的主要活动中,我有

LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){
            if(location != null)
            {
                mUser.setLastKnownLoc(location);
                Log.d("UserLocation", location.getLatitude() + "   "  + location.getLongitude());

            }
        }
    };

我对服务器的请求用于mUser.getLastKnownLoc()获取存储在结果中的位置值。

我的问题:让 sendRequestToServer() 等到我有位置

我尝试了什么:

  • myLocation.getLocation(..)放入doInBackground()AsyncTask 并调用onPostExecute sendRequestToServer()(),但 sendRequestToServer 仍然不会等待 MyLocation 类中的计时器完成。
  • 在 onCreate 中创建另一个计时器并设置mTimer.schedule(RequestToserver,20000)以便仅在 20 秒后调用 sendRequestToServer(到 MyLocation 中的另一个计时器结束时)。这似乎有点工作,因为请求是在 20 秒后才发出的,但是由于一些线程处理问题,我的应用程序会崩溃。我尝试在 UIThread 上运行 timerTask 并将 Looper.prepare() 附加到它。但是我陷入了线程处理错误的恶性循环,这可能是因为sendRequestToServer()调用了一个 AsyncTask 再次拥有自己的后台线程。

很长一段时间以来,我一直在努力完成这项工作。我只需要确保在 20 秒 MyLocation 计时器用完后调用 AsyncTask。

您身边的任何帮助/建议都会非常棒!!

4

2 回答 2

1

这是愚蠢的解决方案有时会起作用。无论如何尝试使用它

boolean x=......获取位置();

以便它等待结果 x。如果需要,将其放入 oncreate() 中。如果问题存在回发

于 2013-03-31T12:11:13.350 回答
1

在您的 onCreate 中只显示一个ProgressDialog阻止用户输入的内容,在后台运行您的 getLocation ,当它获得一个位置时,只需关闭ProgressDialog并开始您的新AsyncTask功能以发送数据。

像。

ProgressDialog pd = ...;
pd.show();
MyLocation ml = new MyLocation(new LocationResultsCallback(){ 
    public void onLocationRecieved(Location loc){
        //must run on UI thread
        pd.dismiss();
        new SendResultsTask(loc).execute();
    }
});
ml.findLocation(); // start location finding

同样对于您的 getLastKnownLocation ,将其切换为类似的东西可能会更好。

String bestProvider = locationManager.getBestProvider(new Criteria(), true);
if(bestProvider!=null)
    Location local = locationManager.getLastKnownLocation(bestProvider);

也就是说,如果这适用,我不知道您对位置提供商有什么限制。

此外,使用 Handler 代替 Timer 更合适,但两者都应该在你身上工作。

于 2013-03-31T13:38:10.430 回答