1. Trang chủ
  2. » Luận Văn - Báo Cáo

XÂY DỰNG ỨNG DỤNG điều KHIỂN THIẾT bị TRONG NHÀ

15 2 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

ĐẠI HỌC QUỐC GIA TP H CHệ MINH TR NG ĐẠI HỌC CÔNG NGH THÔNG TIN - Đ ÁN MÔN HỌC ĐỀ TÀI XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ Sinh viên th c hi n: Đinh Thanh Tùng Mssv: 11520464 Gi ng viên h ng dẫn: Phan Đình Duy H Chí Minh, 2015 XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ MỤC LỤC CH NG – T NG QUAN NG D NG CH NG - CHI TI T TH C HI N 2.1 Giao di n ch ng trình 2.2 Code ch c c a thƠnh phần CH NG - K T QU 15 TÀI LI U THAM KH O 15 Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ CH ƠNG – T NG QUAN ỨNG DỤNG Detect Internet Connection & Detect Device Status Living Room List Device Bed Room List Device Kitchen Room List Device Device & Control Device & Control Device & Control Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ CH ƠNG - CHI TIẾT TH C HI N 2.1 Giao diện chương trình Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ - Để t o List View m rộng, ta cần ba file layout xml Đầu tiên lƠ cho listview chính, đ i với m c xem danh sách nhóm vƠ lƠ lƠ cho xem danh sách m c M activity_main.xml vƠ thêm phần tử ExpandableListView - T o layout xml cho tiêu đề nhóm xem danh sách T o tập tin xml có tên list_group.xml - T o tập tin xml nhiều tên list_item.xml cho danh m c Điều nƠy s ch a y u t TextView đ n gi n Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ - Để kiểm tra thi t bị on/off Đầu tiên ta ph i kiểm tra k t n i đ n server đơy ta kiểm tra có Internet khơng r i sau n u có ta s k t n i server vƠ lấy tình tr ng c a thi t bị đư đ c l u Server tr ớc Để truy cập internet, ta cần Permission INTERNET Để phát hi n tình tr ng m ng, chúng tơi cần ACCESS_NETWORK_STATE Permission file AndroidManifest.xml - Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ 2.2 Code chức thành phần a) Expandable List View: Ta sử d ng lớp adapter tùy chỉnh để t o danh sách xem T o tập tin lớp gọi lƠ ExpandableListAdapter.java vƠ m rộng nƠy từ BaseExpandableListAdapter Lớp nƠy cung cấp ph ng pháp cần thi t để lƠm cho listview getGroupView () - Tr l i tiêu đề nhóm danh sách getChildView () - Tr l i m c danh sách package com.example.mypc.firstapps; import import import import import import import import import import import import java.util.HashMap; java.util.List; android.content.Context; android.graphics.Typeface; android.view.LayoutInflater; android.view.View; android.view.ViewGroup; android.widget.BaseExpandableListAdapter; android.widget.Switch; android.widget.TextView; android.widget.*; android.graphics.*; public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context _context; private List _listDataHeader; // header titles // child data in format of header title, child title private HashMap _listDataChild; public ExpandableListAdapter(Context context, List listDataHeader, HashMap listChildData) { this._context = context; this._listDataHeader = listDataHeader; this._listDataChild = listChildData; } @Override public Object getChild(int groupPosition, int childPosititon) { return this._listDataChild.get(this._listDataHeader.get(groupPosition) ) get(childPosititon); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } public View viewTest; @Override public View getChildView(int groupPosition, final int Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (String) getChild(groupPosition, childPosition); if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) this._context getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.list_item, null); } TextView txtListChild = (TextView) convertView findViewById(R.id.lblListItem); Switch abc= (Switch) convertView.findViewById(R.id.btn_check); final View finalConvertView = convertView; convertView.setBackgroundColor(Color.TRANSPARENT); abc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { CharSequence text = "Device ON"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(_context, text, duration); toast.show(); finalConvertView.setBackgroundColor(Color.argb(255, 120, 230, 230)); } else { finalConvertView.setBackgroundColor(Color.TRANSPARENT); } } }); txtListChild.setText(childText); return convertView; } @Override public int getChildrenCount(int groupPosition) { return this._listDataChild.get(this._listDataHeader.get(groupPosition) ) size(); } @Override public Object getGroup(int groupPosition) { return this._listDataHeader.get(groupPosition); Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ } @Override public int getGroupCount() { return this._listDataHeader.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) this._context getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.list_group, null); } TextView lblListHeader = (TextView) convertView findViewById(R.id.lblListHeader); lblListHeader.setTypeface(null, Typeface.BOLD); lblListHeader.setText(headerTitle); return convertView; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } Trong file MainActivity: public void showAlertDialog(Context context, String title, String message, Boolean status) { AlertDialog alertDialog = new AlertDialog.Builder(context).create(); // Setting Dialog Title alertDialog.setTitle(title); Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ 10 // Setting Dialog Message alertDialog.setMessage(message); // Setting alert dialog icon alertDialog.setIcon((status) ? R.drawable.in : R.drawable.off); // Setting OK Button alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); // Showing Alert Message alertDialog.show(); } ExpandableListAdapter listAdapter; ExpandableListView expListView; List listDataHeader; HashMap listDataChild; /* * Preparing the list data */ private void prepareListData() { listDataHeader = new ArrayList(); listDataChild = new HashMap(); // Adding child data listDataHeader.add("Living Room"); listDataHeader.add("Bed Room"); listDataHeader.add("Kitchen"); // Adding child data List livingroom = new ArrayList(); livingroom.add("Tivi 01"); livingroom.add("Tivi 02"); livingroom.add("Fan 01"); livingroom.add("Fan 02"); livingroom.add("Air Condition 01"); livingroom.add("Air Condition 02"); livingroom.add("Light Left"); livingroom.add("Light Right"); livingroom.add("Light Center"); List Bedroom = new ArrayList(); Bedroom.add("Tivi"); Bedroom.add("Air Condition"); Bedroom.add("Fan"); Bedroom.add("Fan sub"); Bedroom.add("Light For Sleep"); Bedroom.add("Light Center"); List Kitchen = new ArrayList(); Kitchen.add("Tivi"); Kitchen.add("Fan"); Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ 11 Kitchen.add("Light"); Kitchen.add("Air Condition"); Kitchen.add("Refrigerator"); listDataChild.put(listDataHeader.get(0), livingroom); // Header, Child data listDataChild.put(listDataHeader.get(1), Bedroom); listDataChild.put(listDataHeader.get(2), Kitchen); } } b) Detect Internet Connection Status: T o tập tin Class vƠ đặt tên lƠ ConnectionDetector.java package com.example.mypc.firstapps; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class ConnectionDetector { private Context _context; public ConnectionDetector(Context context){ this._context = context; } public boolean isConnectingToInternet(){ ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) for (int i = 0; i < info.length; i++) if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } return false; } } M activity_main.xml t o nút Sau m MainActivity Trong đo n ta cho m hộp tho i thơng báo tình tr ng k t n i public class MainActivity extends Activity { // flag for Internet connection status Boolean isInternetPresent = false; // Connection detector class ConnectionDetector cd; JSONParser jsonParser = new JSONParser(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnStatus = (Button) findViewById(R.id.btn_check); // creating connection detector class instance cd = new ConnectionDetector(getApplicationContext()); // get the listview expListView = (ExpandableListView) findViewById(R.id.lvExp); // preparing list data prepareListData(); listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); // setting list adapter expListView.setAdapter(listAdapter); /** * Check Internet status button click event * */ btnStatus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // get Internet status isInternetPresent = cd.isConnectingToInternet(); // check for Internet status if (isInternetPresent) { // Internet Connection is Present Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ 13 // make HTTP requests showAlertDialog(MainActivity.this, "Device Status", "You have device connection", true); } else { // Internet connection is not present // Ask user to connect to Internet showAlertDialog(MainActivity.this, "Device Status", "No active devices", false); } } }); c) On/Off and Toast Để dùng l nh bật tắt thi t bị phòng ta dùng switch Khi thi t bị bật lên sau bật ta dùng toast để hi n thông báo đư bật thi t bị lên Trong file list_item.xlm ta t o switch Trong file ExpandableListAdapter TextView txtListChild = (TextView) convertView findViewById(R.id.lblListItem); Switch abc= (Switch) convertView.findViewById(R.id.btn_check); final View finalConvertView = convertView; convertView.setBackgroundColor(Color.TRANSPARENT); abc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { CharSequence text = "Device ON"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(_context, text, duration); toast.show(); finalConvertView.setBackgroundColor(Color.argb(255, 120, 230, 230)); } else { finalConvertView.setBackgroundColor(Color.TRANSPARENT); } } }); Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ 14 txtListChild.setText(childText); return convertView; } Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ 15 CH ƠNG - KẾT QU - T o ch ng trình sử d ng Smartphone Android Điều khiển thi t bị nhƠ App nhiều ch c ch a th c hi n đ c: Sever vƠ file JSON bị lỗi ch a ch y đ c TÀI LI U THAM KH O http://www.androidhive.info/ http://stackoverflow.com/questions/23084389/how-to-detect-device-statusoffline-online-in-android http://developer.android.com/ http://www.tutorialspoint.com/android/android_sending_sms.htm Đ ÁN 11520464 ĐINH THANH TÙNG ... THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ CH ƠNG - CHI TIẾT TH C HI N 2.1 Giao diện chương trình Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ - Để... TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ 14 txtListChild.setText(childText); return convertView; } Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ 15... list_item.xml cho danh m c Điều nƠy s ch a y u t TextView đ n gi n Đ ÁN 11520464 ĐINH THANH TÙNG XÂY D NG ỨNG DỤNG ĐIỀU KHIỂN THIẾT BỊ TRONG NHÀ - Để kiểm tra thi t bị on/off Đầu tiên ta ph i

Ngày đăng: 04/08/2022, 16:20

Xem thêm:

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w