1. static String cityName = "深圳";  //城市名   
2.    publicstaticString cityName ;  //城市名   
3.        
4.    privatestaticGeocoder geocoder;   //此对象能通过经纬度来获取相应的城市等信息   
5.        
6.       
7.    publicstaticvoidgetCNBylocation(Context context){    
8.            
9.        geocoder = newGeocoder(context);    
10.        //用于获取Location对象,以及其他   
11.        LocationManager locationManager;     
12.        String serviceName = Context.LOCATION_SERVICE;    
13.        //实例化一个LocationManager对象   
14.        locationManager = (LocationManager)context.getSystemService(serviceName);    
15.        //provider的类型   
16.        String provider = LocationManager.NETWORK_PROVIDER;    
17.    
18.        Criteria criteria = newCriteria();    
19.        criteria.setAccuracy(Criteria.ACCURACY_FINE);   //高精度   
20.        criteria.setAltitudeRequired(false);    //不要求海拔   
21.        criteria.setBearingRequired(false); //不要求方位   
22.        criteria.setCostAllowed(false); //不允许有话费   
23.        criteria.setPowerRequirement(Criteria.POWER_LOW);   //低功耗   
24.            
25.        //通过最后一次的地理位置来获得Location对象   
26.        Location location = locationManager.getLastKnownLocation(provider);    
27.            
28.        String queryed_name = updateWithNewLocation(location);    
29.        if((queryed_name != null) && (0!= queryed_name.length())){    
30.                
31.            cityName = queryed_name;    
32.        }    
33.            
34.           
35.        locationManager.requestLocationUpdates(provider, 30000, 50,    
36.                locationListener);    
37.        //移除监听器,在只有一个widget的时候,这个还是适用的   
38.        locationManager.removeUpdates(locationListener);    
39.    }    
40.        
41.       
42.    privatefinalstaticLocationListener locationListener = newLocationListener() {    
43.        String tempCityName;    
44.        publicvoidonLocationChanged(Location location) {    
45.                
46.            tempCityName = updateWithNewLocation(location);    
47.            if((tempCityName != null) && (tempCityName.length() != 0)){    
48.                    
49.                cityName = tempCityName;    
50.            }    
51.        }    
52.    
53.        publicvoidonProviderDisabled(String provider) {    
54.            tempCityName = updateWithNewLocation(null);    
55.            if((tempCityName != null) && (tempCityName.length() != 0)) {    
56.    
57.                cityName = tempCityName;    
58.            }    
59.        }    
60.    
61.        publicvoidonProviderEnabled(String provider) {    
62.        }    
63.    
64.        publicvoidonStatusChanged(String provider, intstatus, Bundle extras) {    
65.        }    
66.    };    
67.    
68.       
69.    privatestaticString updateWithNewLocation(Location location) {    
70.        String mcityName = "";    
71.        doublelat = 0;    
72.        doublelng = 0;    
73.        List<Address> addList = null;    
74.        if(location != null) {    
75.            lat = location.getLatitude();    
76.            lng = location.getLongitude();    
77.        } else{    
78.    
79.            System.out.println("无法获取地理信息");    
80.        }    
81.             
82.        try{    
83.                
84.            addList = geocoder.getFromLocation(lat, lng, 1);    //解析经纬度   
85.                
86.        } catch(IOException e) {    
87.            // TODO Auto-generated catch block   
88.            e.printStackTrace();    
89.        }    
90.        if(addList != null&& addList.size() > 0) {    
91.            for(inti = 0; i < addList.size(); i++) {    
92.                Address add = addList.get(i);    
93.                mcityName += add.getLocality();    
94.            }    
95.        }    
96.        if(mcityName.length()!=0){    
97.                
98.            returnmcityName.substring(0, (mcityName.length()-1));    
99.        } else{    
100.            returnmcityName;    
101.        }    
102.    }    
103.