Skip to content

Commit

Permalink
Merge pull request #83 from CaptainHuangsh/33-bg-pic
Browse files Browse the repository at this point in the history
33 bg pic
  • Loading branch information
CaptainHuangsh authored Jun 3, 2017
2 parents ad67f09 + 7e4c469 commit 98c2d43
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.owen.weathergo.R;
import com.example.owen.weathergo.modules.domain.Weather;
Expand All @@ -22,7 +21,7 @@ public class Test extends Activity {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
((Button) findViewById(R.id.test)).setOnClickListener(new View.OnClickListener() {
findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// JSONUtil jsonUtil2 = new JSONUtil(getApplicationContext(), "luoyang");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class DoubleClickExit {
/**
* 双击退出检测, 阈值 1000ms
*/
public static long mLastClick = 0L;
private static long mLastClick = 0L;
private static final int THRESHOLD = 2000;// 1000ms

public static boolean check() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ public void setYesOnclickListener(String str, onYesOnclickListener


public interface onNoOnclickListener {
public void onNoClick();
void onNoClick();
}

public interface onYesOnclickListener {
public void onYesClick();
void onYesClick();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.owen.weathergo.modules.dao;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
Expand All @@ -12,8 +11,6 @@
import com.example.owen.weathergo.modules.domain.Weather;
import com.example.owen.weathergo.util.IconGet;

import java.util.ArrayList;

/**
* Created by owen on 2017/4/21.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void bind(Weather weather) {
}
}
} catch (Exception e) {

e.printStackTrace();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void bind(Weather weather) {
suggFluBrief.setText(weather.getSuggestion().getFlu().getBrf());
suggFluTxt.setText(weather.getSuggestion().getFlu().getTxt());
} catch (Exception e) {

e.printStackTrace();
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,69 @@
package com.example.owen.weathergo.modules.dao;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.owen.weathergo.R;
import com.example.owen.weathergo.common.base.BaseViewHolder;
import com.example.owen.weathergo.modules.domain.Weather;
import com.example.owen.weathergo.util.IconGet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
* Created by owen on 2017/4/24.
*/

public class TodayWeatherHolder extends BaseViewHolder<Weather> {

//TODO 每日首次实时刷新图片的问题
//TODO 字体颜色的问题
private final String TAG = TodayWeatherHolder.class.getSimpleName();
private Context mContext;
TextView mTemp_min;
TextView mTemp_max;
TextView mCountry;
TextView mWind_speed;
TextView mTemp;
ImageView mImg;
private Weather weather;
private TextView mTemp_min;
private TextView mTemp_max;
private TextView mCountry;
private TextView mWind_speed;
private TextView mTemp;
private ImageView mImg;
private ImageView mBingPic;

private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
String bingPic = msg.toString();
Glide.with(mContext).load(bingPic).into(mBingPic);
break;
}
}
};

public TodayWeatherHolder(View view, Weather weather) {
super(view);
this.weather = weather;
mContext = view.getContext();
mTemp_min = (TextView) view.findViewById(R.id.weather_temp_min);
mTemp_max = (TextView) view.findViewById(R.id.weather_temp_max);
mCountry = (TextView) view.findViewById(R.id.weather_air);
mWind_speed = (TextView) view.findViewById(R.id.weather_wind_speed);
mTemp = (TextView) view.findViewById(R.id.weather_temp);
mImg = (ImageView) view.findViewById(R.id.weather_img);
mBingPic = (ImageView) view.findViewById(R.id.bg_pic);
}

@Override
Expand All @@ -60,9 +88,59 @@ public void bind(Weather weather) {
weather.getAqi().getCity().getQlty().length() < 2 ? mContext.getResources().getString(R.string.air)
+ weather.getAqi().getCity().getQlty() : weather.getAqi().getCity().getQlty());
mImg.setImageResource(IconGet.getWeaIcon(weather.getNow().getCond().getTxt()));
} catch (Exception e) {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
String today = sf.format(c.getTime());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
String bingPic = prefs.getString("bing_pic", null);
String date = prefs.getString("date", null);
if (bingPic != null && today.equals(date)) {
Glide.with(mContext).load(bingPic).into(mBingPic);
} else {
loadPic();
}

} catch (Exception e) {
e.printStackTrace();
}
}

private void loadPic() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
final String today = sf.format(c.getTime());
String requestBingPic = "http://guolin.tech/api/bing_pic";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(requestBingPic)
.build();
Call call = client.newCall(request);
call.enqueue(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(mContext).edit();
editor.putString("bing_pic", bingPic);
editor.putString("date", today);
editor.apply();
new Thread(new Runnable() {
@Override
public void run() {
Message message = new Message();
message.what = 0;
message.obj = bingPic;
mHandler.sendMessage(message);
}
});
// Glide.with(mContext).load(bingPic).into(mBingPic);
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -226,7 +225,19 @@ public void setListener() {
@Override
public void onClick(View v) {
initRecycleView();
refresh();
String Ccity = SharedPreferenceUtil.getInstance().getCityName();
if (!Ccity.equals(mCityStr)) {
mCityStr = Ccity;
new Thread(new Runnable() {
@Override
public void run() {
mWeather = JSONUtil.getInstance().getWeather(getActivity(), mCityStr);
Message message = new Message();
message.what = UPDATE_WEATHER_DATA;
mHandler.sendMessage(message);
}
}).start();
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
Expand All @@ -22,7 +24,6 @@
import com.example.owen.weathergo.util.IconGet;
import com.example.owen.weathergo.util.JSONUtil;
import com.example.owen.weathergo.util.SharedPreferenceUtil;
import com.example.owen.weathergo.util.ToastUtil;

/**
* Created by owen on 2017/5/1.
Expand All @@ -32,11 +33,28 @@
public class AutoUpdateService extends Service {
//http://www.jianshu.com/p/67c1d82b50b7
private final String TAG = AutoUpdateService.class.getSimpleName();
private SharedPreferences preferences;
// private SharedPreferences preferences;
private boolean mNotificationMode; //通知栏常驻
private boolean mVibrate; //天气推送震动
// private boolean mVibrate; //天气推送震动
private int mUpdateTime;
Weather mWeather;
private Weather mWeather;

private Handler mHandler = new Handler() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
final String Ccity = SharedPreferenceUtil.getInstance().getCityName();
Weather weather = JSONUtil.getInstance().getWeather(getApplicationContext(), Ccity);
if (weather != null) {
Log.d("huangshaohua notification", "" + weather.getBasic().getCity());
createNotification(weather);
}
break;
}
}
};

@Nullable
@Override
Expand All @@ -47,22 +65,30 @@ public IBinder onBind(Intent intent) {
@Override
public void onCreate() {
super.onCreate();


}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
updateWeather();
new Thread(new Runnable() {
@Override
public void run() {
updateWeather();
Message msg = new Message();
msg.what = 0;
mHandler.sendMessage(msg);
}
}).start();//不要忘了start

// createNotification(mWeather);
PreferenceManager.setDefaultValues(this, R.xml.pref_settings, false);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
mNotificationMode = preferences.getBoolean("notification_mode", false);
mVibrate = preferences.getBoolean("vibrate", false);
// mVibrate = preferences.getBoolean("vibrate", false);
mUpdateTime = Integer.valueOf(preferences.getString("update_time", "3"));
if (mWeather != null) {
/*if (mWeather != null) {
createNotification(mWeather);
}
}*/

if (mUpdateTime > 0) {
//几种定时刷新的方式 http://blog.csdn.net/wanglixin1999/article/details/7874316
Expand Down Expand Up @@ -111,7 +137,9 @@ private void createNotification(Weather weather) {
}

private void updateWeather() {
String Ccity = SharedPreferenceUtil.getInstance().getCityName();
Log.d("huangshaohua ", "updateWeather");
final String Ccity = SharedPreferenceUtil.getInstance().getCityName();
mWeather = JSONUtil.getInstance().getWeather(getApplicationContext(), Ccity);
Log.d("huangshaohua notification0", "" + mWeather.getBasic().getCity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@

public class SharedPreferenceUtil {

public static final String CITY_NAME = "city";//选择城市
public static final String HOUR = "current_hour";//当前小时
public static final String CHANG_ICONS = "change_icons";//切换图标
public static final String AUTO_UPDATE = "auto_update_time";//自动更新时长
public static final String CLEAR_CACHE = "clear_cache";//清空缓存
public static final String NOTIFICATION_MODE = "notification_mode";//notification常开与否
private static final String CITY_NAME = "city";//选择城市
private static final String HOUR = "current_hour";//当前小时
private static final String CHANG_ICONS = "change_icons";//切换图标
private static final String AUTO_UPDATE = "auto_update_time";//自动更新时长
// public static final String CLEAR_CACHE = "clear_cache";//清空缓存
private static final String NOTIFICATION_MODE = "notification_mode";//notification常开与否

private SharedPreferences mSharedPreference;

public static SharedPreferenceUtil getInstance() {
return SPHolder.sInstance;
}

public static class SPHolder {
private static class SPHolder {
private static SharedPreferenceUtil sInstance = new SharedPreferenceUtil();
}

Expand Down
Binary file added app/src/main/res/drawable/play.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 98c2d43

Please sign in to comment.