赞
踩
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>
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>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。