#趣米杯#码农天气--码农自己的天气APP(四)
来源:互联网 发布:iphone连接mac传照片 编辑:程序博客网 时间:2024/06/06 08:50
本文参与趣米杯征文活动,如需转载请注明出处和作者
#趣米杯#码农天气--码农自己的天气APP(一)
#趣米杯#码农天气--码农自己的天气APP(二)
#趣米杯#码农天气--码农自己的天气APP(三)
之前我们已经实现了一个天气app的基本功能,这一篇文章我会介绍几个给app添彩的功能:通知栏,桌面widget,以及最近大家都很关心的PM2.5。
首先简单说一下通知栏的实现,这个功能做起来很简单,只要你把布局文件设计好,然后通过NotificationManager展示一下就可以了,取消的时候也是通过NotificationManager来取消,这一块应该没有什么难度,我把我反解的彩虹天气的通知栏布局文件贴出来,大家借鉴一下:
复制代码
下面我们介绍一下桌面widget的实现,上面这个是彩虹天气的,下面的那个是码农天气的:
大家可以看到我做的这个比彩虹天气的少了一个农历日期的展示,其实这块是我偷懒了,我没有加上,大家如果有兴趣的话,可以自己尝试着加一下。
看完效果,我们看一下桌面Wigdet的原理,其实他就是一个广播接收者BroadcastReceiver,这是android的源码:
复制代码
大家掌握这一点,对于桌面widget的理解就简单多了,他只需要接收指定的广播,就可以做一些预定义好的更新事件了。
彩虹天气有好几种widget的形式,我只是选了其中一种来实现的,大家有兴趣的话可以都试试。
这个是我的widget的布局文件,同样也是反解的彩虹天气的:
复制代码
这一讲的最后,我们说一个大家都比较关心的知识点:PM2.5,这个功能彩虹天气以前是有的,不过最近一直都刷不出来数据,估计是他抓的数据源出问题了吧。
大家如果有PM2.5开发需求的,我给大家推荐一个比较好的数据源:PM25.in
大家开发的时候可以使用他提供的测试appKey:
公共测试AppKey:5j1znBVAsnSf5xQyNQyq这个网站的所有的appKey都是有访问次数限制的,所以这个公共的appKey经常获取不到数据,估计是很多人在用,我这里先把我申请的appKey贡献出来,大家都省着点用:
我申请的AppKey: YZGfRKfjgzJE78xFEjxg
我们可以到pm25.in的网站上看一下他提供的接口,每个接口需要上传的请求参数都很详细的列出来了,返回的数据都是Json格式的,这个对我们码农来说用起来就非常方便了,我用了我之前介绍的http://loopj.com/android-async-http/这个开源library项目来实现的这个功能:
复制代码
这个pm25的接口有个问题:就是每个小时的访问次数次数都有限制,其实这也很好解决的,我们只需要在服务器上做个缓存,再开个定时任务去pm25.in取数据就好了。不过昨天在巴士的版主群里问了一下,发现很多人都没有做过服务端的东西,我怕我写了个服务端的东西大家都看不懂,所以我就把这个PM25的功能做成了本地版的了,不过这样做自己玩玩也还行。
这一篇征文就写到这,下一篇我会介绍一下彩虹天气的菜单功能。
#趣米杯#码农天气--码农自己的天气APP(一)
#趣米杯#码农天气--码农自己的天气APP(二)
#趣米杯#码农天气--码农自己的天气APP(三)
之前我们已经实现了一个天气app的基本功能,这一篇文章我会介绍几个给app添彩的功能:通知栏,桌面widget,以及最近大家都很关心的PM2.5。
首先简单说一下通知栏的实现,这个功能做起来很简单,只要你把布局文件设计好,然后通过NotificationManager展示一下就可以了,取消的时候也是通过NotificationManager来取消,这一块应该没有什么难度,我把我反解的彩虹天气的通知栏布局文件贴出来,大家借鉴一下:
- <?xml version="1.0" encoding="UTF-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/widgetRL"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_margin="0.0dip" >
- <TextView
- android:id="@+id/cityTV"
- style="\@style/NotificationTitle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_marginLeft="8.0dip"
- android:layout_marginTop="2.0dip"
- android:textSize="16.0dip"
- android:textStyle="bold" />
- <TextView
- android:id="@+id/todayTempNow"
- style="\@style/NotificationText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/cityTV"
- android:layout_marginLeft="15.0dip"
- android:layout_marginTop="-4.0dip"
- android:textSize="22.0dip" />
- <LinearLayout
- android:id="@+id/days"
- android:layout_width="195.0dip"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:orientation="horizontal" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="2.0"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tvDay1"
- style="\@style/NotificationText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:textSize="10.0dip" />
- <ImageView
- android:id="@+id/imgIcon1"
- android:layout_width="32.0dip"
- android:layout_height="32.0dip"
- android:padding="3.0dip"
- android:scaleType="fitXY" />
- <TextView
- android:id="@+id/tvTemp1"
- style="\@style/NotificationText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="10.0dip" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="2.0"
- android:gravity="center_horizontal"
- android:orientation="vertical"
- android:paddingLeft="2.0dip"
- android:paddingRight="2.0dip" >
- <TextView
- android:id="@+id/tvDay2"
- style="\@style/NotificationText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:textSize="10.0dip" />
- <ImageView
- android:id="@+id/imgIcon2"
- android:layout_width="32.0dip"
- android:layout_height="32.0dip"
- android:padding="3.0dip"
- android:scaleType="fitXY" />
- <TextView
- android:id="@+id/tvTemp2"
- style="\@style/NotificationText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="10.0dip" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="2.0"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tvDay3"
- style="\@style/NotificationText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:textSize="10.0dip" />
- <ImageView
- android:id="@+id/imgIcon3"
- android:layout_width="32.0dip"
- android:layout_height="32.0dip"
- android:padding="3.0dip"
- android:scaleType="fitXY" />
- <TextView
- android:id="@+id/tvTemp3"
- style="\@style/NotificationText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="10.0dip" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="2.0"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tvDay4"
- style="\@style/NotificationText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:textSize="10.0dip" />
- <ImageView
- android:id="@+id/imgIcon4"
- android:layout_width="32.0dip"
- android:layout_height="32.0dip"
- android:padding="3.0dip"
- android:scaleType="fitXY" />
- <TextView
- android:id="@+id/tvTemp4"
- style="\@style/NotificationText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="10.0dip" />
- </LinearLayout>
- </LinearLayout>
- <ImageView
- android:id="@+id/todayImg"
- android:layout_width="wrap_content"
- android:layout_height="46.0dip"
- android:layout_alignParentTop="true"
- android:layout_marginLeft="10.0dip"
- android:layout_marginRight="10.0dip"
- android:layout_marginTop="4.0dip"
- android:layout_toLeftOf="@id/days"
- android:layout_toRightOf="@id/todayTempNow"
- android:padding="3.0dip"
- android:src="@drawable/day0" />
- <TextView
- android:id="@+id/tvTemp"
- style="\@style/NotificationText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/todayTempNow"
- android:layout_marginLeft="8.0dip"
- android:layout_marginTop="-3.0dip"
- android:textSize="12.0dip" />
- <TextView
- android:id="@+id/tvDes"
- style="\@style/NotificationText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/todayTempNow"
- android:layout_marginLeft="10.0dip"
- android:layout_marginRight="10.0dip"
- android:layout_marginTop="-3.0dip"
- android:layout_toLeftOf="@id/days"
- android:layout_toRightOf="@id/todayTempNow"
- android:gravity="center"
- android:textSize="12.0dip" />
- </RelativeLayout>
下面我们介绍一下桌面widget的实现,上面这个是彩虹天气的,下面的那个是码农天气的:
大家可以看到我做的这个比彩虹天气的少了一个农历日期的展示,其实这块是我偷懒了,我没有加上,大家如果有兴趣的话,可以自己尝试着加一下。
看完效果,我们看一下桌面Wigdet的原理,其实他就是一个广播接收者BroadcastReceiver,这是android的源码:
- public class AppWidgetProvider extends BroadcastReceiver {
- /**
- * Constructor to initialize AppWidgetProvider.
- */
- public AppWidgetProvider() {
- }
- // 。。。。其他的代码我就不贴了
- }
大家掌握这一点,对于桌面widget的理解就简单多了,他只需要接收指定的广播,就可以做一些预定义好的更新事件了。
彩虹天气有好几种widget的形式,我只是选了其中一种来实现的,大家有兴趣的话可以都试试。
这个是我的widget的布局文件,同样也是反解的彩虹天气的:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <RelativeLayout
- android:id="@+id/widgetsRL"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_margin="8.0dip" >
- <ImageView
- android:id="@+id/bgImg"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:scaleType="fitXY"
- android:src="@drawable/widget_black_bg" />
- <TextView
- android:id="@+id/clickTV"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
- <LinearLayout
- android:id="@+id/topLL"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <LinearLayout
- android:id="@+id/linearLayout1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="3.0"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tvTime"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="-2.0dip"
- android:textColor="@color/white_color"
- android:textSize="48.0dip" />
- <TextView
- android:id="@+id/tvDay"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10.0dip"
- android:textColor="@color/default_text_color"
- android:textSize="12.0dip" />
- <TextView
- android:id="@+id/tvWeek"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10.0dip"
- android:textColor="@color/dark_blue_color"
- android:textSize="12.0dip"
- android:visibility="gone" />
- </LinearLayout>
- <RelativeLayout
- android:id="@+id/widgetRL"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="3.0" >
- <TextView
- android:id="@+id/cityTV"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:layout_marginRight="3.0dip"
- android:layout_marginTop="3.0dip"
- android:drawableRight="@drawable/ic_next"
- android:textColor="@color/white_color"
- android:textSize="18.0dip"
- android:textStyle="bold" />
- <ImageView
- android:id="@+id/todayImg"
- android:layout_width="54.0dip"
- android:layout_height="54.0dip"
- android:layout_alignParentTop="true"
- android:layout_marginLeft="5.0dip"
- android:layout_marginTop="5.0dip"
- android:padding="4.0dip"
- android:src="@drawable/day0" />
- <TextView
- android:id="@+id/todayTempNow"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_below="@id/cityTV"
- android:layout_marginRight="5.0dip"
- android:layout_marginTop="-2.0dip"
- android:gravity="center"
- android:paddingRight="8.0dip"
- android:textColor="@color/white_color"
- android:textSize="28.0dip" />
- <TextView
- android:id="@+id/todayTemp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_below="@id/todayImg"
- android:layout_marginTop="3.0dip"
- android:paddingRight="8.0dip"
- android:textColor="@color/default_text_color"
- android:textSize="12.0dip" />
- <TextView
- android:id="@+id/todayDesc"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@id/todayImg"
- android:layout_below="@id/todayImg"
- android:layout_marginTop="3.0dip"
- android:paddingLeft="4.0dip"
- android:textColor="@color/default_text_color"
- android:textSize="12.0dip" />
- <TextView
- android:id="@+id/todayWet"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@id/todayImg"
- android:layout_below="@id/todayDesc"
- android:paddingLeft="4.0dip"
- android:textColor="@color/dark_blue_color"
- android:textSize="12.0dip"
- android:visibility="gone" />
- <TextView
- android:id="@+id/todayWind"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_below="@id/todayTemp"
- android:paddingRight="8.0dip"
- android:textColor="@color/dark_blue_color"
- android:textSize="12.0dip"
- android:visibility="gone" />
- </RelativeLayout>
- </LinearLayout>
- </RelativeLayout>
- </LinearLayout>
这一讲的最后,我们说一个大家都比较关心的知识点:PM2.5,这个功能彩虹天气以前是有的,不过最近一直都刷不出来数据,估计是他抓的数据源出问题了吧。
大家如果有PM2.5开发需求的,我给大家推荐一个比较好的数据源:PM25.in
大家开发的时候可以使用他提供的测试appKey:
公共测试AppKey:5j1znBVAsnSf5xQyNQyq这个网站的所有的appKey都是有访问次数限制的,所以这个公共的appKey经常获取不到数据,估计是很多人在用,我这里先把我申请的appKey贡献出来,大家都省着点用:
我申请的AppKey: YZGfRKfjgzJE78xFEjxg
我们可以到pm25.in的网站上看一下他提供的接口,每个接口需要上传的请求参数都很详细的列出来了,返回的数据都是Json格式的,这个对我们码农来说用起来就非常方便了,我用了我之前介绍的http://loopj.com/android-async-http/这个开源library项目来实现的这个功能:
- RequestParams params = new RequestParams();
- DBHelper dbHelper = new DBHelper(context, "db_weather.db");
- params.put("city", dbHelper.getCityNameByCode(cityCode));
- params.put("token", "YZGfRKfjgzJE78xFEjxg");
-
- AsyncHttpClient client = new AsyncHttpClient();
- client.get("http://www.pm25.in/api/querys/aqi_details.json", params, new JsonHttpResponseHandler() {
- @Override
- public void onStart() {
- super.onStart();
- // 开始的回调
- }
- @Override
- public void onSuccess(JSONObject response) {
- // 成功后的回调
- try {
- aqiMsgTV.setText("PM2.5:"+response.getString("pm2_5_24h")); // 这个就是pm2.5的数据
- } catch (Exception e) {
- e.printStackTrace();
- }
- super.onSuccess(response);
- }
- @Override
- public void onFailure(Throwable error) {
- super.onFailure(error);
- // 失败后的回调
- }
- });
这个pm25的接口有个问题:就是每个小时的访问次数次数都有限制,其实这也很好解决的,我们只需要在服务器上做个缓存,再开个定时任务去pm25.in取数据就好了。不过昨天在巴士的版主群里问了一下,发现很多人都没有做过服务端的东西,我怕我写了个服务端的东西大家都看不懂,所以我就把这个PM25的功能做成了本地版的了,不过这样做自己玩玩也还行。
这一篇征文就写到这,下一篇我会介绍一下彩虹天气的菜单功能。
本主题由 System 于 2014-1-14 18:00 解除限时高亮
0 0
- #趣米杯#码农天气--码农自己的天气APP(四)
- #趣米杯#码农天气--码农自己的天气APP(一)
- #趣米杯#码农天气--码农自己的天气APP(二)
- #趣米杯#码农天气--码农自己的天气APP(三)
- #趣米杯#码农天气--码农自己的天气APP(五)
- 码农天气--码农自己的天气APP(一)
- "晴天"天气app的开源库逐步学习(四)
- 天气APP的实战(1)
- 新人自己开发的天气app,有兴趣的可以交流
- RxAndroid 应用(四)(简易天气App之RxBus)
- 天气类APP产品微体验(墨迹天气、中央天气预报、雅虎天气、2345天气王)
- Android 端天气预报APP的实现(四)使用ViewPager展示天气数据
- 基于Android的天气APP
- 自己的网页加载天气
- 【酷我天气】完整的天气App项目
- 天气
- 天气
- 天气
- PHP多进程
- java中(优先队列)PriorityQueue的使用
- Android 开发环境搭建
- 【转载】分布式机器学习的故事
- 做网站建设前的准备
- #趣米杯#码农天气--码农自己的天气APP(四)
- Linux 下载文件命令
- 浅析HTTP协议
- android版本更新
- eclipse代码提示配置方法
- 2013年度总结
- 回文
- java中重写equals方法
- 2014-2-20 同步学习进度总结