2014-09-05 31 views
1

我想知道前一天是否有可能使用服务来保持活动套接字,直到服务被破坏? 这是一个非常有趣的事情,我没有很好地学习,另外你可以添加一个通知来指示连接到套接字的状态。 有没有人有示例来告诉你,如果只是为了了解如何使用套接字构建服务? 我感谢你提前套接字在服务+通知

PS:我看了在计算器上,但我还没有发现太多的说明例

回答

2

你是正确的假设,一个Service是这份工作的合适工具。

参见Services | Android Developers

A服务是可以在后台执行长时间运行的操作,并且不提供用户界面的应用程序组件。

保持套接字符合长时间运行的后台操作。

我创造了这个例子对你来说,它具有以下功能:

  • 连接/重新连接并读取启动ThreadInputStreamonCreate()
  • 报告在Notification
  • 关闭当前连接状态Socket并停止Thread in onDestroy()

MyService.java

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.support.v4.app.NotificationCompat; 
import android.support.v4.app.NotificationManagerCompat; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.InetSocketAddress; 
import java.net.Socket; 
import java.net.UnknownHostException; 

public class MyService extends Service implements Runnable { 

    private static final int NOTIFICATION_ID = 1; 

    private boolean mRunning = false; 
    private Thread mThread; 

    private Socket mSocket; 
    private InputStream mInputStream; 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     setNotificationMessage("Service created"); 

     if (mThread == null) { 
      mRunning = true; 

      mThread = new Thread(this); 
      mThread.start(); 
     } 
    } 

    @Override 
    public void run() { 
     try { 
      while (mRunning) { 
       try { 
        setNotificationMessage("Connecting"); 

        mSocket = new Socket(); 
        mSocket.connect(new InetSocketAddress("192.168.56.1", 9899)); 

        mInputStream = mSocket.getInputStream(); 

        setNotificationMessage("Connected"); 

        for (int c = mInputStream.read(); c > -1; c = mInputStream.read()) { 
         setNotificationMessage("Connected: " + (char) c); 
        } 
       } catch (UnknownHostException ignored) { 
        setNotificationMessage("Unknown host"); 
       } catch (IOException ignored) { 
        setNotificationMessage("Disconnected"); 
        close(); 
       } 

       try { 
        // Reconnect delay 
        Thread.sleep(1000); 
       } catch (InterruptedException ignored) { 
       } 
      } 
     } finally { 
      // Will eventually call onDestroy() 
      stopSelf(); 
     } 
    } 

    private void setNotificationMessage(CharSequence message) { 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
     builder.setSmallIcon(R.drawable.ic_launcher); 
     builder.setContentTitle("Connection status"); 
     builder.setContentText(message); 

     NotificationManagerCompat nm = NotificationManagerCompat.from(this); 
     nm.notify(NOTIFICATION_ID, builder.build()); 
    } 

    private void close() { 
     if (mInputStream != null) { 
      try { 
       mInputStream.close(); 
       mInputStream = null; 
      } catch (IOException ignored) { 
      } 
     } 

     if (mSocket != null) { 
      try { 
       mSocket.close(); 
       mSocket = null; 
      } catch (IOException ignored) { 
      } 
     } 
    } 

    @Override 
    public void onDestroy() { 
     if (mThread != null) { 
      mRunning = false; 

      close(); 

      while (true) { 
       try { 
        mThread.interrupt(); 
        mThread.join(); 
        mThread = null; 
        break; 
       } catch (InterruptedException ignored) { 
       } 
      } 
     } 

     setNotificationMessage("Service destroyed"); 

     super.onDestroy(); 
    } 
} 
+0

什么方法加入线程的? – 2014-09-08 09:06:10

+1

Join会等待线程完成执行,请参阅[Thread | Android Developers](http://developer.android.com/reference/java/lang/Thread.html#join()) – Nicklas 2014-09-08 09:34:36