Android analog keyboard and mouse

Source: Internet
Author: User

1. Create an android Project

2. Create a primary activity class

[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. Create a multi-threaded sorting class

[Java]
Package test. yutel;
 
Public class Servers extends Thread {
Public Servers (){
}
 
Public void run (){
Send ();
}
 
Public native String send ();
 
Static {
System. loadLibrary ("myserver ");
}
}

3. Use javah to generate C-Language header files

4. Create a C language file

[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. Create an Android. mk file.

[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. Use ndk-build to compile the so file

6. Compile the translation project in eclipse

7. Use the signapk tool to sign the generated apk using the system key file (Tool: build/tools/signapk | key file: build/target/product/security)

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

8. Upload the file to the server.


From yutel's column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.