Hi Dear,
I am creting an android application over JNI.i am created some files in java likes
MainActivity.java
package com.allwikan.runkey;
import com.allwikan.runkey.MainActivity;
import com.allwikan.runkey.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private LinearLayout linearLayout;
private InputMethodManager inputMethodManager;
private Keyboard lkeyboard;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lkeyboard=new Keyboard(MainActivity.this);
linearLayout=(LinearLayout)findViewById(R.id.myLayout);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
lkeyboard.getSystemService(null);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Keyboard.java
package com.allwikan.runkey;
import android.R.bool;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
public class Keyboard {
static {
System.loadLibrary("com_allwikan_runkeyboard_Keyboard");
}
Context mContext;
public Keyboard(Context mContext){
this.mContext = mContext;
}
public void getSystemService(bool makevisible){
if(makevisible != null){
InputMethodManager m = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
else
{
InputMethodManager m = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, 0);
}
}
public native void openKeyBoard();
}
It display the keyboard in emuletor.but my requirement call by c over jni then we created two file .likes
com_allwikan_runkey_Keyboard.h
#include <jni.h>
#ifndef _Included_com_allwikan_runkey_Keyboard
#define _Included_com_allwikan_runkey_Keyboard
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT void JNICALL Java_com_allwikan_runkey_Keyboard_openKeyBoard
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
com_allwikan_runkey_Keyboard.c
#include <com_allwikan_runkey_keyboard.h>
#include "stdio.h"
#include "jni.h"
#include
#define LOG_TAG "com_allwikan_runkeyboard_Keyboard"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
int main()
{
JNIEnv *env;
JavaVM *jvm;
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jclass stringClass;
jobjectArray args;
#ifdef JNI_VERSION_1_2
JavaVMInitArgs vm_args;
JavaVMOption options[1];
vm_args.version = 0x00010002;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_TRUE;
res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
#else
JDK1_1InitArgs vm_args;
char classpath[1024];
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
sprintf(classpath, "%s%c%s",
vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
vm_args.classpath = classpath;
res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
#endif /* JNI_VERSION_1_2 */
if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
exit(1);
}
cls = (*env)->FindClass(env, "Prog");
if (cls == NULL) {
goto destroy;
}
mid = (*env)->GetStaticMethodID(env, cls, "main",
"([Ljava/lang/String;)V");
if (mid == NULL) {
goto destroy;
}
jstr = (*env)->NewStringUTF(env, " from C!");
if (jstr == NULL) {
goto destroy;
}
stringClass = (*env)->FindClass(env, "java/lang/String");
args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
if (args == NULL) {
goto destroy;
}
(*env)->CallStaticVoidMethod(env, cls, mid, args);
destroy:
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
}
(*jvm)->DestroyJavaVM(jvm);
}
When compile this code by ndk-build commond its give error
/home/allwikan/Documents/eclipse_workspace/RunKey/jni/com_allwikan_runkey_Keyboard.c:33: undefined reference to `JNI_CreateJavaVM'
collect2: ld returned 1 exit status
Please help me.
(1)How to resolve this error
(2)how to keyboard display in emulator
(3)which function used in c for display keyboard.
Thanks ...
I have also two file like
Android.mk
LOCAL_PATH :=$(call my-dir)
MY_PATH := $(LOCAL_PATH)
include $(call all-subdir-makefiles)
include $(CLEAR_VARS)
LOCAL_PATH := $(MY_PATH)
LOCAL_LDLIBS := -llog -ldl
LOCAL_MODULE := com_allwikan_runkey_Keyboard
LOCAL_SRC_FILES := com_allwikan_runkey_Keyboard.c
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)
and
application.mk
APP_ABI :=all
then where am i wrong.
please help me....