android:background="@color/colorPrimary">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”>
<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小
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
时更新一次(如果用户不使用的时候)
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);