The Android-socket function is implemented in the Service "This is the actual use of the situation"

Source: Internet
Author: User

A few days ago learned Android under Socket programming, as individuals are just beginning to learn the appropriate knowledge of Android. So deliberately to learn the code and process, written as a BLOG, such as: http://blog.csdn.net/91program/article/details/39177401
Learning Socket programming is purposeful and requires the communication between the phone and the PC to be completed. The content of the communication is to transfer the MP3 information from the phone to the PC side via the Socket.
After referring to the relevant socket on the Internet, the socket function is basically completed. So just continue to learn the implementation of the music player under Android. In the implementation of the music player process, it is found that the music player must have at least a playlist and is playing two Activity, so the problem comes:
(1). The Socket was only implemented in the first activity, and there was no problem when the activity was active. However, when this activity is inactive, the Socket cannot be processed.
(2). When you repeatedly enter the first Activity, there will be a problem with the Socket initialization error. This error occurs because the initialization of the SOKCET is placed in the onCreate of the first Activity.

Since the service was used to make the music player, it should be no problem to think of using serivce to handle the Socket. But is there any other way to do it? Since the individual is just touching Android programming, it is not possible to determine this problem!
In CSDN Android forum question, post: http://bbs.csdn.net/topics/390884423. The answer is: (1) serivce; (2) You can also change the activation mode of activity, so that the serial port is not created repeatedly. Obviously, the second method has not been touched. The first type of serivce is used to achieve a more reliable.

First, implement the Socket Service.

Package Com.jia.leozhengfirstapp;import Java.io.ioexception;import Java.io.inputstream;import Java.io.unsupportedencodingexception;import Java.net.serversocket;import Java.net.socket;import Android.app.service;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.content.intentfilter;import Android.os.ibinder;import Android.util.Log;  public class Socketservice extends Service {private Socket clientsocket = null;  Private ServerSocket mserversocket = null;  Private Socketacceptthread socketacceptthread = null;  Private Socketreceivethread socketreceivethread = null;  Private Socketreceiver Socketreceiver;  public static final String socker_action = "Com.jia.Socket.Control";  public static final String SOCKER_RCV = "COM.JIA.SOCKET.RECEIVESTR";  Private Boolean stop = true;  @Override public IBinder onbind (Intent Intent) {//TODO auto-generated method stub return null;         } @Override public void OnCreate () { Super.oncreate ();          LOG.D ("service", "socket service created");          Socketreceiver = new Socketreceiver ();          Intentfilter filter = new Intentfilter ();          Filter.addaction (socker_action);          Registerreceiver (socketreceiver, filter);            Socketacceptthread = new Socketacceptthread ();   Open the Socket Listener thread Socketacceptthread.start ();  } @Override public void OnStart (Intent Intent, int startid) {log.d ("service", "Socket Service start");  } @Override public void OnDestroy () {LOG.D ("service", "Socket Service destroy!"); } public class Socketreceiver extends Broadcastreceiver {@Override public void onreceive (context context, Intent I            ntent) {String action = intent.getaction ();              if (Action.equals (socker_action)) {String sub_action = Intent.getextras (). getString ("action");         if (Sub_action.equals ("Reconnect")) {LOG.D ("service", "Socket Service:reconnect.");        Socketacceptthread = new Socketacceptthread ();              Open the Socket Listener thread Socketacceptthread.start ();          }}}} Private class Socketacceptthread extends Thread {@Override public void run () {           LOG.D ("service", "Socket Service-socketacceptthread::run");           try {//Instantiate the ServerSocket object and set the port number to 12589 Mserversocket = new ServerSocket (12589);           } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();           } try {//waits for the client to connect (block) Clientsocket = Mserversocket.accept ();           } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();           } socketreceivethread = new Socketreceivethread (clientsocket);           stop = false;             Turn on the receive thread Socketreceivethread.start (); Intent SendIntent = new Intent (SOCKER_RCV);             Sendintent.putextra ("Action", "ClientIP");             Sendintent.putextra ("Content", clientsocket.getinetaddress (). gethostaddress ());       Sends the broadcast, receives the broadcastreceiver in the activity component to the Sendbroadcast (sendintent);       }} Private class Socketreceivethread extends Thread {private InputStream minputstream = null;       Private byte[] BUF;       Private String str = NULL;       Socket sused;           Socketreceivethread (socket s) {LOG.D ("service", "Socket Service-socketreceivethread");               try {//Get input stream this.minputstream = S.getinputstream ();           sused = s;           } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }} @Override public void Run () {LOG.D ("service", "Socket Service-socketreceivethread::           Run "); while (!stop) && (!mserversoCket.isclosed ())) {this.buf = new byte[2048];               Read input data (block Read) try {this.mInputStream.read (BUF);               } catch (IOException E1) {//TODO auto-generated catch block E1.printstacktrace (); }//character encoding convert try {this.str = new String (this.buf, "GB2312"). Tri               M (); } catch (Unsupportedencodingexception e) {//TODO auto-generated catch block e.prints               Tacktrace ();               } Intent sendintent = new Intent (SOCKER_RCV);               Sendintent.putextra ("Action", "Rcvstr");               Sendintent.putextra ("Content", THIS.STR);           Sends the broadcast, receives the broadcastreceiver in the activity component to the Sendbroadcast (sendintent); }       }   }}

The SOCKER_RCV action is processed in each Activity to respond to changes in the Socket state and to receive data.
Communication between Service and Activity needs to be broadcast: broadcast.
(1) Define the global variables in the Activity as follows:

1  Public Static Final String socker_action = "Com.jia.Socket.Control"; 2  Public Static Final String SOCKER_RCV = "Com.jia.Socket.ReceiveStr"; 3 4 socketreceiver socketreceiver

(2) Register the broadcast and start the Socket Service in the Activity's onCreate as follows:

1Socketreceiver =Newsocketreceiver ();2Intentfilter Socketintentfilter =NewIntentfilter ();3 socketintentfilter.addaction (SOCKER_RCV);4 Registerreceiver (socketreceiver,socketintentfilter);5 6Intent socketintent =NewIntent ();7Socketintent.setclass (mainactivity. This, Socketservice.class);8StartService (socketintent);//start the Socket service

(3) Socketreceiver is a class inherited from Broadcastreceiver, implemented as follows:

1  Public classSocketreceiverextendsBroadcastreceiver {2 3 4 @Override5    Public voidOnReceive (Context context, Intent Intent) {6     //TODO auto-generated Method Stub7String action =intent.getaction ();8           if(Action.equals (SOCKER_RCV)) {9String URL = Intent.getextras (). getString ("Action");Ten             if(Url.equals ("ClientIP")) { OneString StrIP = Intent.getextras (). getString ("Content"); A             } -             Else if(Url.equals ("Rcvstr")) { -String strcontent = Intent.getextras (). getString ("Content"); the             } -             Else if(Url.equals ("Disconnect")) { -String strcontent = Intent.getextras (). getString ("Content"); -             } +         } -     } +}

(4) When the socket function is implemented, the socket connection is not detected when the client (that is, the PC side) is disconnected.
When the Socket (TCP) was previously used with WinCE, both the client and the server could detect the TCP disconnect event and handle it. But the Socket programming mechanism under Android doesn't even have this stuff.
First, the test found that when the PC side disconnects, the mobile phone Terminal Service program is executed to the following code snippet will not block, and the function return value is:-1.
Second, on-line search found that the problem is the Android Socket has problems, can be processed by the heartbeat package.
So put the following code:

1 //Read input data (block Read)2 Try {3      This. Minputstream.read (BUF);4}Catch(IOException E1) {5     //TODO auto-generated Catch block6 e1.printstacktrace ();7 }8 change to the following code:9 Try {Ten     intLength = This. Minputstream.read (BUF); One     if(-1 = =length) { A       Try { -Sused.sendurgentdata (0xFF); -       } the       Catch(Exception ex) { -         //link is broken -LOG.V ("service", "Disconnect!!!"); -Stop =true; +         if(NULL!=mserversocket) { - mserversocket.close (); +         } A  at  -Intent sendintent =NewIntent (SOCKER_RCV); -Sendintent.putextra ("Action", "Disconnect"); -Sendintent.putextra ("Content", "read Is-1 & Urgent exception!"); - Sendbroadcast (sendintent); -  in  -         Continue; to       } +     } -}Catch(IOException E1) { the     //TODO auto-generated Catch block * e1.printstacktrace (); $}

The Android-socket function is implemented in the Service "This is the actual use of the situation"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.