A get through jni, java framework, application exercises three

sponsored links
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
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of A get through jni, java framework, application exercises three

  • 12 The most important J2EE best practices

    MVC Framework (as well as the J2EE implementation of the MVC) is one of the advantages of the component elements can (in fact, very easy) on your application for unit testing. Therefore, you can easily entity bean, session bean and the JSP an independent

  • Design Pattern in EJB Applications

    What is a Design Pattern Design patterns are a set of re-use, most people know, after cataloging, code design experience. For the use of design patterns are reusable code, so that the code more easily understood by others, and ensure the reliability code.

  • [Have resolved] how to give rails of the model class of congestive (include / extent module)

    Has been resolved Please visit: http://henrik.nyh.se/2008/02/rails-model-extensions 1: the introduction of new features to the controller My approach: I tried to introduce in the application controller extra modules: require "string" This "string

  • hibernate the third study (mapping file configuration)

    If you do not want to use the default hibernate.cfg.xml file as a configuration file, we can also specify the configuration file name: File file = new File ( "C: \ \ myhibernate.xml"); Configuration config = new Configuration (). Configure (file

  • Building Scalable java EE application (2)

    When the number of concurrent users marked the beginning of growth, you may be dissatisfied with a single machine can provide the performance, or because of a single JVM instance gc restrictions, you can not expand your java application, in such circumsta

  • How to do a good job in the demand for change management - needs to change flow specification

    Project Leader: assessment needs to change some of the workload to determine whether it needs to change the contents of the progress on the development of an impact, if the demand to change the progress of an impact on the development, the project le ...

  • Hibernate Inteceptor

    The end of the project stage, the client suddenly put forward a very troublesome but normal demand, the system records all changes must be carried out. Formats such as: 2004.1.1 12:30 Ikuya wind orders Sales Order Date 2004.1.2-> 2004.1.3 The firs ...

  • WebQQ, ExtJs + Servlet + Hibernate + Spring implementation

    Code for the development of boredom when using ExtJs + Servlet + hibernate (Ant + xdoclet generate HBM files) + spring implementation, Pure whim, but implementation has been more than chat, group chat, what's not achieve, nor how to consider the perfo

  • spring struts2.0 hibernate bug killer 1

    exception There is no Action mapped for namespace / and action name checkLogin. - [Unknown location] com.opensymphony.xwork2.DefaultActionProxy.prepare (DefaultActionProxy.java: 186) org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy ...

  • J2EE questions Noodles

    2. Abstract class and interface difference (1) interface can be multiple implements, can only be a single abstract class extends (2) only the definition of interfaces, abstract class can have the definition and implementation (3) the definition of the def

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries