google GPS问题
请问大家,我在开发GPS取经纬度(只用GPS取)当在户外时移动位置是onLocationChanged()这个方法可以获取到经纬度数据,但当我进入室内时(不用WIFI或者基站定位)onLocationChanged()没有作用,怎样实现在监听过程中(在室内没有触发些方法onLocationChanged())去调用别的方法,一旦进入户外时恢复GPS取数据功能?
以下是代码
public class MyService extends Service {
public static final String TAG = "MyService";
CommandReceiver cmdReceiver;
boolean flag;
private double lat, lon;
private List lat_lon;
private Location location;
private Location lastLocation;
private boolean tag, tagLastLocation;
private LocationProvider locationProvider;
private LocationManager locationManager;
@Override
public void onCreate() {// 重写onCreate方法
super.onCreate();
Log.d(TAG, "**********************onCreate()********************");
//flag = true;
cmdReceiver = new CommandReceiver();
initLocation();
}
private void initLocation() {
Log.d(TAG, "**********************initLocation()********************");
// 获取位置管理服务
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(serviceName);
// 查找到服务信息
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);// 高精度
criteria.setAltitudeRequired(false);//不要求海拔
criteria.setBearingRequired(false);//不要求方位
criteria.setCostAllowed(true);//允许有花费
criteria.setPowerRequirement(Criteria.POWER_LOW);// 低功耗
//从可用的位置提供器中,匹配以上标准的最佳提供器
String provider = locationManager.getBestProvider(criteria, true);// 获取GPS信息
//if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER));
// 用GPS获取数据
//String provider = locationManager.GPS_PROVIDER;
// 用WIFI或者移动基站获取数据
// String provider = locationManager.NETWORK_PROVIDER;
//获得最后一次变化的位置
location = locationManager.getLastKnownLocation(provider);
// 保存上次经纬度信息中间变量
lastLocation = location;
updateWithNewLocation(location);
Log.d(TAG, "**********************initLocation()-----------requestLocationUpdates--before********************");
// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米
locationManager.requestLocationUpdates(provider, 1000, 0,locationListener);
// locationManager.requestLocationUpdates(provider, 0, 0,locationListener);
Log.d(TAG, "**********************initLocation()----requestLocationUpdates-after********************");
}
//位置监听器
private final LocationListener locationListener = new LocationListener() {
// 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
public void onLocationChanged(Location location) {
Log.d(TAG, "**********************onLocationChanged()********************");
// updateWithNewLocation(location);
if (location != null) {
lastLocation = location;
if (lastLocation != null) {
// tag=true;
updateWithNewLocation(location);
}
} else {
updateWithNewLocation(lastLocation);
}
}
// Provider被disable时触发此函数,比如GPS被关闭
public void onProviderDisabled(String provider) {
Log.d(TAG, "**********************onProviderDisabled()********************");
updateWithNewLocation(null);
}
// Provider被enable时触发此函数,比如GPS被打开
public void onProviderEnabled(String provider) {
Log.d(TAG, "**********************onProviderEnabled()********************");
}
// Provider的转换才可用、暂时不可用和无服务三个状态直接切换时触发此函数
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, "**********************onStatusChanged()********************");
if(provider==null){
updateWithNewLocation(lastLocation);
}
switch(status){
case LocationProvider.AVAILABLE:{
updateWithNewLocation(lastLocation);
break;
}
case LocationProvider.OUT_OF_SERVICE:{
updateWithNewLocation(lastLocation);
break;
}
case LocationProvider.TEMPORARILY_UNAVAILABLE:{
updateWithNewLocation(lastLocation);
break;
}
}
}
};
private void updateWithNewLocation(Location location) {
Log.d(TAG, "**********************updateWithNewLocation()********************");
if (location != null) {
lat = location.getLatitude();
lon = location.getLongitude();
// 创建Intent对象
Intent intent = new Intent();
intent.setAction("wyf.wpf.Sample_3_6");
intent.putExtra("data", lon);
intent.putExtra("data2", lat);
// if(tag){
// intent.putExtra("data3", 1);
// }
sendBroadcast(intent);// 发送广播
writeAddData();
}
}
private void writeAddData() {
Log.d(TAG, "**********************writeAddData()********************");
lat_lon = new ArrayList();
String format = "yyyy-MM-dd hh:mm:ss";
Date date = new Date();
String str = new SimpleDateFormat(format).format(date);
lat_lon.add("\\n");
lat_lon.add("now Date =");
lat_lon.add(str);
lat_lon.add("\\n");
lat_lon.add("Latitude =");
lat_lon.add(lat);
lat_lon.add("Longitude =");
lat_lon.add(lon);
lat_lon.add("\\n");
try {
// 判断SDCard是否存在,并且可以读写
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
saveToSDCard("TestGPSProjectLatitudeAndLongitude.txt",lat_lon.toString());
// Toast.makeText(getApplicationContext(),"Write file success",Toast.LENGTH_SHORT).show();
} else {
// Toast.makeText(getApplicationContext(),"sdcarderror",Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// Toast.makeText(getApplicationContext(),"Write file fail",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
public void saveToSDCard(String filename, String content) throws Exception {
Log.d(TAG, "**********************saveToSDCard()********************");
File file = new File(Environment.getExternalStorageDirectory(),filename);
FileOutputStream outStream = new FileOutputStream(file, true);
outStream.write(content.getBytes());
outStream.close();
}
[解决办法]
估计要自己用时间去检测了,比如用timer来检测上次获得location到现在是否超过一个设定值,超过了用其他的方式(GPRS&WIFI),相反,如果从室内出来,能检测到location了就又切回GPS。时间的概念。
GPS和NETWORK两种方式都要维护。