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 3 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

Thông tin cơ bản

Định dạng
Số trang 15
Dung lượng 394,78 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

CH ƠNG 1 – 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 &

Trang 1

-

Đ Á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

Trang 2

MỤC LỤC

CH NG 1 – T NG QUAN NG D NG 3

CH NG 2 - CHI TI T TH C HI N 3

2.1 Giao di n ch ng trình 4

2.2 Code ch c năng c a các thƠnh phần 7

CH NG 3 - K T QU 15

TÀI LI U THAM KH O 15

Trang 3

CH ƠNG 1 – 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

Trang 4

CH ƠNG 2 - CHI TIẾT TH C HI N

2.1 Giao diện chương trình

Trang 5

- Để t o một List View có thể m rộng, ta cần ba file layout xml Đầu tiên lƠ

cho listview chính, một trong 2 đ i với m c xem danh sách nhóm vƠ lƠ một

trong 3 lƠ cho xem danh sách m c con M activity_main.xml vƠ thêm phần tử

ExpandableListView

- T o một layout xml cho tiêu đề nhóm xem danh sách T o một tập tin xml có

tên list_group.xml

- T o một tập tin xml nhiều tên list_item.xml cho danh m c con Điều nƠy s

ch a các y u t TextView đ n gi n

Trang 6

- Để kiểm tra thi t bị đang 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 về tình tr ng c a thi t bị đư đ c l u trên 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 trong file AndroidManifest.xml

-

Trang 7

2.2 Code chức năng của các thành phần

a) Expandable List View:

Ta sử d ng một lớp adapter tùy chỉnh để t o ra danh sách xem T o một tập tin lớp mới gọi lƠ ExpandableListAdapter.java vƠ m rộng nƠy từ

BaseExpandableListAdapter Lớp nƠy cung cấp các 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 con trong danh sách

package com.example.mypc.firstapps;

import java.util.HashMap;

import java.util.List;

import android.content.Context;

import android.graphics.Typeface;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseExpandableListAdapter;

import android.widget.Switch;

import android.widget.TextView;

import android.widget.*;

import android.graphics.*;

public class ExpandableListAdapter extends

BaseExpandableListAdapter {

private Context _context;

private List<String> _listDataHeader; // header titles

// child data in format of header title, child title

private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter(Context context, List<String> listDataHeader,

HashMap<String, List<String>> 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

Trang 8

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);

Trang 9

}

@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

Trang 10

// Setting Dialog Message

// Setting alert dialog icon

R.drawable.off);

// Setting OK Button

DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int

which) {

}

});

// Showing Alert Message

}

ExpandableListAdapter listAdapter;

ExpandableListView expListView;

List<String> listDataHeader;

HashMap<String, List<String>> listDataChild;

/*

* Preparing the list data

*/

private void prepareListData() {

listDataHeader = new ArrayList<String>();

listDataChild = new HashMap<String, List<String>>();

// Adding child data

listDataHeader.add("Bed Room");

listDataHeader.add("Kitchen");

// Adding child data

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<String> Bedroom = new ArrayList<String>();

Bedroom.add("Tivi");

Bedroom.add("Air Condition");

Bedroom.add("Fan");

Bedroom.add("Fan sub");

Bedroom.add("Light For Sleep");

Bedroom.add("Light Center");

List<String> Kitchen = new ArrayList<String>();

Kitchen.add("Tivi");

Kitchen.add("Fan");

Trang 11

Kitchen.add("Light");

Kitchen.add("Air Condition");

Kitchen.add("Refrigerator");

listDataChild.put(listDataHeader.get(0), livingroom);

// Header, Child data

listDataChild.put(listDataHeader.get(2), Kitchen);

}

}

b) Detect Internet Connection Status :

T o một tập tin Class mới 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 ra một nút

<?xml version= "1.0" encoding= "utf-8"?>

<RelativeLayout

xmlns: android = "http://schemas.android.com/apk/res/android"

android :layout_width= "fill_parent"

android :layout_height= "fill_parent"

android :orientation= "vertical" >

<TextView

android :layout_width= "fill_parent"

android :layout_height= "wrap_content"

Trang 12

android :text= "Detect Device Status"

android :id= "@+id/textView" />

<Button android :id= "@+id/btn_check"

android :layout_height= "wrap_content"

android :layout_width= "wrap_content"

android :text= "Check Device Status"

android :layout_below= "@+id/textView"

android :layout_alignParentLeft= "true"

android :layout_alignParentStart= "true" />

Sau đó m MainActivity Trong đo n ta cho m một hộp tho i thông báo tình

tr ng k t n i

public class MainActivity extends Activity {

Boolean isInternetPresent = false;

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

// get the listview

findViewById(R.id.lvExp);

// preparing list data

listAdapter = new ExpandableListAdapter(this,

listDataHeader, listDataChild);

// setting list adapter

/**

* Check Internet status button click event

* */

{

@Override

public void onClick(View v) {

// get Internet status

cd.isConnectingToInternet();

// check for Internet status

// Internet Connection is Present

Trang 13

// make HTTP requests

Status",

"You have device connection",

true);

} else {

// Internet connection is not present

// Ask user to connect to Internet

Status",

"No active devices", false);

}

}

});

c) On/Off and Toast

Để dùng l nh bật tắt các thi t bị trong các phòng ta dùng một switch Khi thi t

bị bật lên sau khi 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

<Switch android :id= "@+id/btn_check"

android :focusable= "false"

android :layout_width= "wrap_content"

android :layout_height= "wrap_content"

android :layout_gravity= "center_vertical|right"

android :layout_marginRight= "60dp" />

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);

}

}

});

Trang 14

txtListChild.setText(childText);

return convertView;

}

Trang 15

CH ƠNG 3 - KẾT QU

- T o một ch ng trình sử d ng trên Smartphone Android Điều khiển thi t bị trong nhƠ

- App còn nhiều ch c năng ch a th c hi n đ c: Sever vƠ file JSON vẫn còn bị lỗi ch a ch y đ c

T ÀI LI U THAM KH O

http://stackoverflow.com/questions/23084389/how-to-detect-device-status-offline-online-in-android

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

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

TÀI LIỆU LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w