2.手机端:
package cn.netjava; import java.io.DataInputStream; import Android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.wifi.WifiManager; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.TextView; public class FunctionViewActivity extends Activity{ //定义各个stream private java.io.InputStream ins; private java.io.OutputStream ous; private java.io.DataInputStream dins; //定义各个组件 private ImageView img; private TextView tvw; //定义一个Bitmap 用来存ImageView的每个图 private Bitmap bmm; //放接收到数据的数组 private byte[] data; //初始化 public void init(String ip){ try{ System.out.println(ip); java.net.Socket soc=new java.net.Socket(ip,9090); ins=soc.getInputStream(); dins=new DataInputStream(ins); ous=soc.getOutputStream(); System.out.println("创建成功!"); }catch(Exception ef){ ef.printStackTrace(); } } //内部类,myHandler class MyHandler extends Handler{ public MyHandler(){ } public MyHandler(Looper looper){ super(looper); } public void handleMessage(Message msg){ img.setImageBitmap((Bitmap)msg.obj); } } //onCreate public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainfunctionactivity); //得到各个组件 img=(ImageView)findViewById(R.id.imgView); tvw=(TextView)findViewById(R.id.exitTextView); //textView 添加监听器 tvw.setOnClickListener(new OnClickListener(){ public void onClick(View v) { System.exit(0); } }); init(this.getIntent().getStringExtra("ipaddress")); Looper looper = Looper.myLooper(); //此处甚至可以不需要设置Looper,因为 Handler默认就使用当前线程的Looper final MyHandler myhandler= new MyHandler(looper); new Thread() { public void run() { while(true){ try{ data=new byte[dins.readInt()-5]; dins.readByte(); //注意,这里要用readfully dins.readFully(data); //注意,这里要回收bmm ,否则报错 if (bmm!=null){ bmm.recycle(); } bmm=BitmapFactory.decodeByteArray(data, 0, data.length); System.out.println("decode after:"+System.currentTimeMillis()); //每一百ms 执行 (可调) // Thread.sleep(100); //创建一个Message对象,并把得到的天气信息赋值给Message对象 Message message = Message.obtain(); message.obj = (Bitmap)bmm; myhandler.sendMessage(message); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); } }Android手机监控软件设计实现【源码+附图】(3)
内容版权声明:除非注明,否则皆为本站原创文章。