requestLocationUpdates抛出异常

时间:2012-12-10 22:48:42

标签: android service gps locationmanager

我正在创建一个定期更新位置的服务。但是使用下面的代码,它引发了我:

runException无法在未调用Looper.prepare()

的线程内创建处理程序

此代码在服务中的Async类的doInBackground内运行

private void UpdatePosition()
    {
        locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);


        Location location = 
                locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        showPosition(location);
        locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                showPosition(location);
            }
            public void onProviderDisabled(String provider){

            }
            public void onProviderEnabled(String provider){

            }
            public void onStatusChanged(String provider, int status, Bundle extras){
                Log.i("LocAndroid", "Provider Status: " + status);

            }
        };


        locManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 15000, 0, locationListener);
    }

我正在审核几个帖子,发现请求必须在uithread中运行,但不知道如何或在何处放置它。

我能做什么?

编辑:

我只会将此添加到我的代码中,现在不会抛出任何错误

private void UpdatePosition()
    {
        Looper.prepare();
        looper = Looper.myLooper();
        locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);


        Location location = 
                locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        showPosition(location);
        locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                showPosition(location);
            }
            public void onProviderDisabled(String provider){

            }
            public void onProviderEnabled(String provider){

            }
            public void onStatusChanged(String provider, int status, Bundle extras){
                Log.i("LocAndroid", "Provider Status: " + status);

            }
        };


        locManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 15000, 0, locationListener);
    }

但我不知道它是否正确......你说什么?

0 个答案:

没有答案