关闭

android 模拟键盘鼠标

标签: androidincludereportstructmodulestring
4801人阅读 评论(10) 收藏 举报
分类:

1,新建一个android 项目

2,新建主activity类

package test.yutel;

import android.app.Activity;
import android.os.Bundle;

public class RemoteActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		 Servers s=new Servers();
		 s.start();
		 finish();
	}
}


2,新建一个多线程序类

package test.yutel;

public class Servers extends Thread {
	public Servers() {
	}

	public void run() {
		send();
	}

	public native String send();

	static {
		System.loadLibrary("myserver");
	}
}

3,通过javah 生成c语言的头文件

4,新建一个C语言文件

#include <jni.h>
#define LOG_TAG "myserver"
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args)
#include <android/log.h>

#include <string.h>
#include <errno.h> 
#include <stdio.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
void simulate_key(int fd,int kval) 
{
	struct input_event event; 
	event.type = EV_KEY; 
	event.value = 1; 
	event.code = kval;

	gettimeofday(&event.time,0); 
	write(fd,&event,sizeof(event)) ;

	event.type = EV_SYN; 
	event.code = SYN_REPORT; 
	event.value = 0; 
	write(fd, &event, sizeof(event)); 

	memset(&event, 0, sizeof(event)); 
	gettimeofday(&event.time, NULL); 
	event.type = EV_KEY; 
	event.code = kval; 
	event.value = 0; 
	write(fd, &event, sizeof(event)); 
	event.type = EV_SYN; 
	event.code = SYN_REPORT; 
	event.value = 0; 
	write(fd, &event, sizeof(event));
}
void simulate_mouse(int fd)
{
	struct input_event event;
	memset(&event, 0, sizeof(event));
	gettimeofday(&event.time, NULL);
	event.type = EV_REL;
	event.code = REL_X;
	event.value = 10;
	write(fd, &event, sizeof(event));

	event.type = EV_REL;
	event.code = REL_Y;
	event.value = 10;
	write(fd, &event, sizeof(event));

	event.type = EV_SYN;
	event.code = SYN_REPORT;
	event.value = 0;
	write(fd, &event, sizeof(event));
}
JNIEXPORT jstring JNICALL Java_test_yutel_Servers_send (JNIEnv* env, jobject obj)
{
	int fd_kbd = open("/dev/input/event3",O_RDWR);
	if(fd_kbd<=0){
		LOGD("open keybd error:%s",strerror(errno));
		return -2; 
	}
	int fd_mouse = open("/dev/input/event4",O_RDWR); 
    if(fd_mouse<=0){
        LOGD("open mouse error:%s",strerror(errno));
        return -2;
    }
	int i = 0; 
	for(i=0; i< 10; i++) 
	{ 
		simulate_key(fd_kbd, KEY_A + i);
		simulate_mouse(fd_mouse);
		sleep(1); 
	}
	close(fd_kbd);
	close(fd_mouse);
	LOGD("Hello LIB!"); 
	return (*env)->NewStringUTF(env, (char *)"JNITest Native String"); 
}

5,新建Android.mk文件

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
    myserver.c
LOCAL_C_INCLUDES := \
    $(JNI_H_INCLUDE)
#LOCAL_SHARED_LIBRARIES := libutils
LOCAL_LDLIBS := -llog
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE := myserver
include $(BUILD_SHARED_LIBRARY)

5,采用ndk-build进行编译成so文件

6,在eclipse中编译本项目

7,用signapk工具将生成的apk采用系统密钥文件进行签名(工具:build/tools/signapk | 密钥文件:build/target/product/security) 

signapk platform.x509.pem platform.pk8 input.apk output.apk

8,将output.apk安装到手机里就可以了。

1
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:104910次
    • 积分:1464
    • 等级:
    • 排名:千里之外
    • 原创:37篇
    • 转载:12篇
    • 译文:0篇
    • 评论:37条
    文章分类
    最新评论