这块给大家介绍第二种方式:Network
他跟Gps很多地方比较类似,使用的系统管理是相同的
很多人看到这个代码可能会觉得一样,但是需要注意的是他的Provider值跟Gps是不同的
备注:Network中涉及到一个类Geocoder,大家看到我注释掉了这个类,它主要用来通过经纬度来获取具体位置的名称,但是很多时候它返回为空,获取不到,算是自己的一个Bug,所以现在很少使用,所以如果想知道具体名称,可以通过Google提供的API 或者其他来进行获取具体位置信息
注:权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
package com.camerademo.service;
import android.app.Service;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class NetWorkService extends Service{
private LocationManager locationManager; //Location管理对象
private Location curLocation; //当前最后一次..
private boolean stop = false;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.e("NetService", "Create");
startNet();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("NetService", "Destroy");
}
/**
* NetWork访问经纬度
*/
private void startNet(){
locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
final String cur = locationManager.getBestProvider(getCriteria(), true);
//network
String provider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER).getName();
// curLocation = locationManager.getLastKnownLocation(provider);
curLocation = locationManager.getLastKnownLocation(cur);
if(curLocation == null){
locationManager.requestLocationUpdates(cur, 0, 0, locationListener);
}
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while(!stop){
curLocation = locationManager.getLastKnownLocation(cur);
//curLocation = locationManager.getLastKnownLocation(cur);
//设置查询条件过滤Provider
if(curLocation != null){
Log.e("Net-location", "经度:"+curLocation.getLongitude());
Log.e("Net-location", "纬度:"+curLocation.getLatitude());
}else{
Log.e("Net-location", "经度:未知");
Log.e("Net-location", "纬度:未知");
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
//Geocoder 经常获取不到数据
// Geocoder geoCoder = new Geocoder(this,Locale.getDefault());
// try {
// int latitude = (int)curLocation.getLatitude();
// int longitude = (int)curLocation.getLongitude();
// List<Address> addrs = geoCoder.getFromLocation(latitude, longitude, 2);
// for (Address addr : addrs) {
// Toast.makeText(this, addr.getCountryName() + addr.getAdminArea() + addr.getFeatureName(), 3000).show();
// }
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
//location变化的监听(经纬度)----同时适用于 1.Gps2.netWork
private LocationListener locationListener = new LocationListener() {
//设备GPS状态
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
switch(status){
case LocationProvider.AVAILABLE:
Log.e("Local-Gps-status", "Gps可用(可见)");
break;
//不再服务区
case LocationProvider.OUT_OF_SERVICE:
Log.e("Local-Gps-status", "Gps不在服务区");
break;
//GPS暂停服务
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.e("Local-Gps-status", "Gps暂停服务");
break;
}
}
@Override
public void onProviderEnabled(String provider) {
Location curLocation = locationManager.getLastKnownLocation(provider);
Log.e("location-provider-enabled", "经度:"+curLocation.getLongitude());
Log.e("location-provider-enabled", "纬度:"+curLocation.getLatitude());
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Location curLocation = locationManager.getLastKnownLocation(provider);
Log.e("location-provider-disabled", "经度:"+curLocation.getLongitude());
Log.e("location-provider-disabled", "纬度:"+curLocation.getLatitude());
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.e("location-changed", "经度:"+location.getLongitude());
Log.e("location-changed", "纬度:"+location.getLatitude());
}
};
/**
* 返回查询条件
* 获取Provider时候可通过Criteria设置条件过滤
* @return
*/
private Criteria getCriteria() {
Criteria criteria = new Criteria();
// 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
criteria.setAccuracy(Criteria.ACCURACY_FINE);
// 设置是否要求速度
criteria.setSpeedRequired(false);
// 设置是否允许运营商收费
criteria.setCostAllowed(false);
// 设置是否需要方位信息
criteria.setBearingRequired(false);
// 设置是否需要海拔信息
criteria.setAltitudeRequired(false);
// 设置对电源的需求
criteria.setPowerRequirement(Criteria.POWER_LOW);
return criteria;
}
}