編輯:關於Android編程
廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:
package zcd.netanything; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import android.app.Fragment; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Toast; public class MyCar extends Fragment implements OnClickListener{ private static String wifiurl; private static String wificom; private static String wificamera; private Thread mThreadClient = null; private Socket mSocketClient = null; //視頻線程 private Thread mThreadvideo = null; private String recvMessageClient ; //MySurfaceView r; private boolean isConnect=false; //指令發出 數據緩存 static PrintWriter mPrintWriterClient = null; static BufferedReader mBufferedReaderClient = null; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.mycar,container, false); view.findViewById(R.id.button1).setOnClickListener(this); view.findViewById(R.id.button2).setOnClickListener(this); view.findViewById(R.id.button3).setOnClickListener(this); view.findViewById(R.id.button4).setOnClickListener(this); view.findViewById(R.id.button5).setOnClickListener(this); return view; } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } //內部類 廣播接收 public static class myReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub wifiurl = intent.getStringExtra("wifiurl"); wificom = intent.getStringExtra("wificom"); wificamera = intent.getStringExtra("wificamera"); Toast.makeText(context, "廣播已經接收", Toast.LENGTH_SHORT).show(); } } //來源 http://www.cnblogs.com/xiaobo-Linux/ 趙存檔 QQ463431476 //線程mRunnable啟動 private Runnable mRunnable = new Runnable() { public void run() { try { //連接服務器 mSocketClient = new Socket(wifiurl,Integer.parseInt(wificom)); //取得輸入、輸出流 mBufferedReaderClient = new BufferedReader(new InputStreamReader(mSocketClient.getInputStream())); mPrintWriterClient = new PrintWriter(mSocketClient.getOutputStream(), true); recvMessageClient = "wifi成功連接";//消息換行 Message msg = new Message(); msg.what = 1; mHandler.sendMessage(msg); } catch (Exception e) { recvMessageClient = "連接錯誤!";//消息換行 Message msg = new Message(); msg.what = 1; mHandler.sendMessage(msg); return; } setRecvMessage(); } }; public void setRecvMessage(){ char[] buffer = new char[256]; int count = 0; while (true) { try { if((count = mBufferedReaderClient.read(buffer))>0) { recvMessageClient = getInfoBuff(buffer, count);//消息換行 //Thread.sleep(500); Message msg = new Message(); msg.what = 1; mHandler.sendMessage(msg); //Toast.makeText(getActivity(),recvMessageClient,Toast.LENGTH_SHORT).show(); } } catch (Exception e) { recvMessageClient = "接收異常:" + e.getMessage() + "\n";//消息換行 Message msg = new Message(); msg.what = 0; mHandler.sendMessage(msg); } } } Handler mHandler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what == 0) { Toast.makeText(getActivity(),recvMessageClient,Toast.LENGTH_SHORT).show();//刷新消息機制 } else if(msg.what == 1) { try { String result = recvMessageClient; Toast.makeText(getActivity(),result,Toast.LENGTH_SHORT).show(); }catch(Exception e) { //Toast.makeText(getActivity(),"無法獲取數據,檢查網絡是否連接!",Toast.LENGTH_SHORT).show();//刷新消息機制 } } } }; //接收處理 private String getInfoBuff(char[] buff, int count) { char[] temp = new char[count]; for(int i=0; i<count; i++) { temp[i] = buff[i]; } return new String(temp); } /*public void onDestroy() { super.onDestroy(); if (isConnect) { isConnect = false; try { if(mSocketClient!=null) { mSocketClient.close(); mSocketClient = null; //mThreadvideo.destroy(); mPrintWriterClient.close(); mPrintWriterClient = null; recvMessageClient = "網絡端口成功";//消息換行 Message msg = new Message(); msg.what = 0; mHandler.sendMessage(msg); } } catch (IOException e) { e.printStackTrace(); } mThreadClient.interrupt(); } } */ public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.button1: mPrintWriterClient.print("f"); mPrintWriterClient.flush();//清空 Toast.makeText(getActivity(),"前進",Toast.LENGTH_SHORT).show(); break; case R.id.button2: mPrintWriterClient.print("b"); mPrintWriterClient.flush(); Toast.makeText(getActivity(),"後退",Toast.LENGTH_SHORT).show(); break; case R.id.button3: mPrintWriterClient.print("l"); mPrintWriterClient.flush(); Toast.makeText(getActivity(),"向左",Toast.LENGTH_SHORT).show(); break; case R.id.button4: mPrintWriterClient.print("r"); mPrintWriterClient.flush(); Toast.makeText(getActivity(),"向右",Toast.LENGTH_SHORT).show(); break; case R.id.button5: if(!isConnect) { //開啟mThreadClient線程 mThreadClient = new Thread(mRunnable); mThreadClient.start(); Toast.makeText(getActivity(),"嘗試連接網絡",500).show(); } else { onDestroy(); isConnect=false; // Btn_openwifi.setBackgroundResource(R.drawable.disconnect); } //開啟mThreadvideo線程 //mThreadvideo = new Thread(mRunvideo); //mThreadvideo.start();//開啟視頻監聽 break; //case R.id.button6: //關閉wifi /* if (isConnect) { isConnect = false; try { if(mSocketClient!=null) { mSocketClient.close(); mSocketClient = null; //mThreadvideo.destroy(); mPrintWriterClient.close(); mPrintWriterClient = null; recvMessageClient = "網絡端口成功";//消息換行 Message msg = new Message(); msg.what = 0; mHandler.sendMessage(msg); } } catch (IOException e) { e.printStackTrace(); } mThreadClient.interrupt(); } */ // break; } } }
以上所述是小編給大家介紹的Android Socket 線程連接openwrt與arduino單片機串口雙向通信,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!
這個圖片異步加載並緩存的類已經被很多開發者所使用,是最常用的幾個開源庫之一,主流的應用,隨便反編譯幾個火的項目,都可以見到它的身影。可是有的人並不知道如何去使用這庫如何進
web開發概述 靜態web資源:內容是靜態的,不同的人在不同的時間來訪問時都是相同的內容。HTML、CSS、JS動態web資源:內容是由程序生成的,
在Android的API中可以發現有很多用整數集來作為參數的地方,先來看一下實例。LinearLayout是大家所熟知的一個UI基本元素,它裡面有一個方向的屬性,可以通過
這一篇我們來學點新的東西。做項目的時候應該碰到這種問題:根據不同條件顯示或者隱藏一個控件或者布局,我們能想到的第一個方法就是 調用View.setVisibility()