android WIFI定位和基站定位实现
来源:互联网 作者:佚名 时间:2015-04-01 13:38
关于定位原理网上很多,这里就不多说了。下面说怎么实现的,直接贴代码如下:首先是Util类:import java.io.BufferedReader;import java.io.InputStreamReader;i
关于定位原理网上很多,这里就不多说了。下面说怎么实现的,,直接贴代码如下:
首先是Util类:
import java.io.BufferedReader;
import java.io.InputStreamReader;
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.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
public class Util {
//log的标签
public static final String TAG = "location";
public static final boolean DEBUG = true;
public static final String LOCATION_URL = "";
public static final String LOCATION_HOST = "maps.google.com";
public static void logi(String content){
if(DEBUG) {
Log.i(TAG, content);
}
}
public static void loge(String content){
if(DEBUG) {
Log.e(TAG, content);
}
}
public static void logd(String content){
if(DEBUG) {
Log.d(TAG, content);
}
}
/**
* 获取地理位置
*
* @throws Exception
*/
public static String getLocation(String latitude, String longitude) throws Exception {
String resultString = "";
/** 这里采用get方法,直接将参数加到URL上 */
String urlString = String.format("?key=abcdefg&q=%s,%s", latitude, longitude);
Util.logi("Util: getLocation: URL: " + urlString);
/** 新建HttpClient */
HttpClient client = new DefaultHttpClient();
/** 采用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) {
throw new Exception("获取物理位置出现错误:" + e.getMessage());
} finally {
get.abort();
client = null;
}
return resultString;
}
/**
* 判断网络是否可用
* @param context
* @return
*/
public static boolean isNetworkAvaliable(Context context){
ConnectivityManager manager = (ConnectivityManager) (context
.getSystemService(Context.CONNECTIVITY_SERVICE));
NetworkInfo networkinfo = manager.getActiveNetworkInfo();
return !(networkinfo == null || !networkinfo.isAvailable());
}
/**
* 判断网络类型 wifi 3G
*
* @param context
* @return
*/
public static boolean isWifiNetwrokType(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.isAvailable()) {
if (info.getTypeName().equalsIgnoreCase("wifi")) {
return true;
}
}
return false;
}
}
在MainActivity中根据当前网络环境,决定用WIFI定位还是基站定位
import com.demo.location.cell.CellLocationManager;
import com.demo.location.wifi.WifiLocationManager;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView mTextView;
private Button mButton;
private WifiLocationManager wifiLocation;
private CellLocationManager cellLocation;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.tv_show_info);
mButton = (Button) findViewById(R.id.btn_get_info);
wifiLocation = new WifiLocationManager(MainActivity.this);
cellLocation = new CellLocationManager(MainActivity.this);
mButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Util.logi("MainActivity: on mButton Click!!!");
getLocation();
}
});
}
private void getLocation(){
boolean isNet = Util.isNetworkAvaliable(MainActivity.this);
if(!isNet){
Toast.makeText(MainActivity.this, "网络不可用:打开WIFI 或 数据连接!!!", Toast.LENGTH_LONG).show();
Util.logd("MainActivity: getLocation: Net work is not avaliable, and return!!!");
return;
}
boolean isWifi = Util.isWifiNetwrokType(MainActivity.this);
if(isWifi){
Util.logd("MainActivity: getLocation: Wifi定位");
wifiLocation.getLocation(new WifiReceiver());
}else{
Util.logd("MainActivity: getLocation: 基站定位");
String location = cellLocation.getLocationCell();
mTextView.setText(location);
}
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
Util.logi("get broadcastReceiver: SCAN_RESULTS_AVAILABLE_ACTION");
String location = wifiLocation.getLocationWifi();
mTextView.setText(location);
}
}
}
1. WIFI定位: