当前位置:操作系统 > 安卓/Android >>

android模拟键盘鼠标

1,新建一个android 项目

2,新建主activity类

[java] 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,新建一个多线程序类

[java]
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语言文件

[cpp]
#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文件

[python]
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,上传到服务器后就可以了。


摘自 yutel的专栏

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,