赞
踩
- class CityThread extends Thread {
-
- @Override
- public void run() {
- getCurrentProvinceAndCity();
- }
- }
- private void getCurrentProvinceAndCity() {
- Log.d("zhangmq", "getCurrentProvinceAndCity");
- String provinceName = null;
- String cnCityName = null;
- final String url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?";
- HttpGet httpGet = new HttpGet(url);
- Log.d("zhangmq", "getCurrentProvinceAndCity 1111111111");
- try {
- Log.d("zhangmq", "getCurrentProvinceAndCity 2222222222");
- HttpResponse httpResponse = new DefaultHttpClient()
- .execute(httpGet);
- Log.d("zhangmq", "getCurrentProvinceAndCity 33333333333333333333");
- StatusLine statusLine = httpResponse.getStatusLine();
- Log.d("zhangmq", "getCurrentProvinceAndCity 444444444444444444");
- Log.d("zhangmq","statusLine = "+statusLine);
- if (statusLine != null && statusLine.getStatusCode() == 200) {
- cityResult = EntityUtils.toString(httpResponse.getEntity())
- .trim();
- Log.d("zhangmq","cityResult = "+cityResult);
- cityHandler.sendEmptyMessage(GET_CITY_SUCCESS);
- Log.d("zhangmq","sendEmptyMessage = GET_CITY_SUCCESS");
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- cityHandler.sendEmptyMessage(GET_CITY_FAILURE);
- }
- }
- private Handler cityHandler = new Handler() {
-
- @Override
- public void handleMessage(Message msg) {
- // TODO Auto-generated method stub
- super.handleMessage(msg);
- if (msg.what == GET_CITY_SUCCESS) {
- String[] results = cityResult.split("\t");
- Log.d("zhangmq", "results.length = "+results.length);
- if (results.length >= 5) {
- provinceName = results[4];
- cnCityName = results[5];
- Log.d("zhangmq", "cityHandler provinceName = " + provinceName);
- Log.d("zhangmq", "cityHandler cnCityName = " + cnCityName);
- if (mStatusBarState.getCityState()) {
- iv_city.setVisibility(View.VISIBLE);
- tv_city.setVisibility(View.VISIBLE);
- tv_city.setText(cnCityName);
- }
- }
- saveProvineAndCnCityName(provinceName, cnCityName);
- }
- }
-
- };
- private void saveProvineAndCnCityName(String provinceName, String cityName) {
- Context otherAppContext = null;
- try {
- otherAppContext = mContext.createPackageContext("com.android.msettings",
- Context.CONTEXT_IGNORE_SECURITY);
- } catch (NameNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- SharedPreferences preferences = otherAppContext.getSharedPreferences(
- "share_pre", Context.MODE_MULTI_PROCESS);
- Editor editor = preferences.edit();
- editor.putString("province_name", provinceName);
- editor.putString("cn_city_name", cityName);
- editor.commit();
- }
在LauncherActivity中开启这个线程来获得当前城市:
new CityThread().start();
本来以为可以正确获得城市,但是结果是,根本就没有获取到。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。