天气开发4——第二行代码(酷欧天气)

上一篇

后台

我们使用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();
                    }
                }
            });
        }

    }
}

代码下载

你可能感兴趣的:(Android开发)