Android 4.4 如何在ShutdownThread类中添加Native方法

代码路径:
frameworks/base/services/java/com/android/server/power/ShutdownThread.java
frameworks/base/services/jni/onload.cpp
frameworks/base/services/jni/com_android_server_power_ShutdownThread.cpp
在ShutdownThread.java文件中定义一个Native方法

public static native void disableA();

在frameworks/base/services/jni/目录下面创建一个JNI的com_android_server_power_ShutdownThread.cpp文件

/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "ShutdownThread-JNI"

//#define LOG_NDEBUG 0
#include "JNIHelp.h"
#include <android_runtime/AndroidRuntime.h>
#include <android_runtime/Log.h>
#include <utils/Log.h>
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<errno.h>
#include<unistd.h>
#include<sys/ioctl.h>
#include<jni.h>  // 一定要包含此文件
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include <android/log.h>
#include <limits.h>

#define CMD_FLAG  'i'
#define PWR_ENABLE      _IOR(CMD_FLAG,0x00000003,__u32)
#define PWR_DISABLE     _IOR(CMD_FLAG,0x00000002,__u32)
#define ACC_ENABLE      _IOR(CMD_FLAG,0x00000001,__u32)
#define ACC_DISABLE     _IOR(CMD_FLAG,0x00000000,__u32)

#define DEVICE_NAME "/dev/gpio"

namespace android {

int fd;

static void disableA(JNIEnv* env, jobject obj) {
   fd=open(DEVICE_NAME,O_RDWR);
	if(fd<0){
		ALOGE("don't open dev");
	}else{
		ioctl(fd,ACC_DISABLE,NULL) ;
		close(fd);
		ALOGE("ACC_DISABLE");
	}
}

static const JNINativeMethod gShutdownThreadMethods[] = {
    /* name, signature, funcPtr */
    { "disableA", "()V",
            (void*) disableA },
};

int register_android_server_ShutdownThread(JNIEnv* env) {
    int res = jniRegisterNativeMethods(env, "com/android/server/power/ShutdownThread",
            gShutdownThreadMethods, NELEM(gShutdownThreadMethods));
    (void) res;  // Faked use when LOG_NDEBUG.
    LOG_FATAL_IF(res < 0, "Unable to register native methods.");
    return 0;
}

} /* namespace android */

在onload.cpp文件中注册com_android_server_power_ShutdownThread文件中JNI函数register_android_server_ShutdownThread(env)

namespace android {
int register_android_server_AlarmManagerService(JNIEnv* env);
int register_android_server_ConsumerIrService(JNIEnv *env);
int register_android_server_InputApplicationHandle(JNIEnv* env);
int register_android_server_InputWindowHandle(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_SerialService(JNIEnv* env);
int register_android_server_UsbDeviceManager(JNIEnv* env);
int register_android_server_UsbHostManager(JNIEnv* env);
int register_android_server_VibratorService(JNIEnv* env);
int register_android_server_SystemServer(JNIEnv* env);
int register_android_server_location_GpsLocationProvider(JNIEnv* env);
int register_android_server_location_FlpHardwareProvider(JNIEnv* env);
int register_android_server_connectivity_Vpn(JNIEnv* env);
int register_android_server_AssetAtlasService(JNIEnv* env);
int register_android_server_ShutdownThread(JNIEnv* env);
};

using namespace android;

extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    JNIEnv* env = NULL;
    jint result = -1;

    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        ALOGE("GetEnv failed!");
        return result;
    }
    ALOG_ASSERT(env, "Could not retrieve the env!");

    register_android_server_PowerManagerService(env);
    register_android_server_SerialService(env);
    register_android_server_InputApplicationHandle(env);
    register_android_server_InputWindowHandle(env);
    register_android_server_InputManager(env);
    register_android_server_LightsService(env);
    register_android_server_AlarmManagerService(env);
    register_android_server_UsbDeviceManager(env);
    register_android_server_UsbHostManager(env);
    register_android_server_VibratorService(env);
    register_android_server_SystemServer(env);
    register_android_server_location_GpsLocationProvider(env);
    register_android_server_location_FlpHardwareProvider(env);
    register_android_server_connectivity_Vpn(env);
    register_android_server_AssetAtlasService(env);
    register_android_server_ConsumerIrService(env);
    register_android_server_ShutdownThread(env);

    return JNI_VERSION_1_4;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值