首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GPS LocationManager.GPS_PROVIDER引起ANR

GPS LocationManager.GPS_PROVIDER引起ANR
EN

Stack Overflow用户
提问于 2013-12-04 10:14:30
回答 1查看 324关注 0票数 0

A GPS Listner有问题。当我使用LocationManager.GPS_PROVIDER应用程序时,工作时间很短,然后开始没有响应(ANR)。当我只使用LocationManager.NETWORK时,一切都很好。我需要AndroidManifes.xml (我认为是这样)的权限和线路。这是我的代码:

BackgroundGPSService.java

代码语言:javascript
复制
public class BackgroundGPSService extends Service implements GPSLocationListner {
    private GPSService service;

    public class LocalBinder extends Binder {
        public BackgroundGPSService getService() {
            return BackgroundGPSService.this;
        }
    }

    private final IBinder localBinder = new LocalBinder();

    @Override
    public IBinder onBind(Intent arg0) {
        return localBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        service = new GPSService(this);
        service.construct();
        service.setGPSLocationListner(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        service.destory();
    }

    @Override
    public void onUpdateSetteliteCount(int count) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFirstFix() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLocationUpdate(double lat, double lon) {
        // TODO Auto-generated method stub

    }
}

这是我的GPSService,实现了GpsStatus.Listener。

GPSService.java

代码语言:javascript
复制
public class GPSService implements GpsStatus.Listener {
    private Context context;

    private FineLocationListner fineListner;

    private GPSLocationListner listner;

    private GPSInfoStruct gpsInfo = new GPSInfoStruct();

    public GPSService(Context context) {
        this.context = context;
    }

    public void construct() {
        LocationManager locationManager = getLocationManager();

        fineListner = new FineLocationListner();
        coarseListner = new CoarseLocationListner();

        List<String> providerList = (ArrayList<String>)locationManager.getAllProviders();
        for(int i=0; i<providerList.size(); i++) {
            String providerName = providerList.get(i);
            if(DEBUG) Log.d(TAG, "PROVIDERS LIST name: " + providerName + " enable: " + locationManager.isProviderEnabled(providerName));
            if(providerName.equals(LocationManager.GPS_PROVIDER))
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, fineListner);
            Location loc = locationManager.getLastKnownLocation(providerList.get(i));
            gpsInfo.updateLocation(loc);
        }

        locationManager.addGpsStatusListener(this);
    }

    public Location getLocation() {
        return gpsInfo.getLocation();
    }

    public double getLat() {
        return gpsInfo.getLatitude();
    }

    public double getLon() {
        return gpsInfo.getLongitude();
    }

    @Override
    public void onGpsStatusChanged(int event) {
        if(event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
            GpsStatus status = getLocationManager().getGpsStatus(null);
            int counter = 0;
            Iterable<GpsSatellite> satellites = status.getSatellites();
            Iterator<GpsSatellite> satIter = satellites.iterator();
            while(satIter.hasNext()) {
                counter++;
            }

            //if(DEBUG) Log.d(TAG, "THIS onGpsStatusChanged: " + event + " cnt: " + counter);

            gpsInfo.setSetteliteCount(counter);
            if(listner != null)
                listner.onUpdateSetteliteCount(counter);

        } else if(event == GpsStatus.GPS_EVENT_FIRST_FIX) {
            if(DEBUG) Log.d(TAG, "THIS onFirstFix");
            gpsInfo.setFix();
            if(listner != null)
                listner.onFirstFix();
        } else
            if(DEBUG) Log.d(TAG, "THIS onGpsStatusChanged: " + event);
    }

    public void setGPSLocationListner(GPSLocationListner listner) {
        this.listner = listner;
        if(gpsInfo.isFix()) listner.onFirstFix();
        listner.onUpdateSetteliteCount(gpsInfo.getSetteliteCount());
        listner.onLocationUpdate(gpsInfo.getLatitude(), gpsInfo.getLongitude());
    }

    public void destory() {
        getLocationManager().removeUpdates(fineListner);
        getLocationManager().removeUpdates(coarseListner);
        getLocationManager().removeGpsStatusListener(this);
    }

    private LocationManager getLocationManager() {
        return (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
    }

    class FineLocationListner implements LocationListener {
            ///implementation
}

在LoginActuivity中,我启动服务

代码语言:javascript
复制
public class LoginActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);

        startService(new Intent(this, BackgroundGPSService.class));
    }
}

我漏掉了什么?

编辑1

当我评论这一行时:

代码语言:javascript
复制
locationManager.addGpsStatusListener(this);

应用程序很好。我认为这是个问题,但为什么呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-04 10:28:52

Google发布新位置API.Its相当容易,并且编写了很少行代码,您就可以得到这个位置。

我已经发了答案,请访问这个链接。

How to detect Location Provider ? GPS or Network Provider

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20372513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档