<ImageView
android:id=“@+id/bing_pic_img”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:scaleType=“centerCrop” />
<android.support.v4.widget.SwipeRefreshLayout
android:id=“@+id/swipe_refresh”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<ScrollView
android:id=“@+id/weather_layout”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:scrollbars=“none”
android:overScrollMode=“never”>
<LinearLayout
android:orientation=“vertical”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:fitsSystemWindows=“true”>
</android.support.v4.widget.SwipeRefreshLayout>
<fragment
android:id=“@+id/choose_area_fragment”
android:name=“com.example.msi.coolweather.ChooseAreaFragment”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_gravity=“start”>
</android.support.v4.widget.DrawerLayout>
3、关于网页请求
写了sendOkHttpRequest方法对网页发送请求
public static void sendOkHttpRequest(String address,okhttp3.Callback callback){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(address).build();
client.newCall(request).enqueue(callback);
}
这是一个典型的Okhttp3的网页请求代码。
对address的Url发送请求,
返回的数据用handle来进行JSON处理
//解析天气JSON数据
public static Weather handleWeatherResponse(String response){
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray(“HeWeather”);
String weatherContent = jsonArray.getJSONObject(0).toString();
return new Gson().fromJson(weatherContent,Weather.class);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
4、后台更新服务:
重写onStartCommand每次开始时更新图片和天气,并且每隔8小时更新一次(如果用户不使用的时候)
public int onStartCommand(Intent intent, int flags, int startId) {
updateWeather();
updateBingPic();
AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);
int anHour = 8 * 60 * 60 * 1000; //8hour
long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
Intent i = new Intent(this,MyServiceAutoUpdateService.class);
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 updateBingPic() {
String requestBingPic = “http://guolin.tech/api/bing_pic”;
HttpUtil.sendOkHttpRequest(requestBingPic, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String bingPic = response.body().string();
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(MyServiceAutoUpdateService.this).edit();
editor.putString(“bing_pic”,bingPic);
editor.apply();
}
});
}
private void updateWeather() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String weatherString = prefs.getString(“weather”,null);
if(weatherString != null){
Weather weather = Utility.handleWeatherResponse(weatherString);
final String weatherId = weather.basic.weatherId;
String weatherUrl = “http://guolin.tech/api/weather?ciatyid=?” +
weatherId + “&key=cd510ea531714e62a45fe41560a2ab54”;
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 responseText = response.body().string();
Weather weather = Utility.handleWeatherResponse(responseText);
if(weather != null && “ok”.equals(weather.status)){
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(MyServiceAutoUpdateService.this).edit();
editor.putString(“weather”,responseText);
editor.apply();
}
}
});
}