解决2.3机型无法定位问题
来源:互联网 发布:淘宝p图神器软件 编辑:程序博客网 时间:2024/04/30 11:29
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);
}
};
}
- 解决2.3机型无法定位问题
- 解决google map无法网络定位问题
- 解决google地图无法使用网络定位的问题
- iOS开发 - 解决百度地图模拟器无法定位问题
- iOS开发中解决iOS8无法定位的问题
- 解决apt-get install E: 无法定位软件包问题
- 解决apt-get install E: 无法定位软件包问题
- 剖析Picasso加载压缩本地图片流程(解决Android 5.0部分机型无法加载本地图片的问题)
- 解决无法更新定位行
- 解决百度地图无法定位
- so文件导致的不同机型无法安装问题
- 部分机型PopupWindow先点开后无法取消的问题
- 解决华为手机部分机型不能打印Log问题
- 解决RadioButton与RadioGroup在某些机型偏移问题
- 解决android部分机型拍照后被旋转的问题
- iOS 8 无法定位问题
- 解决Android下Google Maps无法通过GSM基站定位问题
- 解决绝对定位层(position:absolute;)上无法触发滚动事件的问题
- 读书记录
- gvim配置
- Android Tabs 中使用listview,mapview
- PushbackInputStream
- shell 的参数
- 解决2.3机型无法定位问题
- 软件开发的几种模式
- Sqlite3 数据库介绍及常用方法的简单封装
- 《垃圾收集》笔记——第一章
- spring的事务管理有几种方式实现
- 集时通讯成功签约800座席呼叫中心,吹响进入大规模市场冲锋号
- C++异常详解
- 终于完成RTP/RTSP代理服务器
- C#中生成验证码的类