天气+记事+闹钟——android程序
程序员文章站
2022-07-12 11:23:00
...
天气+记事+闹钟
写了一个关于天气+记事+闹钟的android程序,中间运用到了一些第三方库,写个博客记录一下Q_Q,废话不多说直接上代码,
代码的github (自行链接过去下载)https://github.com/yourkiwifruit/Note
运用的第三方库
- Butter Knife
- litepal等
(如果对第三方库不太了解的可以自行百度了解下)
数据库中的字段
(闹钟以及记事本中要运用到数据库,所以先把表放出来吧!)
Note_table | id | content | time | color_key | alarm_key |
---|---|---|---|---|---|
integer | text | text | integer | text |
下面来讲讲核心代码
(下面就来贴一下核心代码,以及实现的过程,具体需要还是自行下载源码)
记事本部分
为listview创建适配器,以及listview中的每一项都是运用了cardview,让呈现出来的item更美观一些。然后让listview去设置这个适配器,当页面打开的时候,会去本地数据库获取数据,放到list集合中,list集合会传递到适配器中,去完成每一项的绘制,最后得到界面。
代码如下:
Note_Adapter.java
public class Note_Adapter extends BaseAdapter {
private Context mcontext;
private ArrayList<Note_table> list;
public Note_Adapter(Context mcontext,ArrayList<Note_table> list){
this.mcontext = mcontext;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(mcontext).inflate(R.layout.list_item,parent,false);
Log.d("这里是Note_adapter",String.valueOf(list.size()));
CardView cardView = (CardView) convertView.findViewById(R.id.card);
ImageView naozhong_icon = (ImageView) convertView.findViewById(R.id.naozhong_icon);
TextView content = (TextView) convertView.findViewById(R.id.content);
TextView time = (TextView) convertView.findViewById(R.id.time);
if(list.get(position).getColor_key()==0){
cardView.setBackgroundResource(R.drawable.card_radius_black);
}
else if(list.get(position).getColor_key()==1){
cardView.setBackgroundResource(R.drawable.card_radius_chengse);
}
else if(list.get(position).getColor_key()==2){
cardView.setBackgroundResource(R.drawable.card_radius_hailu);
}
else if(list.get(position).getColor_key()==3){
cardView.setBackgroundResource(R.drawable.card_radius_red);
}
else if(list.get(position).getColor_key()==4){
cardView.setBackgroundResource(R.drawable.card_radius_qianzi);
}
else if(list.get(position).getColor_key()==5){
cardView.setBackgroundResource(R.drawable.card_radius_shenglan);
}
else if(list.get(position).getColor_key()==6){
cardView.setBackgroundResource(R.drawable.card_radius_luse);
}
if(list.get(position).getAlarm_key().isEmpty()){
naozhong_icon.setVisibility(View.GONE);
}
else {
naozhong_icon.setVisibility(View.VISIBLE);
}
Log.d("颜色",String.valueOf(list.get(position).getColor_key()));
content.setText(list.get(position).getContent().toString());
time.setText(list.get(position).getTime().toString());
return convertView;
}
}
Note_Home.java
@Override
protected void onStart() {
super.onStart();
list.clear();
Data_loading();
Log.d("这里是ONSTART",String.valueOf(list.size()));
note_adapter = new Note_Adapter(mcontext,(ArrayList<Note_table>)list);
listView.setAdapter(note_adapter);
}
//从数据库中加载数据
private void Data_loading(){
list = DataSupport.findAll(Note_table.class);
}
这里放一张配图吧Q_Q
天气部分
看到这里相信你们应该也看到界面上的天气图标了吧,对的,点击这个图标就能进入天气界面。直接上张图再说。(突然想到我的一个坏毛病,瞎扯一波,看别人博客的时候,如果没有图片配着的话,感觉不够专业,哈哈哈哈哈)
好的,来看看核心代码吧!
Weather_Home.java
//通过线程对天气请求得到json数据
private void getWeatherDatafromNet(String cityName)
{
final String address = "http://v.juhe.cn/weather/index?format=2&cityname="+cityName+"&key=我这个是聚合API的接口,你可以自己申请一个,我的就不拿出来的,略略略";
Log.d("Address:",address);
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection urlConnection = null;
try {
URL url = new URL(address);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(8000);
urlConnection.setReadTimeout(8000);
InputStream in = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
String str;
while((str=reader.readLine())!=null)
{
sb.append(str);
Log.d("date from url",str);
}
String response = sb.toString();
Log.d("response",response);
wea_info = ParseJson(response);
if(wea_info != null){
Log.d("标志:","进入Message");
Message message = new Message();
message.what = 1;
message.obj = wea_info;
myHandler.sendMessage(message);
}
}catch (Exception e)
{
e.printStackTrace();
}
}
}).start();
}
//对json数据进行解析,保存到Weather_info
private Weather_Info ParseJson(String json){
try {
wea_info = new Weather_Info();
JSONObject object = new JSONObject(json);
JSONObject object_result = object.getJSONObject("result");
JSONObject object_today = object_result.getJSONObject("today");
wea_info.setWea_local(object_today.getString("city"));
wea_info.setWea_state(object_today.getString("weather"));
wea_info.setWea_temperature(object_today.getString("temperature"));
wea_info.setDressing_advice(object_today.getString("dressing_advice"));
wea_info.setDate(Nowtime.Now());
JSONArray object_future = object_result.getJSONArray("future");
for(int i=1;i<object_future.length();i++) {
JSONObject obj = object_future.getJSONObject(i);
Weather_Info weather_info = new Weather_Info();
weather_info.setWea_state(obj.getString("weather"));
weather_info.setWea_temperature(obj.getString("temperature"));
weather_info.setWea_week(obj.getString("week"));
list.add(weather_info);
}
} catch (JSONException e) {
e.printStackTrace();
}
return wea_info;
}
new一个新的线程去访问接口,得到json数据,将拿到的json数据进行进一步的解析,放到自定义的Weather_Info中去,最后通过适配器,再将数据适配到自己listview中的每一项上去,其实过程和记事本部分的同理啦。
闹钟部分
还是老样子,先上图吧。
当你的定时成功后会在你的记事本内容的右边出现一个小的闹钟提示,表示你的闹钟已经定时成功。当时间到达时,会进行通知栏提醒。
好了,我又要上核心代码咯。
Note_Edit.java
//点击图标,进行定时操作
@OnClick(R.id.naozhong_icon)
public void naozhong_icon(View view){
if(alarm_date.length()<=1) {
//if no alarm clock has been set up before
//show the current time
Calendar c=Calendar.getInstance();
alarm_hour=c.get(Calendar.HOUR_OF_DAY);
alarm_minute=c.get(Calendar.MINUTE);
alarm_year=c.get(Calendar.YEAR);
alarm_month=c.get(Calendar.MONTH)+1;
alarm_day=c.get(Calendar.DAY_OF_MONTH);
Log.d("月份",String.valueOf(alarm_month));
}
else {
//show the alarm clock time which has been set up before
int i=0, k=0;
while(i<alarm_date.length()&&alarm_date.charAt(i)!='/') i++;
alarm_year=Integer.parseInt(alarm_date.substring(k,i));
k=i+1;i++;
while(i<alarm_date.length()&&alarm_date.charAt(i)!='/') i++;
alarm_month=Integer.parseInt(alarm_date.substring(k,i));
k=i+1;i++;
while(i<alarm_date.length()&&alarm_date.charAt(i)!=' ') i++;
alarm_day=Integer.parseInt(alarm_date.substring(k,i));
k=i+1;i++;
while(i<alarm_date.length()&&alarm_date.charAt(i)!=':') i++;
alarm_hour=Integer.parseInt(alarm_date.substring(k,i));
k=i+1;i++;
alarm_minute=Integer.parseInt(alarm_date.substring(k));
}
new TimePickerDialog(this,this,alarm_hour,alarm_minute,true).show();
new DatePickerDialog(this,this,alarm_year,alarm_month-1,alarm_day).show();
}
Note_Home.java
//定时变量
int alarm_hour;
int alarm_minute;
int alarm_year;
int alarm_month;
int alarm_day;
int i=0, k=0;
String alarm_date = data.getStringExtra("alarm_date");
if(alarm_date.length()>1){
int id = data.getIntExtra("Id",0);
while(i<alarm_date.length()&&alarm_date.charAt(i)!='/') i++;
alarm_year=Integer.parseInt(alarm_date.substring(k,i));
k=i+1;i++;
while(i<alarm_date.length()&&alarm_date.charAt(i)!='/') i++;
alarm_month=Integer.parseInt(alarm_date.substring(k,i));
k=i+1;i++;
while(i<alarm_date.length()&&alarm_date.charAt(i)!=' ') i++;
alarm_day=Integer.parseInt(alarm_date.substring(k,i));
k=i+1;i++;
while(i<alarm_date.length()&&alarm_date.charAt(i)!=':') i++;
alarm_hour=Integer.parseInt(alarm_date.substring(k,i));
k=i+1;i++;
alarm_minute=Integer.parseInt(alarm_date.substring(k));
//从数据库中拿到alarm_date数据进行定时操作
Intent intent = new Intent(mcontext, BroadcastAlarm.class);
intent.putExtra("alarmId",id);
PendingIntent sender = PendingIntent.getBroadcast(mcontext,id, intent, 0)
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//calendar.add(Calendar.SECOND, 5);
Calendar alarm_time = Calendar.getInstance();
alarm_time.set(alarm_year,alarm_month-1,alarm_day,alarm_hour,alarm_minute);
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
//if(interval==0)
am.set(AlarmManager.RTC_WAKEUP, alarm_time.getTimeInMillis(), sender);
}
}
好了到这就基本结束了,如果感觉我说的不清楚的话,就直接去看源码吧,希望能对你有所帮助。希望各位看官轻喷~
传送门在这里在放一个吧
代码的github (自行链接过去下载)https://github.com/yourkiwifruit/Note
上一篇: Spring的IOC底层原理
下一篇: Spring后置处理器