package com.ARtest;
public class LocationHelper implements LocationListener {
private Context mContext = null;
/**定位的最小时间 */
private final long REQUEST_LOCATION_UPDATES_MINTTIME = 3000;
/**定位的最小距离 */
private final long REQUEST_LOCATION_UPDATES_MINDISTANCE = 100;
private LocationChangeListener mLocationChanger = null;
/** 定位数据 */
private LocationManager mLocationManger = null;
/** 定时器*/
private Timer timer = null;
private HttpClient httpclient = null;
private Location locations = null;
private Location curlocations = null;
Thread theloc =null;
public LocationHelper(Context context) {
mContext = context;
// 获取系统的服务
mLocationManger = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
locations = new Location("LocationApi");
curlocations = new Location("LocationApi");
}
public void onResume() {
if (mLocationManger != null) {
List<String> listProvider = mLocationManger.getProviders(true);
Location location;
for (int i = 0; i < listProvider.size(); i++) {
location = mLocationManger.getLastKnownLocation(listProvider
.get(i).toString());
if (location == null) {
// mLatitude=34.217222,mLongitude=108.8873558,
/** 开始定时器 */
if (timer == null) {
timer = new Timer();
timer.schedule(timerTask, 10,20000);
}
} else {
Object obj = listProvider.get(i);
if (obj != null) {
mLocationManger.requestLocationUpdates(obj.toString(),
REQUEST_LOCATION_UPDATES_MINTTIME,
REQUEST_LOCATION_UPDATES_MINDISTANCE, this);
break;
}
}
}
}
}
public void onPause() {
mLocationChanger = null;
if (mLocationManger != null) {
mLocationManger.removeUpdates(this);
mLocationManger = null;
}
}
public void release() {
if (timer != null) {
// 销毁的同时退出定时器
timer.cancel();
}
System.gc();
}
protected void onDestroy() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
System.gc();
}
public void setLocationChangeListener(LocationChangeListener listener) {
mLocationChanger = listener;
}
public interface LocationChangeListener {
void onLocationChange(Location location);
}
@Override
public void onLocationChanged(Location location) {
updateLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
//updateLocation(null);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
// 获取用户位置的函数,利用Log显示
private void updateLocation(Location location) {
if (location != null && mLocationChanger != null) {
mLocationChanger.onLocationChange(location);
}
}
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
switch (msg.what)
{
case 1:
// 开启新的线程去获取经纬度
theloc=new Thread(new Runnable() {
@Override
public void run()
{
TelephonyManager mTelNet = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
String operator = mTelNet.getNetworkOperator();
String mcc = operator.substring(0, 3);
String mnc = operator.substring(3);
GsmCellLocation GClocation = (GsmCellLocation) mTelNet.getCellLocation();
int cid = GClocation.getCid();
int lac = GClocation.getLac();
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
httpclient = new DefaultHttpClient(httpParameters);
HttpPost post = new HttpPost("http://www.google.com/loc/json");
try {
JSONObject holder = new JSONObject();
holder.put("version", "1.1.0");
holder.put("host", "maps.google.com");
holder.put("address_language", "zh_CN");
holder.put("request_address", true);
JSONObject tower = new JSONObject();
tower.put("mobile_country_code", mcc);
tower.put("mobile_network_code", mnc);
tower.put("cell_id", cid);
tower.put("location_area_code", lac);
JSONArray towerarray = new JSONArray();
towerarray.put(tower);
holder.put("cell_towers", towerarray);
StringEntity query = new StringEntity(holder.toString());
post.setEntity(query);
HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();
BufferedReader buffReader = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer strBuff = new StringBuffer();
String result = null;
while ((result = buffReader.readLine()) != null)
{
strBuff.append(result);
}
JSONObject json = new JSONObject(strBuff.toString());
JSONObject subjosn = new JSONObject(json.getString("location"));
locations.setLatitude(Double.valueOf(subjosn.getString("latitude")));
locations.setLongitude(Double.valueOf(subjosn.getString("longitude")));
if (locations.getLatitude() != 0 && locations.getLongitude() != 0)
{
if (curlocations.getLatitude() == locations.getLatitude()
&& curlocations.getLongitude() == locations.getLongitude())
{
} else
{
curlocations = locations;
updateLocation(curlocations);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
post.abort();
httpclient = null;
}
}
});
theloc.start();
break;
case 2:
if (theloc != null && !theloc.isInterrupted()) {
theloc.interrupt();
}
break;
}
super.handleMessage(msg);
}
};
/**定时器*/
private TimerTask timerTask = new TimerTask()
{
public void run()
{
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
}
public class LocationHelper implements LocationListener {
private Context mContext = null;
/**定位的最小时间 */
private final long REQUEST_LOCATION_UPDATES_MINTTIME = 3000;
/**定位的最小距离 */
private final long REQUEST_LOCATION_UPDATES_MINDISTANCE = 100;
private LocationChangeListener mLocationChanger = null;
/** 定位数据 */
private LocationManager mLocationManger = null;
/** 定时器*/
private Timer timer = null;
private HttpClient httpclient = null;
private Location locations = null;
private Location curlocations = null;
Thread theloc =null;
public LocationHelper(Context context) {
mContext = context;
// 获取系统的服务
mLocationManger = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
locations = new Location("LocationApi");
curlocations = new Location("LocationApi");
}
public void onResume() {
if (mLocationManger != null) {
List<String> listProvider = mLocationManger.getProviders(true);
Location location;
for (int i = 0; i < listProvider.size(); i++) {
location = mLocationManger.getLastKnownLocation(listProvider
.get(i).toString());
if (location == null) {
// mLatitude=34.217222,mLongitude=108.8873558,
/** 开始定时器 */
if (timer == null) {
timer = new Timer();
timer.schedule(timerTask, 10,20000);
}
} else {
Object obj = listProvider.get(i);
if (obj != null) {
mLocationManger.requestLocationUpdates(obj.toString(),
REQUEST_LOCATION_UPDATES_MINTTIME,
REQUEST_LOCATION_UPDATES_MINDISTANCE, this);
break;
}
}
}
}
}
public void onPause() {
mLocationChanger = null;
if (mLocationManger != null) {
mLocationManger.removeUpdates(this);
mLocationManger = null;
}
}
public void release() {
if (timer != null) {
// 销毁的同时退出定时器
timer.cancel();
}
System.gc();
}
protected void onDestroy() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
System.gc();
}
public void setLocationChangeListener(LocationChangeListener listener) {
mLocationChanger = listener;
}
public interface LocationChangeListener {
void onLocationChange(Location location);
}
@Override
public void onLocationChanged(Location location) {
updateLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
//updateLocation(null);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
// 获取用户位置的函数,利用Log显示
private void updateLocation(Location location) {
if (location != null && mLocationChanger != null) {
mLocationChanger.onLocationChange(location);
}
}
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
switch (msg.what)
{
case 1:
// 开启新的线程去获取经纬度
theloc=new Thread(new Runnable() {
@Override
public void run()
{
TelephonyManager mTelNet = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
String operator = mTelNet.getNetworkOperator();
String mcc = operator.substring(0, 3);
String mnc = operator.substring(3);
GsmCellLocation GClocation = (GsmCellLocation) mTelNet.getCellLocation();
int cid = GClocation.getCid();
int lac = GClocation.getLac();
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
httpclient = new DefaultHttpClient(httpParameters);
HttpPost post = new HttpPost("http://www.google.com/loc/json");
try {
JSONObject holder = new JSONObject();
holder.put("version", "1.1.0");
holder.put("host", "maps.google.com");
holder.put("address_language", "zh_CN");
holder.put("request_address", true);
JSONObject tower = new JSONObject();
tower.put("mobile_country_code", mcc);
tower.put("mobile_network_code", mnc);
tower.put("cell_id", cid);
tower.put("location_area_code", lac);
JSONArray towerarray = new JSONArray();
towerarray.put(tower);
holder.put("cell_towers", towerarray);
StringEntity query = new StringEntity(holder.toString());
post.setEntity(query);
HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();
BufferedReader buffReader = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer strBuff = new StringBuffer();
String result = null;
while ((result = buffReader.readLine()) != null)
{
strBuff.append(result);
}
JSONObject json = new JSONObject(strBuff.toString());
JSONObject subjosn = new JSONObject(json.getString("location"));
locations.setLatitude(Double.valueOf(subjosn.getString("latitude")));
locations.setLongitude(Double.valueOf(subjosn.getString("longitude")));
if (locations.getLatitude() != 0 && locations.getLongitude() != 0)
{
if (curlocations.getLatitude() == locations.getLatitude()
&& curlocations.getLongitude() == locations.getLongitude())
{
} else
{
curlocations = locations;
updateLocation(curlocations);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
post.abort();
httpclient = null;
}
}
});
theloc.start();
break;
case 2:
if (theloc != null && !theloc.isInterrupted()) {
theloc.interrupt();
}
break;
}
super.handleMessage(msg);
}
};
/**定时器*/
private TimerTask timerTask = new TimerTask()
{
public void run()
{
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
}