首页 > 其他分享 >Android获取所在地城市名

Android获取所在地城市名

时间:2023-03-30 20:08:26浏览次数:41  
标签:criteria String 所在地 获取 tempCityName location provider Android null

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.




标签:criteria,String,所在地,获取,tempCityName,location,provider,Android,null
From: https://blog.51cto.com/u_548275/6160122

相关文章

  • Android图片转换类 1. Bitmap去色,…
    publicclassImageTools{publicstaticBitmaptoGrayscale(BitmapbmpOriginal){intwidth,height;height=bmpOriginal.getHeight();width=bmpOriginal.getWidth();BitmapbmpGrayscale=Bitmap.createBitmap(width,height......
  • android 监听SDCard安装和卸载的代…
    //监听类privatefinalBroadcastReceiversdcardListener=newBroadcastReceiver(){@OverridepublicvoidonReceive(Contextcontext,Intentintent){Stringaction=intent.getAction();Log.d("TAG","sdcar......
  • android内存统计
    由于linux内核和nativeservice使用的内存在settings中统计不出来,因此对于分析内存相关问题时,使用procrank命令则能比较清晰的获取每个进程占用的内存资源$adbshellprocrankPIDVssRssPssUsscmdline15949668K39664K19857K17016Ksystem_server23629688K29676K1......
  • 关于android分辨率和使用iphone版…
    为了节省成本,开发app,一般情况下android和iphone用的是一套设计图。那适配怎么办?今天统计了下android的分辨率主要有:三星i9300 galaxys3 1280x720像素三星galaxys4  1920x1080像素还有经典的分辨率:854x480 还有部分960x540的。他们有个共同的特点,就是高宽比都是 1.......
  • Android中在控件上显示倒计时
    Android中在控件上显示倒计时Android中在控件上显示倒计时publicclassCountdownTimerActivityextendsActivity{privateTimeCounttime;privateButtonchecking;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodst......
  • WebRTC通信时获取速率(每秒帧数)相关信息
    在用WebRTC进行通信时,可以通过RTCPeerConnection对象的getStats方法获取相关的连接统计信息,以此获取每秒帧数。--ByBriskyu1 getStats的使用方法constpc=newRTCPeerConnection()//获取视频流对象varselector=pc.getRemoteStreams()[0].getAudioTracks()[0]//......
  • android 检测应用异常 U…
    继承接口UncaughtExceptionHandler,并重写里面的uncaughtException(Threadthread,Throwableex)方法,这样就可以监测应用程序的异常情况,做相应的处理:publicclassmyCustomExceptionHandlerimplementsUncaughtExceptionHandlerprivateUncaughtExceptionHandlerpublicm......
  • 利用 rpc 获取 boss __zp_stoken__
    1什么是RPCRPC,英文RangPaCong,中文让爬虫,旨在为爬虫开路,秒杀一切,让爬虫畅通无阻!开个玩笑,实际上RPC为远程过程调用,全称RemoteProcedureCall,是一种技术思想而非一种......
  • Android开发-Android常用组件-ToggleButton开关按钮 & Switch开关
    4.7 开关按钮ToggleButton和开关Switch 1.开关按钮ToggleButton 属性名说明android:disabledAlpha设置按钮在禁用时的透明度android:textOff......
  • c#通过DateTime.Now获取当前时间星期值
    1、获取星期数字例如:周四:4//int类型Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"));//string类型DateTime.Now.DayOfWeek.ToString("d"); 2、获取得......