YOU'VE ENTERED A SIMPLE ANDROIDBLOG, WELCOME.

Develop And Improve。

极简天气开发日记Day05

Things have done

与上一版相比,这次完善了界面图标,完成了通过定位直接显示天气,添加了侧滑栏,设置了切换城市功能,程序入口也不再每次都是选择城市界面,而是直接显示上次退出前选择的城市,最后增加了一些提示信息。

先来完成图

http://pic.yupoo.com/333ddd/FPga8CUa/11zC03.jpg

http://pic.yupoo.com/333ddd/FPgacl5n/10MkoG.jpg

http://pic.yupoo.com/333ddd/FPgaeEIN/bLiQH.jpg

实现方法

1.定位功能

上次死在了处理定位回馈信息上,我错误的使用了getAddrStr()方法获得到了具体定位信息,由于天气Api只需要县级单位的名称,所以在转换过程中头疼了很久,因为有些是以区结尾,有些是县,还有镇,旗等等,困扰半天才想到难道百度没有一个直接返回县级单位名称的函数么,果然,翻一下Doc文档,找到了 getDistrict():
获取区/县信息这个函数,这教训了我以后要好好查阅官方提供的Doc文档,不要做些无用功。

1
2
3
4
5
6
7
String str = location.getDistrict();
String locate_result = str.substring(0, str.length() - 1);
//show weather
Intent intent=new Intent(ChooseAreaActivity.this,ShowWeatherActivity.class);
intent.putExtra("countyName",locate_result);
startActivity(intent);
finish();

获得县级单位后使用subString去掉最后一个字符,因为天气Api只接受去掉区县的名称

2.切换城市

在Utility类中的saveWeatherInfoBasic方法中添加了一个标识符

1
editor.putBoolean("city_selected",true);

在ChooseAreaActivity中onCreate()中添加判断

1
2
3
4
5
6
7
       SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
if(sharedPreferences.getBoolean("city_selected",false)){
Intent intent=new Intent(ChooseAreaActivity.this,ShowWeatherActivity.class);
startActivity(intent);
finish();
return;
}

由于是在侧滑栏上添加的切换城市功能,所以在对应点击事件中要将刚才的city_selected更新为false。

3.侧滑栏

侧滑栏使用的SpecialCyCi提供的AndroidResideMenu这一开源代码

这个代码代码量很少方便阅读,实际使用极其容易上手

按照开发者提供的提示导入相应资源后在主活动中添加dispatchKeyEvent和setUpMenu方法即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
        private void setUpMenu() {

// attach to current activity;
resideMenu = new ResideMenu(this);
resideMenu.setUse3D(true);
resideMenu.setBackground(R.drawable.menu_background);
resideMenu.attachToActivity(this);
// resideMenu.setMenuListener(menuListener);
//valid scale factor is between 0.0f and 1.0f. leftmenu'width is 150dip.
resideMenu.setScaleValue(0.9f);
resideMenu.setSwipeDirectionDisable(ResideMenu.DIRECTION_RIGHT);

// create menu items;
itemHome = new ResideMenuItem(this, R.drawable.icon_home, "切换城市");
// itemProfile = new ResideMenuItem(this, R.drawable.icon_profile, "Profile");
// itemCalendar = new ResideMenuItem(this, R.drawable.icon_calendar, "Calendar");
itemSettings = new ResideMenuItem(this, R.drawable.icon_settings, "设置");

itemHome.setOnClickListener(this);
itemSettings.setOnClickListener(this);
//itemProfile.setOnClickListener(this);
// itemCalendar.setOnClickListener(this);

resideMenu.addMenuItem(itemHome, ResideMenu.DIRECTION_LEFT);
resideMenu.addMenuItem(itemSettings, ResideMenu.DIRECTION_LEFT);
//resideMenu.addMenuItem(itemProfile, ResideMenu.DIRECTION_LEFT);

// You can disable a direction by setting ->
// resideMenu.setSwipeDirectionDisable(ResideMenu.DIRECTION_RIGHT);

findViewById(R.id.left_menu).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
}
});
}
public boolean dispatchKeyEvent(KeyEvent event) {
return super.dispatchKeyEvent(event);
}

4.退出提示

重写onBackPressed方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        @Override
public void onBackPressed() {
if (mBackKeyPressedTimes == 0) {
Toast.makeText(this, "再按一次退出程序 ", Toast.LENGTH_SHORT).show();
mBackKeyPressedTimes = 1;
new Thread() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
mBackKeyPressedTimes = 0;
}
}
}.start();
return;
} else {
this.finish();
}
super.onBackPressed();
}

Things to do

设置菜单具体内容、通知栏、桌面小部件、主界面背景优化