我正在创建一个使用GPS的应用程序。这是onCreate()我第一次检查GPS是否已启用。如果未启用,那么我会将用户引导至设置菜单以设置GPS为启用状态。
启用GPS后,我便开始执行我的任务。但是,如果用户之间停止了通知管理器上的GPS滚动,那我怎么知道呢?
因为在未启用GPS的情况下,我的方法仅执行一次。此后,如果再次禁用它,则无法执行。
I just want to know whenever in between the work the GPS is disabled by the user. Just this information is required.
这是我的代码。
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我在Activity类的按钮单击上调用此服务。
任何帮助,将不胜感激。
lmGps.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,gpsLocationListener);
lmGps.addGpsStatusListener(gpsStatusListener);
Run Code Online (Sandbox Code Playgroud)
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){}
};
Run Code Online (Sandbox Code Playgroud)
GPS状态监听器
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){}
}
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3393 次 |
| 最近记录: |