





















Preview text:
TỔNG QUAN ĐỀ TÀI
I. LÝ DO CHỌN ĐỀ TÀI 
Đn vi hc k môn Android nâng cao, sinh viên đưc hc thêm nhiu
kin thc kh c như đ! d" liê $u, service, fragment, database, random,… t) 
đ* cung c+p thêm nn t-ng đ. ch/ng em c* th. t0 t1o cho mnh nh"ng 
ng d2ng Android mi th/ v3 hơn, đa chc năng hơn.
D0 b o th8i tit iWeather như l: mô $t công c2 d0 b o th8i tit cơ b-n 
đưc l+y t) OpenWeatherMap. Gi/p ngư8i d?ng c* th. xem đưc th8i 
tit cAa b+t k th:nh phB n:o mnh muBn: nhiê $t đô $, đô $ Dm, gi* v: mây. 
Ngo:i ra đ. ph2c v2 nhu cFu công viê $c, app cGn c* chc năng d0 b o th8i
tit cAa 7 ng:y k tip. Mong rIng vi app n:y sJ gi/p Kch cho mi ngư8i  trong cuô $c sBng hIng ng:y.
II. ĐỐI TƯỢNG NGHIÊN CỨU 
Đi tượng nghiên cứu: Nghiên cu, ng d2ng công c2 Android
Studio v: web site OpenWeatherMap.org
Phạm vi nghiên cứu: Ứng d2ng d0 b o th8i tit trên hệ điu h:nh Android. 
III. PHƯONG PHÁP NGHIÊN CỨU 
+ Về lý thuyết: 
Tm hi.u v hệ điu h:nh Android. 
Tm hi.u v ngôn ng" Anroid trong Android Studio.  1
C ch chn lc v: đ! d" liê $u t) Web xuBng App vi API KEY 
+ Về thực nghiệm: 
Xây d0ng ng d2ng iWeather đ. xem th8i tit dY d:ng trên điê $n tho1i Android. 2 MỤC LỤC
TỔNG QUAN ĐỀ TÀI............................................................
LỜI CẢM ƠN....................................................................
XÂY DỰNG ỨNG DgNG iWEATHER.............................................
1. T!ng quan đ t:i...............................................................
1.1. X c đ3nh yêu cFu b:i to n....................................................
1.2. M2c đKch nghiên cu........................................................
2. Xây d0ng ng d2ng............................................................
2.1. C+u tr/c chương trnh.......................................................
2.2. Bi.u tưng icon cAa chương trnh............................................
2.3 Lập trnh c c chc năng.......................................................
2.3.1 Giao diện m:n hnh ch8...................................................
2.3.2 C c chc năng chKnh.......................................................
3 Giao diện ng d2ng..............................................................
4 Kt luận........................................................................
4.1 Kt qu- đ1t đưc.............................................................
4.2 Hưng ph t tri.n.............................................................
TỔNG KẾT.......................................................................
TÀI LIỆU THAM KHẢO.......................................................... 3 LỜI CẢM ƠN -------- 
Xin chân th:nh c m ơn Ban gi m hiệu c?ng to:n th. quý thFy cô, đặc
biệt l: quý thFy cô Viện CNTT - Điện - Điện Tử Trư8ng Đ1i Hc B: R3a
Vũng T:u, nh"ng ngư8i đã truyn đ1t cho tôi nhiu kin thc quý b u trong hc tập.
Xin c m ơn cô NguyYn Th3 Minh Nương - gi-ng viên Viện CNTT -
Điện - Điện Tử l: ngư8i đã tận tnh hưng dẫn tôi trong suBt qu trnh ho:n th:nh đ t:i n:y Xin trân trng c-m ơn!  
Tp.Vũng Tàu, ngày 25 tháng 3 năm 2018 
 Sinh viên thực hiện  NguyYn Trung Hiu  
XÂY DỰNG ỨNG DỤNG iWEATHER
1. Tổng quan đề tài.
1.1. Xác định yêu cầu bài toán.
Ph2c v2 nhu cFu cuô $c sBng hIng ng:y: Xem th8i tit. App cung c+p
mi thông tin cơ b-n cFn thit cho ngư8i d?ng, cũng như l: đưa ra d0 b o
7 ng:y ti đ. thuâ $n tiê $n cho l3ch l:m viê $c cAa h.
1.2. Mục đích nghiên cứu.
Nghiên cu v: ng d2ng web OpenWeatherMap (ngu•n tin câ $y) p
d2ng trên nn t-ng Android đ. t1o ra s-n phDm d0 b o th8i tit ti tay
ngư8i d?ng mô $t c ch tr0c quan, dY sử d2ng.
2. Xây dựng ứng dụng.
2.1. Cấu trúc chương trình.
- Trưc tiên t1o một Android Project vi tên l: iWeather
- Câu tr/c chung cAa chương trnh g•m c c phFn như sau:
Hình 2.1.1 Cấu trúc chung của chương trình
 Thư mục AndroidMainfest.xml
- Khai báo tên thuộc tính của các activity.
Hình 2.1.2 File XML trong Proprties
 Thư mục layout trong file XML
- File Thit k giao diện c c layout.
Hình 2.1.3 File XML trong thư mục Layout
 Thư mục các file code
- Class d?ng đ. xử lý c c s0 kiện t1i c c m:n hnh.
Hình 2.1.4 Class code của sự kiện trong layout
 Hello_activity: Xử lý s0 kiện m:n hnh ch:o trưc khi ch1y v:o ng d2ng.
 Main2Activity: Xử lK c c s0 kiê $n „ m:n hnh th 2 (7 ng:y tip theo).
 MainActivity: Xử lý c c s0 kiện trong layout Main.
 iWeather_class: Kh„i t1o đBi tưng vi c c thuô $ c tKnh v:
phương thc tương đương.
 CustomAdapter_class: Kh„i t1o Adapter đ. p d2ng CustomListView
 Thư mục drawable: Cha to:n bộ file hnh -nh sử d2ng trong chương trnh.
Hình 2.1.5 File chứa hình ảnh
trong thư mục drawable
 Thư mục x9 l: Animation khi chuy;n gi
2.2. Biểu tượng icon của chương trình.
Hình 2.2.1 Bi;u tượng icon
Bi.u tưng Icon đă $t trong thư m2c mipmap vi kKch thưc đa  d1ng:
2.3 Lập trình các chức năng.
2.3.1 Giao diện màn hình chào.
Hình 2.3.1.1 Màn hình chờ
2.3.2 Các chức năng chính.  
Hình 2.3.2.1 Màn hình ch:nh
Khi nh+n c c n/t trên m:n hnh th ng d2ng sJ đưc đn m:n
hnh tương ng vi n/t chc năng trên giao diện m:n hnh.
package com.example.makio.iweather;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat; import java.util.Date;
public class MainActivity extends AppCompatActivity {  EditText  ; edtVitri
 Button btnOk, btnNextday;        TextView  tvTenTP,  tvTenNuoc,  tvTrangThai,  tvNhietDo,
tvNgayCapNhat, tvDoAm, tvGio, tvMay;  ImageView imgIcon;
 String City = "";  @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);  requestWindowFeature(Window. ); FEATURE_NO_TITLE
 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FUL LSCREEN,
 WindowManager.LayoutParams.FLAG_FULLSCREEN);
 setContentView(R.layout.activity_main);
 edtVitri = (EditText) findViewById(R.id.edtLocation);
 btnOk = (Button) findViewById(R.id.btnOk);   = (Button) findViewById(R.id. btnNextday  btnNext);  
= (TextView) findViewById(R.id. tvTenTP  tvCityName);                tvTenNuoc             = (TextView)
findViewById(R.id.tvCountryName);                tvTrangThai             = (TextView)
findViewById(R.id.tvTrangthai);  
= (TextView) findViewById(R.id. tvNhietDo  tvTemp);                tvNgayCapNhat             = (TextView)
findViewById(R.id.tvDateTime);
 tvDoAm = (TextView) findViewById(R.id.tvHumidity);
 tvGio = (TextView) findViewById(R.id.tvWind);
 tvMay = (TextView) findViewById(R.id.tvCloud);
 imgIcon = (ImageView) findViewById(R.id.imageView);  //Mă
c đnh 1 thnh ph khi m App lên
 GetCurrentWeatherData("Vung Tau");
 btnOk.setOnClickListener( View.OnClickListener() { new   @Override
 public void onClick(View view) {  String city =  .getText().toString(); edtVitri
 if(city.equals(""))  {
 City = "Vung Tau";  GetCurrentWeatherData( ); City  }  else  {  City = city;  GetCurrentWeatherData( ); City  }  GetCurrentWeatherData(city);  }  });  // Gi d liê u t MainActivity
 btnNextday.setOnClickListener(new View.OnClickListener() {  @Override
 public void onClick(View view) {  String city =  .getText().toString(); edtVitri
 Intent intent = new Intent(MainActivity. , this Main2Activity.class);
 intent.putExtra("name",city);  startActivity(intent);  //Chuy!n c"nh Activity
 overridePendingTransition(R.anim.slide_in_from_left,
R.anim.slide_out_to_left);  }  });  }  //L$y d liê u th%i ti&t
 public void GetCurrentWeatherData(String data)  {                 RequestQueue   requestQueue   =
Volley.newRequestQueue(MainActivity.this);
 String url = "http://api.openweathermap.org/data/2.5/weather?
q="+data+"&units=metric&appid=be8d3e323de722ff78208a7dbb2dcd6f";                StringRequest stringRequest =  new
StringRequest(Request.Method.GET,   url,  new Response.Listener() {  @Override
 public void onResponse(String response) {  try {  JSONObject jsonObject =  JSONObject(response); new 
 String day = jsonObject.getString("dt");
 String name = jsonObject.getString("name");
 tvTenTP.setText("Tên th4nh ph5: "+name);  //L$y d liê u ngy câ p nhât
 //Chuy!n bi&n Day thnh d1ng di
 long l = Long.valueOf(day);  //Gi4 tr c6a mili giây
 Date date = new Date(l*1000L);
 SimpleDateFormat simpleDateFormat = new
SimpleDateFormat("EEEE dd-MM-yyyy HH-mm-ss");
 String Day = simpleDateFormat.format(date);
 tvNgayCapNhat.setText(Day+ " updated");  JSONArray jsonArrayWeather =
jsonObject.getJSONArray("weather");
 JSONObject jsonObject1Weather =
jsonArrayWeather.getJSONObject(0);    String status =
jsonObject1Weather.getString("main");                                        String icon =
jsonObject1Weather.getString("icon");  //L$y d liê u t thư viê
n Pi c4t sô (Online)  Picasso. (MainActivity. with
this).load("http://open
weathermap.org/img/w/"+icon+".png").into(imgIcon);
 tvTrangThai.setText(status);  //L$y d liê u Nhiê t đô v Đô  >m
 JSONObject jsonObject1Main =
jsonObject.getJSONObject("main");    String nhietdo =
jsonObject1Main.getString("temp");                                        String doam =
jsonObject1Main.getString("humidity");
 Double a = Double.valueOf(nhietdo);
 String Nhietdo = String.valueOf(a.intValue());
 tvNhietDo.setText(Nhietdo+ ); "°C"
 tvDoAm.setText(doam+"%");  //L$y d liê u GI@ v Mây
 JSONObject jsonObjectWind =
jsonObject.getJSONObject("wind");
 String gio = jsonObjectWind.getString("speed");
 tvGio.setText(gio+"m/s");  JSONObject jsonObject1Cloud = jsonObject.getJSONObject( ); "clouds"
 String may = jsonObject1Cloud.getString( "all");
 tvMay.setText(may+"%");  //L$y d liê u Quc gia
 JSONObject jsonObject1Sys =
jsonObject.getJSONObject("sys");    String country =
jsonObject1Sys.getString("country");  tvTenNuoc.setText( +country); "M; qu5c gia: " 
 } catch (JSONException e) {  e.printStackTrace();  }  }  },
 new Response.ErrorListener() {  @Override
 public void onErrorResponse(VolleyError error) {  }  });
 requestQueue.add(stringRequest);  } }
Hình 2.3.2.2 Màn hình hiện 7 ngày gEm co
Listview và ItemListview(hình dưIi)
Các bư]c x_ lí sự kiê a n Kh„i t1o đBi tưng:
public class iWeather_class {
 public String Day;
 public String Status;
 public String Image;
 public String MaxTemp;
 public String MinTemp;
 public iWeather_class(String day, String status, String image,
String maxTemp, String minTemp) {  Day = day;  Status = status;  Image = image;   = maxTemp; MaxTemp    = minTemp; MinTemp   } } Kh„i t1o customAdapter
public class CustomAdaper_class extends BaseAdapter {  Context context;  ArrayList  ; arrayList
 public CustomAdaper_class(Context    context, ArrayList arrayList) {
 this.context = context;  this. = arrayList; arrayList   }  @Override  public int getCount()  {
 return arrayList.size();  }  @Override
 public Object getItem(int i)  {
 return arrayList.get(i);  }  @Override
 public long getItemId(int i) {  return ; 0  }  @Override  //%nh x' ki)u khác
 public View getView(int i, View view, ViewGroup viewGroup) {              
 LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 view = inflater.inflate(R.layout. , 
dong_listview null);
 iWeather_class iWeatherClass = arrayList.get(i);
 TextView tvDay = (TextView) view.findViewById(R.id.tvNgay);
 TextView tvStatus = (TextView) view.findViewById(R.id.tvTrangthai);
 TextView tvMaxTemp = (TextView) view.findViewById(R.id.tvMaxTemp);
 TextView tvMinTemp = (TextView) view.findViewById(R.id.tvMinTemp);              
 ImageView imgStatus = (ImageView)
view.findViewById(R.id.imgTrangthai);
 tvDay.setText(iWeatherClass.Day);
 tvStatus.setText(iWeatherClass.Status);
 tvMaxTemp.setText(iWeatherClass.MaxTemp+"đô a  C"); 
