第一行代码酷欧天气开发(五)

时间:2021-12-05 03:27:18

为了更加智能,我们在这里假如后台自动更新天气的功能,这样就可以尽可能地保证每次打开软件时看到的都是最新的天气信息,想要实现这个功能,就需要创建一个长期在后台运行的定时任务。
我们在service包下新建一个AutoUpdateService继承自service

package com.coolweather.app.service;

import com.coolweather.app.receiver.AutoUpdateReceiver;
import com.coolweather.app.util.HttpCallbackListener;
import com.coolweather.app.util.HttpUtil;
import com.coolweather.app.util.Utility;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.SystemClock;
import android.preference.PreferenceManager;

public class AutoUpdateService extends Service{

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {

@Override
public void run() {
updateWeather();
}
}).start();
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
int anHour = 60*60*1000*8;//八小时的毫秒数
long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
Intent intent1 = new Intent(this,AutoUpdateReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent1, 0);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
return super.onStartCommand(intent1, flags, startId);
}
private void updateWeather() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String weatherCode = sharedPreferences.getString("weather_code", "");
String address = "http://www.weather.com.cn/data/cityinfo/" + weatherCode + ".html";
HttpUtil.sendHttpRequest(address, new HttpCallbackListener() {

@Override
public void onFinish(String response) {
Utility.handlerWeatherResponse(AutoUpdateService.this, response);
}

@Override
public void onError(Exception e) {
e.printStackTrace();
}
});
}

}

可以看到,在onStartCommand()方法中先是开启了一个子线程,然后在子线程中调用updateWeather()方法来更新天气,我们仍然会将服务器返回的天气数据交给UtilityhandleWeatherResponse()方法来处理,这样就可以把最新的天气信息存储到SharedPreferences()文件中

然后在receiver包下新建一个AutoUpdateReceiver继承自BroadcastReceiver

package com.coolweather.app.receiver;

import com.coolweather.app.service.AutoUpdateService;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AutoUpdateReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context,AutoUpdateService.class);
context.startService(intent1);
}

}

其实在我们的WeatherActivity里面的showWeatherInfo()方法中,有这么两行代码,看看

Intent intent = new Intent(this, AutoUpdateService.class);
startService(intent);

启动AutoUpdateService这个服务,这样只要选中了某个城市并成功更新天气后,AutoUpdateService就会在后台一直运行
最后,别忘了在xml文件中注册新增的服务和广播接收器

<service android:name=".service.AutoUpdateService"></service>
<receiver android:name=".receiver.AutoUpdateReceiver"></receiver>

好了,搞定!!!可能有很多地方有谬误。

CSDN下载地址,点我下载source code

github下载地址,点我下载source code