android 创建socket 通信型service

仿照instaled写的

1、声明   在system/core/rootdir/init.rc中添加:

service testlang /system/bin/testlang

     #       名称        路径
      class main
      socket dumpstate stream 0660 shell log
      #disabled 没有立马启用
      #oneshot 如果用这个参数说明只启动一次

2、实现    在frameworks/base/cmds/添加testlang文件夹

testlang.h 内容:

#define LOG_TAG "testlang"
#include <stdio.h>

#include <cutils/sockets.h>
#include <cutils/log.h>
#include <fcntl.h>
#include <errno.h>

#define MY_SOCKET_PATH "testlang"


testlang.c 内容:

#include "testlang.h"

#define LOG_NDEBUG 0
#define LOG_NDDEBUG 0
#define LOG_NVDEBUG 0

static int read_testlang(int fd, void* _buf, size_t count){
    int r;
    char *buf = _buf;
    r = read(fd,buf,count);
    LOGD("langxw buf = %s",*buf);
    return 0;
}

int main(int argc, char** argv){
    LOGE("langxw testlang main");
    
    char buf[1];

    struct sockaddr addr;
    socklen_t alen;
    int mysocket, s, count;

    LOGD("langxw A/N mysocket  = %d, s = %d, count = %d\n",mysocket, s, count);
    
    if(argc==2){
        LOGD("langxw aargv[1] = %s\n",argv[1]);
    }else{
        LOGD("langxw A/N argc = %d\n",argc);
    }

    mysocket = android_get_control_socket(MY_SOCKET_PATH);
    if(mysocket<0){
        LOGE("langxw testlang failed");
        printf("langxw failed to get socket , reason %s,\n",strerror(errno));
        exit(1);
    }else{
        LOGE("langxw testlang success");
    }
    if(listen(mysocket,5)){
        LOGE("langxw listten on socket failed\n");
        exit(1);
    }

    fcntl(mysocket, F_SETFD, FD_CLOEXEC);

    int timce = -1;
    for(;;){
        LOGE("langxw timce = %d\n",timce);
        alen = sizeof(addr);
        s = accept(mysocket, &addr, &alen);
        if(s<0){
           // LOGE("langxw accept failed:%s\n",strerror(errno));
            LOGE("langxw accept failed");
            continue;
        }else{
            LOGE("langxw accept success:%s\n",strerror(errno));
            LOGE("langxw accept success");
        }

        fcntl(s, F_SETFD, FD_CLOEXEC);
        LOGE("langxw new connection");

        for(;;){
            //read_testlang(s,buf,1);
        }


        LOGE("langxw closing connection");
        close(s);
    }

    LOGE("langxw testlang main end");
    return 0;
}

Android.mk 内容:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := OPTIONAL
LOCAL_MODULE := testlang
LOCAL_SRC_FILES := testlang.c

#log 所需的库
LOCAL_SHARED_LIBRARIES :=   libcutils

include $(BUILD_EXECUTABLE)

3、client端:

部分代码:

import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


    //langxw add
    InputStream mIn;
    OutputStream mOut;
    LocalSocket mSocket;
    private boolean connect() {
        if (mSocket != null) {
            Log.e(TAG, "langxw already connecting...");
            return true;
        }
        Log.e(TAG, "langxw connecting...");
        try {
            mSocket = new LocalSocket();

            LocalSocketAddress address = new LocalSocketAddress("installd",
                    LocalSocketAddress.Namespace.RESERVED);

            mSocket.connect(address);

            mIn = mSocket.getInputStream();
            mOut = mSocket.getOutputStream();
        } catch (IOException ex) {
            Log.i(TAG, "langxw connect failed");
            ex.printStackTrace();
            disconnect();
            return false;
        }
        return true;
    }
    
    private void disconnect() {
        Log.i(TAG, "langxw disconnecting...");
        try {
            if (mSocket != null)
                mSocket.close();
        } catch (IOException ex) {
        }
        try {
            if (mIn != null)
                mIn.close();
        } catch (IOException ex) {
        }
        try {
            if (mOut != null)
                mOut.close();
        } catch (IOException ex) {
        }
        mSocket = null;
        mIn = null;
        mOut = null;
    }

4、需要android.uid.system权限

在AndroidManifest.xml

<manifest
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:sharedUserId="android.uid.system">

APK 的Android.mk文件里面添加

LOCAL_CERTIFICATE := platform

注意:需要全编译才行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值