package com.zhou.activity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class WhereAmIActivity extends Activity {
Button btn ;
//声明LocationManager对象
LocationManager loctionManager;
Handler handler;
TextView mText;
Location mloc;
StringBuffer sb=new StringBuffer();
int GPS_FAIL=0;
String address;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.btn);
mText=(TextView) findViewById(R.id.myLoctionText);
Log.e("", "111");
class myth extends Thread{
@Override
public void run() {
Log.e("", "thread run");
Message msg=new Message();
msg.what=10;
handler.sendMessage(msg);
Log.e("", "start");
//通过系统服务,取得LocationManager对象
loctionManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
Log.e("", "start--"+loctionManager.isProviderEnabled(LocationManager.GPS_PROVIDER));
if(loctionManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
//通过GPS位置提供器获得位置
Log.e("", "Gps");
Location location = loctionManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mloc=location;
if(location==null){
Message msggps=new Message();
msggps.what=0;
Log.e("", "loc-null");
handler.sendMessage(msggps);
}else{
Message msggps=new Message();
msggps.what=4;
Log.e("", "Gps..");
handler.sendMessage(msggps);
}
Log.e("", "Gps-end");
}else{
Log.e("", "发射GPS消息");
Message msgwifi=new Message();
msgwifi.what=0;
handler.sendMessage(msgwifi);
Log.e("", "准备WIFI");
}
if(loctionManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
Log.e("", "NET");
Location location = loctionManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
mloc=location;
if(location!=null){
Log.e("", "NET"+location.toString());
}
}
if(iswifi()){
Log.e("", "WIFI。。。");
requestWIFILocation();
Log.e("", "WIFI-end");
Log.e("isgprs", ""+isGPRS());
}else{
Log.e("", "发射WIFI失败消息");
Message msggprs=new Message();
msggprs.what=1;
handler.sendMessage(msggprs);
Log.e("", "准备GPRS ");
}
if(isGPRS()){
Log.e("", "GPRS..");
requestTelLoc();
Log.e("", "GPRS-end");
}else{
Log.e("", "GPRS失败,发射消息 ");
Message msgend=new Message();
msgend.what=2;
handler.sendMessage(msgend);
}
}};
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sb.delete(0, sb.length());
new myth().start();
}
});
Log.e("", "112");
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
String str;
switch(msg.what){
case 10:
str="正在尝试GPS定位\n";
sb.append(str);
mText.setText(sb);
break;
case 0:
str="GPS不可用,尝试WIFI网络定位\n";
sb.append(str);
mText.setText(sb);
// mText.setText("GPS不可用,尝试WIFI网络定位");
break;
case 1:
str="WIFI不可用,尝试GPRS网络定位\n";
sb.append(str);
mText.setText(sb);
// mText.setText("WIFI不可用,尝试GPRS网络定位");
break;
case 2:
str="GPRS网络不可用,无法定位\n";
sb.append(str);
mText.setText(sb);
// mText.setText("GPRS网络不可用,无法定位");
break;
case 4:
updateWithNewLocation(mloc);
break;
case 5:
updateWithNewLocation(mloc);
break;
case 6:
sb.append(address);
updateWithNewLocation(mloc);
break;
default:
mText.setText("正在定位。。。");
break;
}
}};
Log.e("", "113");
//通过GPS位置提供器获得位置
// String provider=LocationManager.GPS_PROVIDER;
// Location location = loctionManager.getLastKnownLocation(provider);
//使用标准集合,让系统自动选择可用的最佳位置提供器,提供位置
// Criteria criteria = new Criteria();
// criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度
// criteria.setAccuracy(Criteria.ACCURACY_COARSE); //设置粗略精确度
// criteria.setAltitudeRequired(false);//不要求海拔
// criteria.setBearingRequired(false);//不要求方位
// criteria.setCostAllowed(true);//允许有花费
// criteria.setPowerRequirement(Criteria.POWER_HIGH);//低功耗
// POWER_HIGH--POWER_LOW
// String provider = loctionManager.getBestProvider(criteria, true);
// String provider=loctionManager.NETWORK_PROVIDER;
// List<String> list=loctionManager.getAllProviders();
// list.size();
// Log.e("location", ""+list.size()+"--"+list.toString());
// requestWIFILocation();
// requestTelLoc();
// Log.e("location", "---"+loctionManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
//获得最后一次变化的位置
// Location location = loctionManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// Location location = loctionManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// if(location==null){
// Log.e("location", "null");
// }else{
// Log.e("location", "not null");
// }
//
//// Log.e("location", ""+location.toString());
// //显示在TextView中
// updateWithNewLocation(location);
//
//监听位置变化,2秒一次,距离10米以上
// loctionManager.requestLocationUpdates("network", 2000, 10, locationListener);
}
private boolean iswifi(){
ConnectivityManager conn = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(conn!=null){
NetworkInfo[] info=conn.getAllNetworkInfo();
if(info!=null){
for(NetworkInfo ni : info){
if(ni.getTypeName().equals("WIFI") && ni.isConnected()){
return true;
}
}
}
}
return false;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
WhereAmI-综合定位.rar (30个子文件)
WhereAmI-综合定位
.project 844B
project.properties 647B
src
com
zhou
activity
WhereAmIActivity.java 16KB
AndroidManifest.xml 1KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
layout
main.xml 602B
values
strings.xml 220B
assets
gen
com
zhou
activity
BuildConfig.java 159B
R.java 841B
.classpath 380B
bin
resources.ap_ 11KB
classes
com
zhou
activity
R$layout.class 391B
WhereAmIActivity.class 12KB
BuildConfig.class 343B
R$drawable.class 397B
R$attr.class 337B
WhereAmIActivity$1myth.class 3KB
WhereAmIActivity$1.class 1KB
R$id.class 415B
R.class 524B
R$string.class 453B
WhereAmIActivity$2.class 1KB
WhereAmIActivity$3.class 2KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 2KB
classes.dex 16KB
WhereAmI.apk 20KB
共 30 条
- 1
资源评论
- 程序猿小爱2014-11-10无法通过wif定位i
- ilovewuping2015-10-21貌似Wifi功能不行呢
博艺
- 粉丝: 181
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功