編輯:關於android開發
socket 連接采用流的方式進行發送接收數據,采用thread線程的方式。
什麼是線程?
詳細代碼介紹:
博文來源:http://blog.csdn.net/zhaocundang 如果你對代碼有些疑問,請在下面留言來探討,或者加我的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成功連接";//消息換行
// Toast.makeText(getActivity(),"wifi連接成功",Toast.LENGTH_SHORT).show();
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);
}
}
catch (Exception e)
{
recvMessageClient = "接收異常:" + e.getMessage() + "\n";//消息換行
Message msg = new Message();
msg.what = 0;
mHandler.sendMessage(msg);
}
}
}
//handler用於線程中更新界面數據
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(); //接收消息的
String [] results = recvMessageClient.split("\\.");;
// Toast.makeText(getActivity(),results[3],Toast.LENGTH_SHORT).show(); //接收消息的
/*
Intent intent=new Intent();
intent.putExtra("temp",results[0]);
intent.putExtra("humi",results[2]);
intent.setAction("zcd.netanything.temp");
getActivity().sendBroadcast(intent);
*/
}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.print("t");
mPrintWriterClient.flush();//清空
Toast.makeText(getActivity(),"前進",Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
mPrintWriterClient.print("b");
// mPrintWriterClient.print("t");
mPrintWriterClient.flush();
Toast.makeText(getActivity(),"後退",Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
mPrintWriterClient.print("l");
// mPrintWriterClient.print("t");
mPrintWriterClient.flush();
Toast.makeText(getActivity(),"向左",Toast.LENGTH_SHORT).show();
break;
case R.id.button4:
mPrintWriterClient.print("r");
// mPrintWriterClient.print("t");
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;
}
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;
}
}
項目演示:
APP控制小車視頻演示
APP物聯系統視頻演示
Android 手機衛士--打包生成apk維護到服務器,androidapk項目打包生成apk過程: 1、生成簽名文件,並且指定所在位置 2、使用生成的簽名文件,給工程打
(轉)android屏幕適配,android屏幕適配本文轉自:http://isux.tencent.com/learn-android-from-zero-sessio
[android] 切換界面的通用處理,android界面實現不改變activity,只切換View 抽取View界面的基類 利用面向對象多態的思路,實現通用  
RecyclerView 結合 CardView 使用,recyclerview使用准備工作:導入 1.activity_mian.xml <android.sup