问题描述
我正在创建一个使用 GPS 的应用程序.我第一次在 onCreate()
检查 GPS 是否启用.如果它没有启用,那么我会将用户发送到设置菜单以启用 GPS.
I am creating an app which uses GPS. For the first time in onCreate()
I am checking whether the GPS is enabled or not. And if its not enabled then I am sending the user to the setting menu to set the GPS enabled.
一旦启用 GPS,我就会开始执行我的工作.但如果用户在通知管理器上停止 GPS 滚动,那么我怎么知道呢?
Once the GPS is enabled then I starts executing my stuff. But if in between the user stops the GPS scrolling on Notification manager then how would I know this?
因为我的方法只在 GPS 未启用时执行一次.之后,当它再次被禁用时它不会被执行.
Because my method executed only once when the GPS is not enabled. After that its not getting executed when it is disabled again.
我只是想知道在工作之间何时 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 是
gpsLocationListner is
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
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中禁用gps时如何获得通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!