版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Good is good, but better carries it.精益求精,善益求善。PC客戶端與Android服務(wù)端的Socket同步通信-PC客戶端與Android服務(wù)端的Socket同步通信(USB)收藏需求:1.一個(gè)android端的service后臺(tái)運(yùn)行的程序,作為socket的服務(wù)器端;用于接收Pcclient端發(fā)來(lái)的命令,來(lái)處理數(shù)據(jù)后,把結(jié)果發(fā)給PCclient2.PC端程序,作為socket的客戶端,用于給android手機(jī)端發(fā)操作命令難點(diǎn)分析:1.手機(jī)一定要有adb模式,即插上USB線時(shí)馬上提示的對(duì)話框選adb。好多對(duì)手機(jī)的操作都可以用adb直接作。不過(guò),我發(fā)現(xiàn)LGGW
2、880就沒(méi)有,要去下載個(gè)2.android默認(rèn)手機(jī)端的IP為“”3.要想聯(lián)通PC與android手機(jī)的sokcet,一定要用adbforward來(lái)作下端口轉(zhuǎn)發(fā)才能連上socket.viewplaincopytoclipboardprint?Runtime.getRuntime().exec(adbforwardtcp:12580tcp:10086);Thread.sleep(3000);Runtime.getRuntime().exec(adbforwardtcp:12580tcp:10086);Thread.sleep(3000);4.android端的service程序Install到手機(jī)
3、上容易,但是還要有方法來(lái)從PC的client端來(lái)啟動(dòng)手機(jī)上的service,這個(gè)辦法可以通過(guò)PC端adb命令來(lái)發(fā)一個(gè)Broastcast,手機(jī)端再寫(xiě)個(gè)接收BroastcastReceive來(lái)接收這個(gè)Broastcast,在這個(gè)BroastcastReceive來(lái)啟動(dòng)servicepc端命令:viewplaincopytoclipboardprint?Runtime.getRuntime().exec(adbshellambroadcast-aNotifyServiceStart);Runtime.getRuntime().exec(adbshellambroadcast-aNotifyServ
4、iceStart);android端的代碼:ServiceBroadcastReceiver.javaviewplaincopytoclipboardprint?packagecom.otheri.service;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.util.Log;publicclassServiceBroadcastReceiverextendsBroadcastReceiverprivatestati
5、cStringSTART_ACTION=NotifyServiceStart;privatestaticStringSTOP_ACTION=NotifyServiceStop;OverridepublicvoidonReceive(Contextcontext,Intentintent)Log.d(androidService.TAG,Thread.currentThread().getName()+-+ServiceBroadcastReceiveronReceive);Stringaction=intent.getAction();if(START_ACTION.equalsIgnoreC
6、ase(action)context.startService(newIntent(context,androidService.class);Log.d(androidService.TAG,Thread.currentThread().getName()+-+ServiceBroadcastReceiveronReceivestartend);elseif(STOP_ACTION.equalsIgnoreCase(action)context.stopService(newIntent(context,androidService.class);Log.d(androidService.T
7、AG,Thread.currentThread().getName()+-+ServiceBroadcastReceiveronReceivestopend);packagecom.otheri.service;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.util.Log;publicclassServiceBroadcastReceiverextendsBroadcastReceiverprivatestatic
8、StringSTART_ACTION=NotifyServiceStart;privatestaticStringSTOP_ACTION=NotifyServiceStop;OverridepublicvoidonReceive(Contextcontext,Intentintent)Log.d(androidService.TAG,Thread.currentThread().getName()+-+ServiceBroadcastReceiveronReceive);Stringaction=intent.getAction();if(START_ACTION.equalsIgnoreCa
9、se(action)context.startService(newIntent(context,androidService.class);Log.d(androidService.TAG,Thread.currentThread().getName()+-+ServiceBroadcastReceiveronReceivestartend);elseif(STOP_ACTION.equalsIgnoreCase(action)context.stopService(newIntent(context,androidService.class);Log.d(androidService.TA
10、G,Thread.currentThread().getName()+-+ServiceBroadcastReceiveronReceivestopend);5.由于是USB連接,所以socket就可以設(shè)計(jì)為一但連接就一直聯(lián)通,即在newsocket和開(kāi)完out,in流后,就用個(gè)while(true)來(lái)循環(huán)PC端和android端的讀和寫(xiě)android的代碼:viewplaincopytoclipboardprint?publicvoidrun()Log.d(androidService.TAG,Thread.currentThread().getName()+-+aclienthasconn
11、ectedtoserver!);BufferedOutputStreamout;BufferedInputStreamin;try/*PC端發(fā)來(lái)的數(shù)據(jù)msg*/StringcurrCMD=;out=newBufferedOutputStream(client.getOutputStream();in=newBufferedInputStream(client.getInputStream();/testSocket();/測(cè)試socket方法androidService.ioThreadFlag=true;while(androidService.ioThreadFlag)tryif(!cli
12、ent.isConnected()break;/*接收PC發(fā)來(lái)的數(shù)據(jù)*/Log.v(androidService.TAG,Thread.currentThread().getName()+-+willread.);/*讀操作命令*/currCMD=readCMDFromSocket(in);Log.v(androidService.TAG,Thread.currentThread().getName()+-+*currCMD=+currCMD);/*根據(jù)命令分別處理數(shù)據(jù)*/if(currCMD.equals(1)out.write(OK.getBytes();out.flush();elsei
13、f(currCMD.equals(2)out.write(OK.getBytes();out.flush();elseif(currCMD.equals(3)out.write(OK.getBytes();out.flush();elseif(currCMD.equals(4)/*準(zhǔn)備接收文件數(shù)據(jù)*/tryout.write(servicereceiveOK.getBytes();out.flush();catch(IOExceptione)e.printStackTrace();/*接收文件數(shù)據(jù),4字節(jié)文件長(zhǎng)度,4字節(jié)文件格式,其后是文件數(shù)據(jù)*/bytefilelength=newbyte4
14、;bytefileformat=newbyte4;bytefilebytes=null;/*從socket流中讀取完整文件數(shù)據(jù)*/filebytes=receiveFileFromSocket(in,out,filelength,fileformat);/Log.v(Service139.TAG,receivedata=+new/String(filebytes);try/*生成文件*/Filefile=FileHelper.newFile(R0013340.JPG);FileHelper.writeFile(file,filebytes,0,filebytes.length);catch(I
15、OExceptione)e.printStackTrace();elseif(currCMD.equals(exit)catch(Exceptione)/try/out.write(error.getBytes(utf-8);/out.flush();/catch(IOExceptione1)/e1.printStackTrace();/Log.e(androidService.TAG,Thread.currentThread().getName()+-+readwriteerror111111);out.close();in.close();catch(Exceptione)Log.e(an
16、droidService.TAG,Thread.currentThread().getName()+-+readwriteerror222222);e.printStackTrace();finallytryif(client!=null)Log.v(androidService.TAG,Thread.currentThread().getName()+-+client.close();client.close();catch(IOExceptione)Log.e(androidService.TAG,Thread.currentThread().getName()+-+readwriteer
17、ror333333);e.printStackTrace();publicvoidrun()Log.d(androidService.TAG,Thread.currentThread().getName()+-+aclienthasconnectedtoserver!);BufferedOutputStreamout;BufferedInputStreamin;try/*PC端發(fā)來(lái)的數(shù)據(jù)msg*/StringcurrCMD=;out=newBufferedOutputStream(client.getOutputStream();in=newBufferedInputStream(client
18、.getInputStream();/testSocket();/測(cè)試socket方法androidService.ioThreadFlag=true;while(androidService.ioThreadFlag)tryif(!client.isConnected()break;/*接收PC發(fā)來(lái)的數(shù)據(jù)*/Log.v(androidService.TAG,Thread.currentThread().getName()+-+willread.);/*讀操作命令*/currCMD=readCMDFromSocket(in);Log.v(androidService.TAG,Thread.cu
19、rrentThread().getName()+-+*currCMD=+currCMD);/*根據(jù)命令分別處理數(shù)據(jù)*/if(currCMD.equals(1)out.write(OK.getBytes();out.flush();elseif(currCMD.equals(2)out.write(OK.getBytes();out.flush();elseif(currCMD.equals(3)out.write(OK.getBytes();out.flush();elseif(currCMD.equals(4)/*準(zhǔn)備接收文件數(shù)據(jù)*/tryout.write(servicereceiveOK
20、.getBytes();out.flush();catch(IOExceptione)e.printStackTrace();/*接收文件數(shù)據(jù),4字節(jié)文件長(zhǎng)度,4字節(jié)文件格式,其后是文件數(shù)據(jù)*/bytefilelength=newbyte4;bytefileformat=newbyte4;bytefilebytes=null;/*從socket流中讀取完整文件數(shù)據(jù)*/filebytes=receiveFileFromSocket(in,out,filelength,fileformat);/Log.v(Service139.TAG,receivedata=+new/String(filebyt
21、es);try/*生成文件*/Filefile=FileHelper.newFile(R0013340.JPG);FileHelper.writeFile(file,filebytes,0,filebytes.length);catch(IOExceptione)e.printStackTrace();elseif(currCMD.equals(exit)catch(Exceptione)/try/out.write(error.getBytes(utf-8);/out.flush();/catch(IOExceptione1)/e1.printStackTrace();/Log.e(andr
22、oidService.TAG,Thread.currentThread().getName()+-+readwriteerror111111);out.close();in.close();catch(Exceptione)Log.e(androidService.TAG,Thread.currentThread().getName()+-+readwriteerror222222);e.printStackTrace();finallytryif(client!=null)Log.v(androidService.TAG,Thread.currentThread().getName()+-+
23、client.close();client.close();catch(IOExceptione)Log.e(androidService.TAG,Thread.currentThread().getName()+-+readwriteerror333333);e.printStackTrace();6.如果是在PC端和android端的讀寫(xiě)操作來(lái)while(true)循環(huán),這樣socket流的結(jié)尾不好判斷,不能用“-1”來(lái)判斷,因?yàn)椤?1”是只有在socket關(guān)閉時(shí)才作為判斷結(jié)尾。7.socket在out.write(bytes);時(shí),要是數(shù)據(jù)太大時(shí),超過(guò)socket的緩存,socket自動(dòng)
24、分包發(fā)送,所以對(duì)方就一定要用循環(huán)來(lái)多次讀。最好的辦法就是服務(wù)器和客戶端協(xié)議好,比如發(fā)文件時(shí),先寫(xiě)過(guò)來(lái)一個(gè)要發(fā)送的文件的大小,然后再發(fā)送文件;對(duì)方用這個(gè)大小,來(lái)循環(huán)讀取數(shù)據(jù)。android端接收數(shù)據(jù)的代碼:viewplaincopytoclipboardprint?/*功能:從socket流中讀取完整文件數(shù)據(jù)*InputStreamin:socket輸入流*bytefilelength:流的前4個(gè)字節(jié)存儲(chǔ)要轉(zhuǎn)送的文件的字節(jié)數(shù)*bytefileformat:流的前5-8字節(jié)存儲(chǔ)要轉(zhuǎn)送的文件的格式(如.apk)*/publicstaticbytereceiveFileFromSocket(Input
25、Streamin,OutputStreamout,bytefilelength,bytefileformat)bytefilebytes=null;/文件數(shù)據(jù)tryintfilelen=MyUtil.bytesToInt(filelength);/文件長(zhǎng)度從4字節(jié)byte轉(zhuǎn)成IntStringstrtmp=readfilelengthok:+filelen;out.write(strtmp.getBytes(utf-8);out.flush();filebytes=newbytefilelen;intpos=0;intrcvLen=0;while(rcvLen=in.read(filebyte
26、s,pos,filelen-pos)0)pos+=rcvLen;Log.v(androidService.TAG,Thread.currentThread().getName()+-+readfileOK:filesize=+filebytes.length);out.write(readfileok.getBytes(utf-8);out.flush();catch(Exceptione)Log.v(androidService.TAG,Thread.currentThread().getName()+-+receiveFileFromSocketerror);e.printStackTra
27、ce();returnfilebytes;/*功能:從socket流中讀取完整文件數(shù)據(jù)*InputStreamin:socket輸入流*bytefilelength:流的前4個(gè)字節(jié)存儲(chǔ)要轉(zhuǎn)送的文件的字節(jié)數(shù)*bytefileformat:流的前5-8字節(jié)存儲(chǔ)要轉(zhuǎn)送的文件的格式(如.apk)*/publicstaticbytereceiveFileFromSocket(InputStreamin,OutputStreamout,bytefilelength,bytefileformat)bytefilebytes=null;/文件數(shù)據(jù)tryintfilelen=MyUtil.bytesToInt(
28、filelength);/文件長(zhǎng)度從4字節(jié)byte轉(zhuǎn)成IntStringstrtmp=readfilelengthok:+filelen;out.write(strtmp.getBytes(utf-8);out.flush();filebytes=newbytefilelen;intpos=0;intrcvLen=0;while(rcvLen=in.read(filebytes,pos,filelen-pos)0)pos+=rcvLen;Log.v(androidService.TAG,Thread.currentThread().getName()+-+readfileOK:filesize
29、=+filebytes.length);out.write(readfileok.getBytes(utf-8);out.flush();catch(Exceptione)Log.v(androidService.TAG,Thread.currentThread().getName()+-+receiveFileFromSocketerror);e.printStackTrace();returnfilebytes;8.socket的最重要的機(jī)制就是讀寫(xiě)采用的是阻塞的方式,如果客戶端作為命令發(fā)起者,服務(wù)器端作為接收者的話,只有當(dāng)客戶端client用out.writer()寫(xiě)到輸出流里后,即流中
30、有數(shù)據(jù)service的read才會(huì)執(zhí)行,不然就會(huì)一直停在read()那里等數(shù)據(jù)。9.還要讓服務(wù)器端可以同時(shí)連接多個(gè)client,即服務(wù)器端用newthread()來(lái)作數(shù)據(jù)讀取操作。源碼:客戶端(pc端):testPcClient.javaviewplaincopytoclipboardprint?importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.BufferedReader;importjava.io.ByteArrayOutputStream;importjava.io.IOE
31、xception;importjava.io.InputStream;importjava.io.InputStreamReader;.InetAddress;.Socket;.UnknownHostException;publicclasstestPcClient/*paramargs*throwsInterruptedException*/publicstaticvoidmain(Stringargs)throwsInterruptedExceptiontryRuntime.getRuntime().exec(adbshellambroadcast-aNotifyServiceStop);
32、Thread.sleep(3000);Runtime.getRuntime().exec(adbforwardtcp:12580tcp:10086);Thread.sleep(3000);Runtime.getRuntime().exec(adbshellambroadcast-aNotifyServiceStart);Thread.sleep(3000);catch(IOExceptione3)e3.printStackTrace();Socketsocket=null;tryInetAddressserverAddr=null;serverAddr=InetAddress.getByNam
33、e();System.out.println(TCP1111+C:Connecting.);socket=newSocket(serverAddr,12580);Stringstr=hi,wufenglong;System.out.println(TCP221122+C:RECEIVE);BufferedOutputStreamout=newBufferedOutputStream(socket.getOutputStream();BufferedInputStreamin=newBufferedInputStream(socket.getInputStream();BufferedReade
34、rbr=newBufferedReader(newInputStreamReader(System.in);booleanflag=true;while(flag)System.out.print(請(qǐng)輸入16的數(shù)字,退出輸入exit:);StringstrWord=br.readLine();/從控制臺(tái)輸入16if(strWord.equals(1)out.write(1.getBytes();out.flush();System.out.println(1finishsendingthedata);StringstrFormsocket=readFromSocket(in);System.o
35、ut.println(thedatasentbyserveris:rn+strFormsocket);System.out.println(=);elseif(strWord.equals(2)out.write(2.getBytes();out.flush();System.out.println(2finishsendingthedata);StringstrFormsocket=readFromSocket(in);System.out.println(thedatasentbyserveris:rn+strFormsocket);System.out.println(=);elseif
36、(strWord.equals(3)out.write(3.getBytes();out.flush();System.out.println(3finishsendingthedata);StringstrFormsocket=readFromSocket(in);System.out.println(thedatasentbyserveris:rn+strFormsocket);System.out.println(=);elseif(strWord.equals(4)/*發(fā)送命令*/out.write(4.getBytes();out.flush();System.out.println
37、(sendfilefinishsendingtheCMD:);/*服務(wù)器反饋:準(zhǔn)備接收*/StringstrFormsocket=readFromSocket(in);System.out.println(servicereadyreceicedata:UPDATE_CONTACTS:+strFormsocket);bytefilebytes=FileHelper.readFile(R0013340.JPG);System.out.println(filesize=+filebytes.length);/*將整數(shù)轉(zhuǎn)成4字節(jié)byte數(shù)組*/bytefilelength=newbyte4;file
38、length=ToByte(filebytes.length);/*將.apk字符串轉(zhuǎn)成4字節(jié)byte數(shù)組*/bytefileformat=null;fileformat=.apk.getBytes();System.out.println(fileformatlength=+fileformat.length);/*字節(jié)流中前4字節(jié)為文件長(zhǎng)度,4字節(jié)文件格式,以后是文件流*/*注意如果write里的byte超過(guò)socket的緩存,系統(tǒng)自動(dòng)分包寫(xiě)過(guò)去,所以對(duì)方要循環(huán)寫(xiě)完*/out.write(filelength);out.flush();Stringstrok1=readFromSocket
39、(in);System.out.println(servicereceivefilelength:+strok1);/out.write(fileformat);/out.flush();/Stringstrok2=readFromSocket(in);/System.out.println(servicereceivefileformat:+/strok2);System.out.println(writedatatoandroid);out.write(filebytes);out.flush();System.out.println(*);/*服務(wù)器反饋:接收成功*/Stringstrr
40、ead=readFromSocket(in);System.out.println(senddatasuccess:+strread);System.out.println(=);elseif(strWord.equalsIgnoreCase(EXIT)out.write(EXIT.getBytes();out.flush();System.out.println(EXITfinishsendingthedata);StringstrFormsocket=readFromSocket(in);System.out.println(thedatasentbyserveris:rn+strForm
41、socket);flag=false;System.out.println(=);catch(UnknownHostExceptione1)System.out.println(TCP331133+ERROR:+e1.toString();catch(Exceptione2)System.out.println(TCP441144+ERROR:+e2.toString();finallytryif(socket!=null)socket.close();System.out.println(socket.close();catch(IOExceptione)System.out.println
42、(TCP5555+ERROR:+e.toString();/*從InputStream流中讀數(shù)據(jù)*/publicstaticStringreadFromSocket(InputStreamin)intMAX_BUFFER_BYTES=4000;Stringmsg=;bytetempbuffer=newbyteMAX_BUFFER_BYTES;tryintnumReadedBytes=in.read(tempbuffer,0,tempbuffer.length);msg=newString(tempbuffer,0,numReadedBytes,utf-8);tempbuffer=null;ca
43、tch(Exceptione)e.printStackTrace();/Log.v(Service139.TAG,msg=+msg);returnmsg;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.BufferedReader;importjava.io.ByteArrayOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;.Inet
44、Address;.Socket;.UnknownHostException;publicclasstestPcClient/*paramargs*throwsInterruptedException*/publicstaticvoidmain(Stringargs)throwsInterruptedExceptiontryRuntime.getRuntime().exec(adbshellambroadcast-aNotifyServiceStop);Thread.sleep(3000);Runtime.getRuntime().exec(adbforwardtcp:12580tcp:1008
45、6);Thread.sleep(3000);Runtime.getRuntime().exec(adbshellambroadcast-aNotifyServiceStart);Thread.sleep(3000);catch(IOExceptione3)e3.printStackTrace();Socketsocket=null;tryInetAddressserverAddr=null;serverAddr=InetAddress.getByName();System.out.println(TCP1111+C:Connecting.);socket=newSocket(serverAdd
46、r,12580);Stringstr=hi,wufenglong;System.out.println(TCP221122+C:RECEIVE);BufferedOutputStreamout=newBufferedOutputStream(socket.getOutputStream();BufferedInputStreamin=newBufferedInputStream(socket.getInputStream();BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in);booleanflag=true;w
47、hile(flag)System.out.print(請(qǐng)輸入16的數(shù)字,退出輸入exit:);StringstrWord=br.readLine();/從控制臺(tái)輸入16if(strWord.equals(1)out.write(1.getBytes();out.flush();System.out.println(1finishsendingthedata);StringstrFormsocket=readFromSocket(in);System.out.println(thedatasentbyserveris:rn+strFormsocket);System.out.println(=)
48、;elseif(strWord.equals(2)out.write(2.getBytes();out.flush();System.out.println(2finishsendingthedata);StringstrFormsocket=readFromSocket(in);System.out.println(thedatasentbyserveris:rn+strFormsocket);System.out.println(=);elseif(strWord.equals(3)out.write(3.getBytes();out.flush();System.out.println(
49、3finishsendingthedata);StringstrFormsocket=readFromSocket(in);System.out.println(thedatasentbyserveris:rn+strFormsocket);System.out.println(=);elseif(strWord.equals(4)/*發(fā)送命令*/out.write(4.getBytes();out.flush();System.out.println(sendfilefinishsendingtheCMD:);/*服務(wù)器反饋:準(zhǔn)備接收*/StringstrFormsocket=readFro
50、mSocket(in);System.out.println(servicereadyreceicedata:UPDATE_CONTACTS:+strFormsocket);bytefilebytes=FileHelper.readFile(R0013340.JPG);System.out.println(filesize=+filebytes.length);/*將整數(shù)轉(zhuǎn)成4字節(jié)byte數(shù)組*/bytefilelength=newbyte4;filelength=ToByte(filebytes.length);/*將.apk字符串轉(zhuǎn)成4字節(jié)byte數(shù)組*/bytefileformat=nu
51、ll;fileformat=.apk.getBytes();System.out.println(fileformatlength=+fileformat.length);/*字節(jié)流中前4字節(jié)為文件長(zhǎng)度,4字節(jié)文件格式,以后是文件流*/*注意如果write里的byte超過(guò)socket的緩存,系統(tǒng)自動(dòng)分包寫(xiě)過(guò)去,所以對(duì)方要循環(huán)寫(xiě)完*/out.write(filelength);out.flush();Stringstrok1=readFromSocket(in);System.out.println(servicereceivefilelength:+strok1);/out.write(fil
52、eformat);/out.flush();/Stringstrok2=readFromSocket(in);/System.out.println(servicereceivefileformat:+/strok2);System.out.println(writedatatoandroid);out.write(filebytes);out.flush();System.out.println(*);/*服務(wù)器反饋:接收成功*/Stringstrread=readFromSocket(in);System.out.println(senddatasuccess:+strread);Syst
53、em.out.println(=);elseif(strWord.equalsIgnoreCase(EXIT)out.write(EXIT.getBytes();out.flush();System.out.println(EXITfinishsendingthedata);StringstrFormsocket=readFromSocket(in);System.out.println(thedatasentbyserveris:rn+strFormsocket);flag=false;System.out.println(=);catch(UnknownHostExceptione1)Sy
54、stem.out.println(TCP331133+ERROR:+e1.toString();catch(Exceptione2)System.out.println(TCP441144+ERROR:+e2.toString();finallytryif(socket!=null)socket.close();System.out.println(socket.close();catch(IOExceptione)System.out.println(TCP5555+ERROR:+e.toString();/*從InputStream流中讀數(shù)據(jù)*/publicstaticStringread
55、FromSocket(InputStreamin)intMAX_BUFFER_BYTES=4000;Stringmsg=;bytetempbuffer=newbyteMAX_BUFFER_BYTES;tryintnumReadedBytes=in.read(tempbuffer,0,tempbuffer.length);msg=newString(tempbuffer,0,numReadedBytes,utf-8);tempbuffer=null;catch(Exceptione)e.printStackTrace();/Log.v(Service139.TAG,msg=+msg);retur
56、nmsg;android服務(wù)器端:主類androidService.javaviewplaincopytoclipboardprint?packagecom.otheri.service;importjava.io.File;importjava.io.IOException;.ServerSocket;.Socket;importandroid.app.Service;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.
57、content.IntentFilter;importandroid.os.IBinder;importandroid.util.Log;/*設(shè)置:android手機(jī)*/publicclassandroidServiceextendsServicepublicstaticfinalStringTAG=TAG;publicstaticBooleanmainThreadFlag=true;publicstaticBooleanioThreadFlag=true;ServerSocketserverSocket=null;finalintSERVER_PORT=10086;FiletestFile;
58、privatesysBroadcastReceiversysBR;OverridepublicvoidonCreate()super.onCreate();Log.v(TAG,Thread.currentThread().getName()+-+onCreate);/*創(chuàng)建內(nèi)部類sysBroadcastReceiver并注冊(cè)registerReceiver*/sysRegisterReceiver();newThread()publicvoidrun()doListen();.start();privatevoiddoListen()Log.d(TAG,Thread.currentThread
59、().getName()+-+doListen()START);serverSocket=null;tryLog.d(TAG,Thread.currentThread().getName()+-+doListen()newserverSocket);serverSocket=newServerSocket(SERVER_PORT);booleanmainThreadFlag=true;while(mainThreadFlag)Log.d(TAG,Thread.currentThread().getName()+-+doListen()listen);Socketclient=serverSoc
60、ket.accept();newThread(newThreadReadWriterIOSocket(this,client).start();catch(IOExceptione1)Log.v(androidService.TAG,Thread.currentThread().getName()+-+newserverSocketerror);e1.printStackTrace();/*創(chuàng)建內(nèi)部類sysBroadcastReceiver并注冊(cè)registerReceiver*/privatevoidsysRegisterReceiver()Log.v(TAG,Thread.currentT
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五版危險(xiǎn)品倉(cāng)儲(chǔ)安全協(xié)議合同3篇
- 2024聯(lián)合研發(fā)合同3篇帶眉腳
- 2025年度夾板加工及銷售一體化合同范本4篇
- 二零二五版銀行存款賬戶資金調(diào)撥與監(jiān)管合同3篇
- 2025年新型投影顯示屏銷售合同3篇
- 二零二五年度玩具OEM委托生產(chǎn)合同示范文本3篇
- 二零二五年度按摩養(yǎng)生館安全管理與應(yīng)急預(yù)案合同3篇
- 2024版學(xué)校食堂擴(kuò)建工程施工協(xié)議范本一
- 二零二五年度跨境電商物流服務(wù)合同模板6篇
- 二零二四年嬰幼兒奶粉市場(chǎng)調(diào)研與品牌定位合同3篇
- 2025年經(jīng)濟(jì)形勢(shì)會(huì)議講話報(bào)告
- 北師大版小學(xué)三年級(jí)上冊(cè)數(shù)學(xué)第五單元《周長(zhǎng)》測(cè)試卷(含答案)
- 國(guó)家安全責(zé)任制落實(shí)情況報(bào)告3篇
- 2024年度順豐快遞冷鏈物流服務(wù)合同3篇
- 六年級(jí)下冊(cè)【默寫(xiě)表】(牛津上海版、深圳版)(漢譯英)
- 合同簽訂培訓(xùn)
- 新修訂《保密法》知識(shí)考試題及答案
- 電工基礎(chǔ)知識(shí)培訓(xùn)課程
- 鐵路基礎(chǔ)知識(shí)題庫(kù)單選題100道及答案解析
- 金融AI:顛覆與重塑-深化理解AI在金融行業(yè)的實(shí)踐與挑戰(zhàn)
- 住宅樓安全性檢測(cè)鑒定方案
評(píng)論
0/150
提交評(píng)論