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的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。