android框架层开发,android平台开发之框架层

三,框架层(framework)

这里主要实现两部分

(1: 为Android HAL编写JNI方法,以便使得上层的APP能够使用下层提供的硬件服务

(2: 在Android系统的框架层提供Java接口的硬件服务

1,在frameworks/base/services/jni中新建com_android_server_swtled.cpp

点击(此处)折叠或打开

#define LOG_TAG "SwtledService"

#include "jni.h"

#include "JNIHelp.h"

#include "android_runtime/AndroidRuntime.h"

#include

#include

#include

#include

#include

namespace android

{

struct swtled_device_t* swtled_device = NULL;

static void swtled_set_on(JNIEnv* env, jobject clazz,jint number)

{

int num = number;

LOGI("Swtled JNI: select lighton is %d",num);

if (!swtled_device)

{

LOGI("Swtled JNI:device is not open.");

return;

}

swtled_device->set_on(swtled_device,num);

}

static void swtled_set_off(JNIEnv* env, jobject clazz,jint number)

{

int num = number;

LOGI("Swtled JNI: select lightoff is %d",num);

if (!swtled_device)

{

LOGI("Swtled JNI:device is not open.");

return;

}

swtled_device->set_off(swtled_device,num);

}

static inline int swtled_device_open(hw_module_t* module,swtled_device_t** device)

{

return module->methods->open(module,SWTLED_HARDWARE_MODULE_ID,(hw_device_t**)device);

}

static jboolean swtled_init(JNIEnv* env,jclass clazz)

{

swtled_module_t* swtledmodule;

LOGI("Swtled JNI: initializing...");

if (hw_get_module(SWTLED_HARDWARE_MODULE_ID,(const struct hw_module_t**)&swtledmodule)==0)

{

LOGI("Swtled JNI: swtled stub be found.");

if (swtled_device_open(&(swtledmodule->common),&swtled_device) == 0)

{

LOGI("Swtled JNI: swtled device open successful.");

return 0;

}

LOGI("Swtled JNI: failed to open swtled device.");

return -1;

}

LOGI("Swtled JNI: failed to get swtled stub module.");

return -1;

}

static const JNINativeMethod method_table[] =

{

{"init_native", "()Z", (void*)swtled_init},

{"setOn_native","(I)V",(void*)swtled_set_on},

{"setOff_native","(I)V",(void*)swtled_set_off},

};

int register_android_server_SwtledService(JNIEnv* env)

{

return jniRegisterNativeMethods(env,"com/android/server/SwtledService",method_table,NELEM(method_table));

}

}

修改同级目录下的Android.mk和Onload.cpp文件

在Android.mk的LOCAL_SRC_FILES:=下添加

点击(此处)折叠或打开

LOCAL_SRC_FILES:= \

....

com_android_server_swtled.cpp \

在Onload.cpp中的namespace android {  下加入

点击(此处)折叠或打开

int register_android_server_SwtledService(JNIEnv* env);

同时在JNI_OnLoad函数下加入

点击(此处)折叠或打开

register_android_server_SwtledService(env);

2,编译

(1,执行mmm framework/base/services/jni

(2,make snod

3,在frameworks/base/core/java/android/os 新建ISwtledService.aidl,代码如下

点击(此处)折叠或打开

package android.os;

interface ISwtledService {

void setOn(int number);

void setOff(int number);

}

返回到frameworks/base目录,打开Android.mk文件,修改LOCAL_SRC_FILES变量的值,增加ISwtledService.aidl源文件

点击(此处)折叠或打开

LOCAL_SRC_FILES += /

....................................................................

core/java/android/os/IVibratorService.aidl /

core/java/android/os/ISwtledService.aidl /

core/java/android/service/urlrenderer/IUrlRendererService.aidl /

4,编译ISwtledService接口

mmm framework/base,如果正确,那么会根据ISwtledService.aidl生成对应的ISwtledService.Stub接口

5,进入到frameworks/base/services/java/com/android/server目录,新增ISwtledService.java文件:

点击(此处)折叠或打开

package com.android.server;

import android.content.Context;

import android.os.ISwtledService;

import android.util.Slog;

public class SwtledService extends ISwtledService.Stub {

private static final String TAG = "SwtledService";

SwtledService()

{

init_native();

}

public void setOn(int number)

{

setOn_native(number);

}

public void setOff(int number)

{

setOff_native(number);

}

private static native boolean init_native();

private static native void setOn_native(int number);

private static native void setOff_native(int number);

};

6,修改同目录下的SystemServer.java文件,在ServerThread::run方法里加入

点击(此处)折叠或打开

try{

Slog.i(TAG, "Swtled Service");

ServiceManager.addService("swtled", new SwtledService());

} catch (Throwable e) {

Slog.e(TAG, "Failure starting Swtled Service", e);

}

7,编译SwtledService.java并打包进system.img

(1)mmm framework/base/services/java

(2)make snod

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值