A get through jni, java framework, application exercises three

A get through jni, java framework, application exercises three

Daniel Wood 08/27/2010

Exercise: get a string from the jni layer, on the screen, of course, cell phone screen

Requirements:
1. Need to include the three-tier structure: JNI, java framework, java application
2. The contents of the string "Hello from JNI!", Must be defined in the JNI layer

This practice is under the Android source project to do, my Android source code path is / Android/android-1.6_r2

Start:

1. JNI layer: in / Android/android-1.6_r2/frameworks/base/core/jni example android_mytest_hellojni.cpp file path to create this file in the JNI layer that implement the interface. File as follows: (refer to the same directory android_debug_JNITest.cpp document preparation)

#define LOG_TAG "HelloJNI"
#include "jni.h"
#include "nativehelper/JNIHelp.h"
#include "utils/Log.h"
#include "utils/misc.h"

namespace android {
static jstring android_mytest_hellojni_displayString(JNIEnv *env, jclass clazz)
{
 return env->NewStringUTF("Hello from JNI!");
} 

/*
 * JNI registration.
 */
static JNINativeMethod gMethods[] = {
    /* name, signature, funcPtr */
    { "displayString",      "()Ljava/lang/String;",
            (void*) android_mytest_hellojni_displayString },

};
int register_android_mytest_hellojni(JNIEnv* env)
{ // This directory structure is a Javaframework layer files directory  , And must be consistent
return jniRegisterNativeMethods(env, "android/mytest/hellojni",
        gMethods, NELEM(gMethods));
}
};

2. JNI layer: changes to the build configuration

2.1 changes / Android/android-1.6_r2/frameworks/base/core/jni directory Android.mk documents in LOCAL_SRC_FILES: = \, by adding
android_mytest_hellojni.cpp \
2.2 changes / Android/android-1.6_r2/frameworks/base/core/jni directory AndroidRuntime.cpp file extern int add back
extern int register_android_mytest_hellojni (JNIEnv * env);
Then static const RegJNIRec gRegJNI [] = (add the following
REG_JNI (register_android_mytest_hellojni),
This, JNI layer modification stop here.

3. Javaframework layer: in / Android/android-1.6_r2/frameworks/base/core/java/android / new directory mytest, the new file in the directory hellojni.java statement interfaces. As follows: (refer to android-1.6_r2/frameworks/base/core/java/android/debug directory JNITest.java document preparation)

package android.mytest;
public class hellojni{
        public hellojni(){}
// Here it was declared as public can be  application Call
        public static native String displayString();
}

4. And then in eclipse, or inside the editor such as Notepad (so that only to illustrate only the source code), the new Android project, an example is structured as follows:

A get through jni, java framework, application exercises three

In testApp.java file reads as follows:

package com.integration.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
// You need to import their own defined package  , A package name can be based on the file directory structure  

import android.mytest.hellojni;
public class testAPP extends Activity {
    /** Called when the activity is first created. */
        private TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView)findViewById(R.id.tv);
        hellojni hello = new hellojni();    // Call himself wrote  JNI
        tv.setText(hello.displayString()+"");
    }
}

Then copy the files to / Android/android1.6_r2/packages/apps, and see tutorial "in the source code to compile their own Android project" you can see the application in the emulator to run the following diagram

A get through jni, java framework, application exercises three

分类:Mobile 时间:2010-09-15 人气:182
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.645 (s). 10 q(s)