背景:搞android framework改制,加入一个系统服务。在framework/base/service里
写一个测试程序:
xxxxxx_android_hua_HuaRadio.cpp
#define LOG_TAG "Vehicle-JNI"
#include "JNIHelp.h"
#include "jni.h"
#include <termios.h>
#include <string.h>
#include <fcntl.h>
#include <utils/Log.h>
#define MCU_DEV "/dev/uart-mcu"
using namespace std;
namespace android {
static void xxx_android_hua_HuaRadio_nativeInit
(JNIEnv * env, jobject obj){
jclass cls = env->GetObjectClass(obj);
jmethodID print= env->GetMethodID(cls,"print","()V");
env->CallIntMethod(obj,print);
}
static void xxx_android_hua_HuaRadio_nativeRun
(JNIEnv *, jobject){
LOGE("send data:%d",2222222);
}
static JNINativeMethod HuaRadioMethods [] = {
{"nativeInit","()Z",(void*)xxx_android_hua_HuaRadio_nativeInit},
{"nativeRun","()Z",(void*)xxx_android_hua_HuaRadio_nativeRun},
};
int register_xxx_android_hua_HuaRadio(JNIEnv *env)
{
int res = jniRegisterNativeMethods(env,"cn/com/xxx/android/hua/HuaRadio",
HuaRadioMethods,NELEM(HuaRadioMethods));
return 0;
}
}
HuaRadio.java
package cn.com.xxx.android.hua;
import android.content.Context;
import cn.com.xxx.android.IHuaRadio;
public class HuaRadio extends IHuaRadio.Stub{
private Context context;
public HuaRadio(Context ctx){
System.out.println("HuaRadio init() by hua");
this.context=ctx;
try{
nativeInit();
nativeRun();
}catch(Exception e){
System.out.plintln("hua "+e);
}
}
public void init(){
nativeRun();
}
public void print(){
System.out.println("HuaService print method!!!");
}
public native void nativeInit();
public native void nativeRun();
}
最后编译出现
No implementation found for native问题
为什么呢?
找了许久,发现register_xxx_android_hua_HuaRadio是要自己注册的
在onload.cpp 里加入
namespace android {
int register_android_server_AlarmManagerService(JNIEnv* env);
int register_android_server_BatteryService(JNIEnv* env);
int register_android_server_InputManager(JNIEnv* env);
int register_android_server_LightsService(JNIEnv* env);
int register_android_server_PowerManagerService(JNIEnv* env);
int register_android_server_UsbService(JNIEnv* env);
int register_android_server_VibratorService(JNIEnv* env);
int register_android_server_SystemServer(JNIEnv* env);
int register_android_server_location_GpsLocationProvider(JNIEnv* env);
//加入这个 就OK了
int register_cs2c_android_hua_HuaRadio(JNIEnv* env);
};
哎!!!