天气开发4——第二行代码(酷欧天气)
来源:互联网 发布:tcp20端口和21端口 编辑:程序博客网 时间:2024/05/29 03:46
上一篇
后台
我们使用Alarm来实现后台的定时自动更新
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); long TriggerAtTime = SystemClock.elapsedRealtime()+8*60*60*1000;//8小时 PendingIntent pi = PendingIntent.getService(this, 0, i, 0); manager.cancel(pi); manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, TriggerAtTime, pi);
在服务中,我们在onStartCommand中实现定时自动下载功能,因为一旦服务启动后,onStartComand就会无限期执行下去。我们要记得在WeatherActivity中开启服务,因为此时所有数据都已经准备好,并且完整的呈现出来:
//显示天气数据 private void showWeatherInfo(Weather weather) { Intent intent = new Intent(this, AutoUpdateService.class); startService(intent); ....}
@Override public int onStartCommand(Intent intent, int flags, int startId) { updateWeather(); updatePic(); Intent i = new Intent(this, AutoUpdateService.class); AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); long TriggerAtTime = SystemClock.elapsedRealtime()+8*60*60*1000;//8小时 PendingIntent pi = PendingIntent.getService(this, 0, i, 0); manager.cancel(pi); manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, TriggerAtTime, pi); return super.onStartCommand(intent, flags, startId); }
完整的代码为:
public class AutoUpdateService extends Service { public AutoUpdateService() { } @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { updateWeather(); updatePic(); Intent i = new Intent(this, AutoUpdateService.class); AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); long TriggerAtTime = SystemClock.elapsedRealtime()+8*60*60*1000;//8小时 PendingIntent pi = PendingIntent.getService(this, 0, i, 0); manager.cancel(pi); manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, TriggerAtTime, pi); return super.onStartCommand(intent, flags, startId); } //更新图片 private void updatePic() { String address = "http://guolin.tech/api/bing_pic"; HttpUtil.sendOkHttpRequest(address, new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { String pic = response.body().string(); SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit(); editor.putString("pic", pic);//将更新的内容存储到缓存中 editor.apply(); } }); } //service是发生在界面显示出来以后,所以会有缓存,通过缓存获得weatherId来访问服务器 private void updateWeather() { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this); final String weatherText = preferences.getString("weather", null); if (weatherText != null) { Weather weather = Utility.handleWeatherResponse(weatherText); String weatherId = weather.basic.weatherId; String weatherUrl = "https://api.heweather.com/x3/weather?cityid=" + weatherId + "&key=bc0418b57b2d4918819d3974ac1285d9";// String weatherUrl = "https://api.heweather.com/x3/weather?cityid=" + weatherId + "&key=25b66c67c448456b88bd0f006f9adad9"; HttpUtil.sendOkHttpRequest(weatherUrl, new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { String weathterText = response.body().string(); Weather weather = Utility.handleWeatherResponse(weatherText); if (weather != null && "ok".equals(weather.status)) { SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit(); editor.putString("weather", weatherText); editor.apply(); } } }); } }}
代码下载
阅读全文
0 0
- 天气开发4——第二行代码(酷欧天气)
- 天气开发1——第二行代码(酷欧天气)
- 天气开发2——第二行代码(酷欧天气)
- 天气开发3——第二行代码(酷欧天气)
- 第一行代码酷欧天气开发(一)
- 第一行代码酷欧天气开发(二)
- 第一行代码酷欧天气开发(三)
- 第一行代码酷欧天气开发(四)
- 第一行代码酷欧天气开发(五)
- <第二行代码>中的天气应用
- 酷欧天气开发笔记3:显示天气信息
- 第一行代码酷欧天气开发详细总结
- 《第一行代码》之开发欧酷天气学习笔记
- 阅读郭林《第一行代码》的笔记——第14章 进入实战,开发酷欧天气
- 用kotlin方式打开《第一行代码:Android》之开发酷欧天气(1)
- 用kotlin方式打开《第一行代码:Android》之开发酷欧天气(2)
- 用kotlin方式打开《第一行代码:Android》之开发酷欧天气(最终版)
- 简单天气开发系列(二)——获取城市代码
- HDU-3966-Aragorn's Story(树链剖分)
- java连接Mysql数据库
- IEEE VIS Citations
- opencv2.4.9:为caffe编译精简的opencv_core,opencv_imgproc,opencv_highgui全静态库
- 自动装箱拆箱
- 天气开发4——第二行代码(酷欧天气)
- [AHK]函数的参数是传值,还是传引用?
- ZOJ--1008:Gnome Tetravex(剪枝dfs)
- TCP/IP协议
- IOS Swift3.1 获得文本宽度
- 安卓高级控件
- 动态规划:0-1背包问题
- QImage 与 cv::Mat 之间的相互转换
- 定义多个Activity以及启动一个Activity的方法