请问大家,我在开发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(); }
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(); }
解决方案 »
- 自己下载Android的源码编译的ADT,SDK,自动生成的代码编译失败,求教。
- 在ExpandableListView中子item中有两个按钮,要如何监听呢?
- 关于android usb摄像头的连接
- 为什么源码内的中文全是乱码?
- 为什么android手机就是没有iPhone运行得那样流畅呢
- 高手指点,关于SQLite的问题。谢谢!
- [武汉]下周就要比赛了,产品的服务器与数据库也开发的差不多了,可是就是找不到一个做客户端开发的。。。
- 卸载应用的同时删除sdcard上一个文件夹
- imageView显示图片的高度问题
- 安卓手势解锁怎么回到原来的页面
- 求教,蓝牙刷卡器(Pos机?)如何连接Android设备
- 1.6的模拟器上安装apk报:Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES]
GPS和NETWORK两种方式都要维护。