怎么在service里面启动socket服务

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么在service里面启动socket服务相关的知识,希望对你有一定的参考价值。

参考技术A 首先,实现 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 www.hbbz08.com = new SocketAcceptThread();
// 开启 Socket 监听线程
socketAcceptThread.start();


@Override
public void onStart(Intent intent, int startId)
Log.d("service", "socket service start");

以上是关于怎么在service里面启动socket服务的主要内容,如果未能解决你的问题,请参考以下文章

用bind的方法启动service,调用者退出后,service也销毁?

怎么把自己写的脚本添加到服务里面,即可以使用service命令来调用

Apollo启动报Config service failed to start in 120 seconds! Please check ./service/apollo-ser

Apollo启动报Config service failed to start in 120 seconds! Please check ./service/apollo-ser

ubuntu命令安装tomcat 并怎么启动服务

SaltStack 启动服务