Android中,当开启WIFI或者GPRS,如何能获取到公网的IP,定位用户所在的位置???
解决方案 »
- 在service中如何给用户提示??
- 求大神帮我添加一个后台运行的代码吧
- android怎样估算电池电量可用时间 问题补充(参与酌情给分)
- 急:Activity中我设置点击某个EditText会跳出DatePickerDialog,没想到点击完后,再点其他EditText也跳出,怎么能设置不让它跳出
- 如何动态修改ListView中某个Item里的组件值
- 有安卓系统下的蒙古文软件吗?
- 安卓隐藏状态栏不全屏问题
- 广播中读写变量冲突问题
- [求助]真机Android Socket异常,无法接收返回数据
- PathMeasure 这个类怎么用的?sdk表示看不懂啊,搜也搜不到例子,有没有大神用过的。谢谢~在线等
- android里头有没有类似iphone badge的属性
- 有关map用户定位
http://fw.qq.com/ipaddress
public String GetNetIp(String ipaddr){
URL infoUrl = null;
InputStream inStream = null;
try {
infoUrl = new URL(ipaddr);
URLConnection connection = infoUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK)
{
inStream = httpConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8"));
StringBuilder strber = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
strber.append(line + "\n");
inStream.close();
return strber.toString();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
查看 System.out.println((GetNetIp("http://fw.qq.com/ipaddress")));
加权限 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
我的目的是这样的比如说一个人开启了WIFI或者GPRS,能够得到他的公网IP,并且定位他现在的大概位置!!!
private void openGPSSettings() {
LocationManager alm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Log.v("message","1");
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
{
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
Log.v("message","ok");
getLocation();
}
else
{
Log.v("message","wrong");
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent,0); //此为设置完成后返回到获取界面
}
}
private void getLocation() { // 获取位置管理服务
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);// 查找到服务信息
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息
Log.v("provider",provider);
Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
//String str = location.toString();
updateToNewLocation(location);
}
private void updateToNewLocation(Location location) {
TextView tv1;
tv1 = (TextView) this.findViewById(R.id.tv1);
if (location != null) {
double latitude = location.getLatitude();
double longitude= location.getLongitude();
tv1.setText("维度:" + latitude+ "\n经度" + longitude);
}
else {
tv1.setText("无法获取地理信息");
}
}
这是我找的网上的一个实现方法,没什么效果。
那个大牛给点思路啊???
怎么处理???