Android开发欧酷天气,【一篇文章搞懂

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=“com.example.msi.coolweather.WeatherActivity”

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小时更新一次(如果用户不使用的时候)

public int onStartCommand(Intent intent, int flags, int startId) {

updateWeather();

updateBingPic();

AlarmManager manager = (AlarmManager)getSystemServi
ce(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);

}
ndingIntent pi = PendingIntent.getService(this,0,i,0);

manager.cancel(pi);

manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);

return super.onStartCommand(intent,flags,startId);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值