Android中,当开启WIFI或者GPRS,如何能获取到公网的IP,定位用户所在的位置???

解决方案 »

  1.   

    这个网址可以查看外网IP
    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> 
      

  2.   

    怎么根据基站定位???
    我的目的是这样的比如说一个人开启了WIFI或者GPRS,能够得到他的公网IP,并且定位他现在的大概位置!!!
      

  3.   

    即使得到了IP,也只是知道了基站的位置, 所以要根据IP去定位位置,感觉不是很好
      

  4.   

     //判断GPRS是否开启
        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("无法获取地理信息");     
    }
    }
    这是我找的网上的一个实现方法,没什么效果。
    那个大牛给点思路啊???
      

  5.   

    locationManager.getLastKnownLocation("gps")获得的数据一直是NULL的
    怎么处理???