很精练的android系统开发介绍
http://blog.csdn.net/luoshengyang/article/details/6567257
摘要如下:
import android.os.IHelloService; private IHelloService helloService; helloService = IHelloService.Stub.asInterface(ServiceManager.getService("hello")); helloService.setVal(val);"hello": 系统启动时加载HelloService时指定
interface IHelloService { void setVal(int val); int getVal(); }
public class HelloService extends IHelloService.Stub { HelloService() { init_native(); } public void setVal(int val) { setVal_native(val); } public int getVal() { return getVal_native(); } private static native boolean init_native(); private static native void setVal_native(int val); private static native int getVal_native(); };
ServiceManager.addService("hello", new HelloService());
namespace android { struct hello_device_t* hello_device = NULL; static void hello_setVal(JNIEnv* env, jobject clazz, jint value) { hello_device->set_val(hello_device, val); } static jint hello_getVal(JNIEnv* env, jobject clazz) { hello_device->get_val(hello_device, &val); } static inline int hello_device_open(const hw_module_t* module, struct hello_device_t** device) { return module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (struct hw_device_t**)device); } static jboolean hello_init(JNIEnv* env, jclass clazz) { hello_module_t* module; if(hw_get_module(HELLO_HARDWARE_MODULE_ID, (const struct hw_module_t**)&module) == 0) { if(hello_device_open(&(module->common), &hello_device) == 0) { <span style="white-space:pre"> </span> } <span style="white-space:pre"> </span> } } static const JNINativeMethod method_table[] = { {"init_native", "()Z", (void*)hello_init}, {"setVal_native", "(I)V", (void*)hello_setVal}, {"getVal_native", "()I", (void*)hello_getVal}, }; int register_android_server_HelloService(JNIEnv *env) { return jniRegisterNativeMethods(env, "com/android/server/HelloService", method_table, NELEM(method_table)); } };