Search in sources :

Example 1 with Weather

use of com.coolweather.android.gson.Weather in project coolweather by yeliheng.

the class WeatherActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= 21) {
        // 判断API版本>Android5.0
        // 使图片与状态栏融为一体
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
    }
    setContentView(R.layout.activity_weather);
    // 请求权限
    // 初始化控件
    bingPicImg = findViewById(R.id.bing_pic_img);
    swipeRefresh = findViewById(R.id.swipe_refresh);
    drawerLayout = findViewById(R.id.drawer_layout);
    navButton = findViewById(R.id.nav_button);
    swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
    weatherLayout = findViewById(R.id.weather_layout);
    titleCity = findViewById(R.id.title_city);
    titleUpdateTime = findViewById(R.id.title_update_time);
    degreeText = findViewById(R.id.degree_text);
    weatherInfoText = findViewById(R.id.weather_info_text);
    forecastLayout = findViewById(R.id.forecast_layout);
    aqiText = findViewById(R.id.aqi_text);
    pm25Text = findViewById(R.id.pm25_text);
    comfortText = findViewById(R.id.comfort_text);
    carWashText = findViewById(R.id.car_wash_text);
    sportText = findViewById(R.id.sport_text);
    // 存储数据到sd卡
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String weatherString = prefs.getString("weather", null);
    final String weatherId;
    String bingPic = prefs.getString("bing_pic", null);
    if (bingPic != null) {
        // 使用Glide开源库加载必应图片
        Glide.with(this).load(bingPic).into(bingPicImg);
    } else {
        // 加载必应图片
        loadBingPic();
    }
    if (weatherString != null) {
        // 检查是否存在缓存
        // 存在时直接本地解析天气数据
        Weather weather = Utility.handleWeatherResponse(weatherString);
        weatherId = weather.basic.weatherId;
        // 显示天气信息
        showWeatherInfo(weather);
    } else {
        // 无缓存时到服务器上查询
        weatherId = getIntent().getStringExtra("weather_id");
        weatherLayout.setVisibility(View.INVISIBLE);
        requestWeather(weatherId);
    }
    swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            requestWeather(weatherId);
        }
    });
    navButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // 打开滑动菜单
            drawerLayout.openDrawer(GravityCompat.START);
        }
    });
}
Also used : Weather(com.coolweather.android.gson.Weather) SharedPreferences(android.content.SharedPreferences) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout)

Example 2 with Weather

use of com.coolweather.android.gson.Weather in project coolweather by yeliheng.

the class AutoUpdateService method updateWeather.

/*
    * 更新天气信息
    * */
private void updateWeather() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String weatherString = prefs.getString("weather", null);
    if (weatherString != null) {
        Weather weather = Utility.handleWeatherResponse(weatherString);
        String weatherId = weather.basic.weatherId;
        // 请求地址
        String weatherUrl = "http://guolin.tech/api/weather?cityid=" + weatherId + "&key=" + key;
        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)) {
                    // 判断信息是否有效
                    // 存储数据到SD卡
                    SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(AutoUpdateService.this).edit();
                    editor.putString("weather", responseText);
                    editor.apply();
                }
            }
        });
    }
}
Also used : Weather(com.coolweather.android.gson.Weather) Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) SharedPreferences(android.content.SharedPreferences) IOException(java.io.IOException)

Aggregations

SharedPreferences (android.content.SharedPreferences)2 Weather (com.coolweather.android.gson.Weather)2 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 ScrollView (android.widget.ScrollView)1 TextView (android.widget.TextView)1 IOException (java.io.IOException)1 Call (okhttp3.Call)1 Callback (okhttp3.Callback)1 Response (okhttp3.Response)1