安卓开发(10)——智能家居APP设计

智能家居APP设计

一、设计思路

  • 第一个为欢迎页面
    • 主要实现功能是页面右上角3秒倒计时,完成后自动跳转到第二个页面。在主函数中创建用于计时的线程,每过一秒通过handler机制给主线程发送消息,主线程中通过handler机制更改右上角的计时显示,3秒结束后跳转到第二个页面。
  • 源码
    package com.example.jiangyo.learn;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.widget.TextView;
    
    public class WelcomActivity extends Activity {
    
    	Handler handler;
    	TextView tx;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_welcom);
    		
    		tx = (TextView) findViewById(R.id.tx);
    		handler = new Handler(){
    			@Override
    			public void handleMessage(Message msg) {
    				// TODO Auto-generated method stub
    				super.handleMessage(msg);
    				
    				tx.setText(msg.what+"s");
    			}
    		};
    		
    		new Thread(new Runnable() {
    			public void run() {
    				int i;
    				for(i=3;i>=0;i--) {
    					Message msg = new Message();
    					msg.what = i;
    					handler.sendMessage(msg);
    					try {
    						Thread.sleep(1000);
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
    				Intent intent = new Intent(WelcomActivity.this, MainActivity.class);
    				startActivity(intent);
    			}
    		}).start();
    	}
    }
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    	android:background="@drawable/welcome"
        tools:context=".WelcomActivity" >
    
        <TextView
            android:id="@+id/tx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3s" 
            android:textSize="20sp"
            android:background="#000000"
            android:layout_alignParentRight="true"
            android:layout_marginTop="25dp"
            android:textColor="#ffffff"
            />
    
    </RelativeLayout>
    
  • 第二个页面为数据处理并显示监控的画面。主线程中创建了4个线程,分别是:创建socket客户端连接服务端线程、创建客户端接收数据线程、创建更改温湿度线程、创建更改webview的线程。 其中更改温湿度和更改webview是用于处理Handler机制的线程,主要作用是接收服务器发送的温湿度、按键的开关监控数据,把这些数据通过Handler机制线程可更改页面的显示。除外还有按键响应处理函数,卧室灯、二楼灯、餐厅灯、浴室灯、开监控、关监控 6个按键分别被按下后,会创建线程给服务端发送数据。
  • 源码
    package com.example.jiangyo.learn;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.TextView;
    
    @SuppressLint("HandlerLeak")
    public class MainActivity extends Activity {
    	Handler handler;
    	Handler handler2;
    	TextView tx;
    	TextView tx2;
    	Socket client_c;
    	
    	WebView wb;
    	String mes = "massege frome Android";
    
    	public void Socket_Client(final int num)
    	{
    		new Thread(new Runnable() {
    			
    			public void run() {
    				// TODO Auto-generated method stub
    				try {
    					OutputStream out = client_c.getOutputStream();
    					
    					switch(num)
    					{
    						case 1:
    							mes = "livingroomLight";
    							break;
    						case 2:
    							mes = "upstairLight";
    							break;
    						case 3:
    							mes = "restaurantLight";
    							break;
    						case 4:
    							mes = "bathroomLight";
    							break;
    						case 5:
    							mes = "opencamera";
    							new Thread( new Runnable() {
    								
    								public void run() {
    									// TODO Auto-generated method stub
    									Message msg = new Message();
    									Bundle b = new Bundle();
    									b.putString("msg", mes);
    									msg.setData(b);
    									
    									handler2.sendMessage(msg);
    								}
    							}).start();
    							
    							break;
    						case 6:
    							mes = "closecamera";
    							new Thread( new Runnable() {
    								
    								public void run() {
    									// TODO Auto-generated method stub
    									Message msg = new Message();
    									Bundle b = new Bundle();
    									b.putString("msg", mes);
    									msg.setData(b);
    									
    									handler2.sendMessage(msg);
    								}
    							}).start();
    
    							break;
    						default :
    							break;
    					}
    					out.write(mes.getBytes());//发送通道发送数据
    				} catch (UnknownHostException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		}).start();
    	}
    	
    	//key时间处理函数
    	@SuppressLint("ShowToast")
    	public void ButtonBeClicked(View v)
    	{
    		switch(v.getId())
    		{
    			case R.id.button1:
    				Socket_Client(1);
    				break;
    			case R.id.button2:
    				Socket_Client(2);
    				break;
    			case R.id.button3:
    				Socket_Client(3);
    				break;
    			case R.id.button4:
    				Socket_Client(4);
    				break;
    			case R.id.button5:
    				Socket_Client(5);
    				break;
    			case R.id.button6:
    				Socket_Client(6);
    				break;
    			default :
    				break;
    		}
    	}
    
    	
    	
    	
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
            tx = (TextView) findViewById(R.id.tx1);
            tx2 = (TextView) findViewById(R.id.tx2);
            
            
            //创建socket客户端连接服务端
            new Thread(new Runnable() {
    			public void run() {
    				// TODO Auto-generated method stub
    				try {
    					//连接服务端
    					client_c = new Socket("192.168.101.76", 8080);
    				} catch (UnknownHostException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		}).start();
            
            //创建接收数据线程
            new Thread(new Runnable() {
    			public void run() {
    				// TODO Auto-generated method stub
    				try {
    					Thread.sleep(1000);
    					OutputStream out = client_c.getOutputStream();
    					String mes = "massege frome Android";
    					out.write(mes.getBytes());//发送通道发送数据
    					
    					while(true) {
    						int len;
    						InputStream in = client_c.getInputStream();
    						byte[] data = new byte[128];
    						
    						len = in.read(data);
    						String str = new String(data,0,len);
    						Message msg = new Message();
    						
    						Bundle b = new Bundle();
    						b.putString("msg", str);
    						msg.setData(b);
    						
    						handler.sendMessage(msg);
    					}
    
    				} catch (UnknownHostException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		}).start();
    
            
            //创建更改温湿度线程
    		handler = new Handler(){
    			@Override
    			public void handleMessage(Message msg) {
    				// TODO Auto-generated method stub
    				super.handleMessage(msg);
    				
    				Bundle b = msg.getData();
    				String string  = b.getString("msg");
    				String str2 = string.substring(1,3);
    				String str1 = string.substring(0, 1);
    				if(str1.equals("T")) {
    					tx.setText(str2+"℃");
    				}
    				else {
    					tx2.setText(str2+"%");
    				}
    			}
    		};
    		
    		//创建更改webview的线程
    		handler2 = new Handler(){
    			@Override
    			public void handleMessage(Message msg) {
    				// TODO Auto-generated method stub
    				super.handleMessage(msg);
    				
    				Bundle b = msg.getData();
    				String string  = b.getString("msg");
    				if(string.equals("opencamera")) {
    					wb = (WebView) findViewById(R.id.webView1);
    			        wb.loadUrl("http://192.168.101.76:8081/");
    			        wb.setWebViewClient(new WebViewClient());
    				}
    				else {
    					wb = (WebView) findViewById(R.id.webView1);
    			        wb.loadUrl("http://www.linuxcool.com/");
    			        wb.setWebViewClient(new WebViewClient());
    				}
    			}
    		};
    	}
    }
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    	android:background="@drawable/bg"
        tools:context=".MainActivity" >
    
        <LinearLayout 
            android:id="@+id/LinearLayout1"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:orientation="horizontal"
            android:layout_marginTop="80dp"
            >
            
            <WebView
    	        android:id="@+id/webView1"
    	        android:layout_width="match_parent"
    	        android:layout_height="250dp"
            />
            
        </LinearLayout>
        
        <LinearLayout 
            android:id="@+id/LinearLayout2"
            android:layout_below="@id/LinearLayout1"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:gravity="center_horizontal"
            >
            
            <LinearLayout 
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="200dp"
                android:orientation="vertical"
                >
                <TextView 
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="温度:"
                    android:textSize="15sp"
                    android:textColor="#ffffff"
                    android:layout_weight="1"
                    android:layout_gravity="center_horizontal"
                    />
                
                 <TextView 
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="湿度:"
                    android:textSize="15sp"
                    android:textColor="#ffffff"
                    android:layout_weight="1"
                    android:layout_gravity="center_horizontal"
                    />
                 
                  <TextView 
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="火警:"
                    android:textSize="15sp"
                    android:textColor="#ffffff"
                    android:layout_weight="1"
                    android:layout_gravity="center_horizontal"
                    />
                
            </LinearLayout>
            
            <LinearLayout 
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="200dp"
                android:orientation="vertical"
                >
                
                 <TextView 
                    android:id="@+id/tx1"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="26℃"
                    android:textSize="15sp"
                    android:textColor="#ffffff"
                    android:layout_weight="1"
                    android:layout_gravity="center_horizontal"
                    />
                
                 <TextView 
                    android:id="@+id/tx2"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="78%"
                    android:textSize="15sp"
                    android:textColor="#ffffff"
                    android:layout_weight="1"
                    android:layout_gravity="center_horizontal"
                    />
                 
                  <TextView 
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="监视"
                    android:textSize="15sp"
                    android:textColor="#ffffff"
                    android:layout_weight="1"
                    android:layout_gravity="center_horizontal"
                    />
            </LinearLayout>
            
        </LinearLayout>
        
        
        <LinearLayout 
            android:id="@+id/LinearLayout3"
            android:layout_below="@id/LinearLayout2"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal"
            android:gravity="center_horizontal"
            >
            
    			<Button 
    			    android:id="@+id/button1"
    			    android:layout_weight="1"
    			    android:layout_width="0dp"
    			    android:layout_height="wrap_content"
    			    android:text="卧室灯"
    			    android:background="@drawable/btn_selector"
    			    android:onClick="ButtonBeClicked"
    			    />
    			<Button 
    			    android:id="@+id/button2"
    			    android:layout_weight="1"
    			    android:layout_width="0dp"
    			    android:layout_height="wrap_content"
    			    android:text="二楼灯"
    			    android:background="@drawable/btn_selector"
    			    android:onClick="ButtonBeClicked"
    			    />
    			<Button 
    			    android:id="@+id/button3"
    			    android:layout_weight="1"
    			    android:layout_width="0dp"
    			    android:layout_height="wrap_content"
    			    android:text="餐厅灯"
    			    android:background="@drawable/btn_selector"
    			    android:onClick="ButtonBeClicked"
    			    />
    			<Button 
    			  android:id="@+id/button4"  
    			  android:layout_weight="1"
    			  android:layout_width="0dp"
    			  android:layout_height="wrap_content"
    			  android:text="浴室灯"
    			  android:background="@drawable/btn_selector"
    			  android:onClick="ButtonBeClicked"
    			  />
        </LinearLayout>
        
        <LinearLayout 
            android:layout_below="@id/LinearLayout3"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal"
            android:gravity="center_horizontal"
            >
            
    			<Button 
    			    android:id="@+id/button5"
    			    android:layout_weight="1"
    			    android:layout_width="0dp"
    			    android:layout_height="wrap_content"
    			    android:text="开监控"
    			    android:background="@drawable/btn_selector"
    			    android:onClick="ButtonBeClicked"
    			    />
    			<Button 
    			    android:id="@+id/button6"
    			    android:layout_weight="1"
    			    android:layout_width="0dp"
    			    android:layout_height="wrap_content"
    			    android:text="关监控"
    			    android:background="@drawable/btn_selector"
    			    android:onClick="ButtonBeClicked"
    			    />
        </LinearLayout>
    
    </RelativeLayout>
    
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值