Home | History | Annotate | Download | only in hellojni
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.example.hellojni;
     17 
     18 import android.app.Activity;
     19 import android.widget.TextView;
     20 import android.os.Bundle;
     21 
     22 
     23 public class HelloJni extends Activity
     24 {
     25     /** Called when the activity is first created. */
     26     @Override
     27     public void onCreate(Bundle savedInstanceState)
     28     {
     29         super.onCreate(savedInstanceState);
     30 
     31         /* Create a TextView and set its content.
     32          * the text is retrieved by calling a native
     33          * function.
     34          */
     35         TextView  tv = new TextView(this);
     36         tv.setText( stringFromJNI() );
     37         setContentView(tv);
     38     }
     39 
     40     /* A native method that is implemented by the
     41      * 'hello-jni' native library, which is packaged
     42      * with this application.
     43      */
     44     public native String  stringFromJNI();
     45 
     46     /* This is another native method declaration that is *not*
     47      * implemented by 'hello-jni'. This is simply to show that
     48      * you can declare as many native methods in your Java code
     49      * as you want, their implementation is searched in the
     50      * currently loaded native libraries only the first time
     51      * you call them.
     52      *
     53      * Trying to call this function will result in a
     54      * java.lang.UnsatisfiedLinkError exception !
     55      */
     56     public native String  unimplementedStringFromJNI();
     57 
     58     /* this is used to load the 'hello-jni' library on application
     59      * startup. The library has already been unpacked into
     60      * /data/data/com.example.hellojni/lib/libhello-jni.so at
     61      * installation time by the package manager.
     62      */
     63     static {
     64         System.loadLibrary("hello-jni");
     65     }
     66 }
     67