package com.genius.listen;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.telephony.CellLocation;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.widget.Toast;
public class MyLocationManager {
private final static int CHECK_POSITION_INTERVAL = 60 * 1000;
private LocationManager locationManager;
private Context mContext;
private Handler mHandler;
private myListen myListenGPS;
private myListen myListenNetwork;
public MyLocationManager(Context context, Handler handler)
{
mContext = context;
mHandler = handler;
locationManager=(LocationManager)context.getSystemService(context.LOCATION_SERVICE);
mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
}
/*
* 获取GPS开关状态״̬
*/
public boolean getGPSState()
{
ContentResolver resolver = mContext.getContentResolver();
boolean open = Settings.Secure.isLocationProviderEnabled(resolver, LocationManager.GPS_PROVIDER);
return open;
}
/*
* 切换GPS开关状态̬
*/
public void toggleGPS() {
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(mContext, 0, gpsIntent, 0).send();
} catch (CanceledException e) {
e.printStackTrace();
}
}
public Location getLocationByGps()
{
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return location;
}
public Location getLocationByNetwork()
{
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
return location;
}
public void registerListen()
{
if (myListenGPS == null)
{
myListenGPS = new myListen();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
CHECK_POSITION_INTERVAL, 0, myListenGPS);
}
if (myListenNetwork == null)
{
myListenNetwork = new myListen();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
CHECK_POSITION_INTERVAL, 0, myListenNetwork);
}
}
public void unRegisterListen()
{
if(myListenGPS != null)
{
locationManager.removeUpdates(myListenGPS);
myListenGPS = null;
}
if(myListenNetwork != null)
{
locationManager.removeUpdates(myListenNetwork);
myListenNetwork = null;
}
}
class myListen implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
MyUtil.display("onLocationChanged.......");
if (mHandler != null)
{
Message msg = mHandler.obtainMessage(LocationListenDemoActivity.REFRESH_LOCATION, location);
msg.sendToTarget();
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
/** 谷歌查询地址URL*/ // %s: latitude, %s: longitude
private static final String GOOGLE_QUERY_ADDRESS_URL = "http://maps.google.cn/maps/geo?key=abcdefg&q=%s,%s";
private static final int TIME_OUT_INTERVAL = 3 * 1000;
private TelephonyManager mTelephonyManager;
private GsmCellLocation mGsmCellLocation;
private int cid;
private int lac;
private String mcc = "";
private String mnc = "";
/*
* 获取基站信息
*/
public CellIDInfo getCellInfo()
{
mGsmCellLocation = ((GsmCellLocation) mTelephonyManager.getCellLocation());
if (mGsmCellLocation == null)
return null;
cid = mGsmCellLocation.getCid();
lac = mGsmCellLocation.getLac();
String netWorkOperator = mTelephonyManager.getNetworkOperator();
mcc = netWorkOperator.substring(0, 3);
mnc = netWorkOperator.substring(3, 5);
CellIDInfo cellIDInfo = new CellIDInfo();
cellIDInfo.cellId = cid;
cellIDInfo.mobileCountryCode = mcc;
cellIDInfo.mobileNetworkCode = mnc;
cellIDInfo.locationAreaCode = lac;
return cellIDInfo;
}
/**
* 通过谷歌获取地理位置
*
* @throws Exception
*/
public String queryAddressByGoogle(Double lat, Double lon){
String resultString = "";
/** 这里采用get方法,直接将参数加到URL上 */
String urlString = String.format(GOOGLE_QUERY_ADDRESS_URL, lat,lon);
/** 新建HttpClient */
HttpClient client = new DefaultHttpClient();
/** 设置超时 */
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIME_OUT_INTERVAL);//请求超时
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, TIME_OUT_INTERVAL);//读取超时
/** 采用GET方法 */
HttpGet get = new HttpGet(urlString);
try {
/** 发起GET请求并获得返回数据 */
HttpResponse response = client.execute(get);
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);
}
resultString = strBuff.toString();
/** 解析JSON数据,获得物理地址 */
if (resultString != null && resultString.length() > 0) {
JSONObject jsonobject = new JSONObject(resultString);
JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark").toString());
resultString = "";
for (int i = 0; i < jsonArray.length(); i++) {
resultString = jsonArray.getJSONObject(i).getString("address");
}
}
} catch (Exception e) {
return null;
} finally {
get.abort();
client = null;
}
return resultString;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android位置定位
共34个文件
class:12个
png:6个
java:5个
4星 · 超过85%的资源 需积分: 18 166 下载量 141 浏览量
2012-04-24
19:17:29
上传
评论 2
收藏 75KB RAR 举报
温馨提示
andoid位置定位DEMO,手把手教你如何定位,本文为定位系列第一篇,未完待续
资源推荐
资源详情
资源评论
收起资源包目录
LocationListenDemo.rar (34个子文件)
LocationListenDemo
AndroidManifest.xml 2KB
res
layout
main.xml 3KB
drawable-ldpi
ic_launcher.png 2KB
drawable-mdpi
ic_launcher.png 3KB
drawable-hdpi
ic_launcher.png 4KB
values
strings.xml 202B
proguard.cfg 1KB
src
com
genius
listen
MyLocationManager.java 7KB
MyUtil.java 1KB
LocationListenDemoActivity.java 7KB
CellIDInfo.java 409B
bin
classes
com
genius
listen
MyLocationManager$myListen.class 2KB
CellIDInfo.class 420B
R.class 524B
MyLocationManager.class 7KB
R$layout.class 391B
R$attr.class 337B
MyUtil.class 2KB
R$id.class 755B
R$string.class 424B
LocationListenDemoActivity.class 6KB
R$drawable.class 404B
LocationListenDemoActivity$1.class 2KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-mdpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
classes.dex 15KB
resources.ap_ 11KB
LocationListenDemo.apk 20KB
.classpath 364B
assets
project.properties 360B
.settings
org.eclipse.core.resources.prefs 88B
.project 854B
gen
com
genius
listen
R.java 1KB
共 34 条
- 1
蓝斯
- 粉丝: 1302
- 资源: 33
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页