我正在创建一个使用 GPS 的应用程序。我第一次在 onCreate()
中检查 GPS 是否启用。如果它未启用,那么我会将用户发送到设置菜单以设置启用 GPS。
启用 GPS 后,我便开始执行我的工作。 但如果用户在通知管理器上停止 GPS 滚动,我怎么知道呢?
因为我的方法只在 GPS 未启用时执行了一次。之后,当它再次被禁用时它不会被执行。
我只想知道在工作间隙用户何时禁用 GPS。仅此信息是必需的。
这是我的代码。
class LocationFinderService extends Service{
boolean gps_enabled;
LocationManager locationManager;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if(!isGpsEnabled())
{
startGpsForLocation();
stopSelf();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public boolean isGpsEnabled()
{
gps_enabled = (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)&& locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
Log.i("Log", "Gps: "+gps_enabled);
return gps_enabled;
}
public void startGpsForLocation()
{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ;
startActivity(intent);
Toast.makeText(this, "Please start the GPS", Toast.LENGTH_LONG).show();
}
}
我在点击 Activity 类的按钮时调用此服务。
如有任何帮助,我们将不胜感激。
最佳答案
lmGps.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,gpsLocationListener);
lmGps.addGpsStatusListener(gpsStatusListener);
gpsLocationListner 是
LocationListener gpsLocationListener=new LocationListener(){
public void onLocationChanged(Location location){
dGpsLat=location.getLatitude();
dGpsLng=location.getLongitude();
dLat=dGpsLat;
dLng=dGpsLng;
try{
SaveLocation();
}
catch (Exception e) {
sResponse=e.toString();
return;
}
}
public void onStatusChanged(String provider,int status,Bundle extras){}
public void onProviderEnabled(String provider){}
public void onProviderDisabled(String provider){}
};
gpsStatusListener
GpsStatus.Listener gpsStatusListener=new GpsStatus.Listener(){
@Override
public void onGpsStatusChanged(int event){
if(event==GpsStatus.GPS_EVENT_SATELLITE_STATUS){
try{
GpsStatus status=lmGps.getGpsStatus(null);
sats=status.getSatellites();
Iterator satI=sats.iterator();
int count=0;
while(satI.hasNext()){
GpsSatellite gpssatellite=(GpsSatellite) satI.next();
if(gpssatellite.usedInFix()){
count++;
}
}
iSalelliteCount=count;
}
catch(Exception ex){}
}
}
};
关于android - 当用户在android中禁用gps时如何得到通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13524164/