当前位置: 动力学知识库 > 问答 > 编程问答 >

android - detect gps disabled

问题描述:

I have this code to get my GPS position:

 public void run() {

mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

Looper.prepare();

mLocationListener = new MyLocationListener();

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);

Looper.loop();

Looper.myLooper().quit();

} else {

//Never be here!!!

}

}

private Handler handler = new Handler() {

@Override

public void handleMessage(Message msg) {

pd.dismiss();

mLocationManager.removeUpdates(mLocationListener);

if (currentLocation!=null) {

//found. OK

}

}

};

private class MyLocationListener implements LocationListener {

@Override

public void onLocationChanged(Location loc) {

if (loc != null) {

setCurrentLocation(loc);

handler.sendEmptyMessage(0);

}

}

@Override

public void onProviderDisabled(String provider) {

//If I disable de GPS I have an error

}

@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

}

}

how can I detect the GPS disabled and too time looking for data?

I thought that onProviderDisabled detect that event, but, my code never go there. I got this exception:

Cant create handler inside thread that has not called Looper.prepare

Thank you in advance

分享给朋友:
您可能感兴趣的文章:
随机阅读: