後臺
我們使用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();
}
}
});
}
}
}