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

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


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;
    public void onCreate(Bundle savedInstanceState) {
        tv = (TextView)findViewById(R.id.tv);
        hellojni hello = new hellojni();    // Call himself wrote  JNI

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 人气:183
blog comments powered by Disqus


iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



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

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.316 (s). 12 q(s)