`

Android 开发手记二

 
阅读更多

 工程建立好后,编辑 src/com/jpf/myjni/MyJNI.java 文件,内容如下:

package com.jpf.myjni; 

import android.app.Activity; 

import android.widget.TextView; 

import android.os.Bundle; 

public class MyJNI extends Activity { 

    /** Called when the activity is first created. */ 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super .onCreate(savedInstanceState); 

        TextView  tv = new TextView( this ); 

        tv.setText( stringFromJNI() ); 

        System. out .println( "Here we go ..." ); 

        setContentView(tv); 

        System. out .println( "Done!" ); 

    } 

    public native String  stringFromJNI(); 

    static { 

           System.loadLibrary ( "myjni" ); 

    } 

} 
 

 

需要说明的几点:
       public native String  stringFromJNI(); 这句申明,带有 native 关键字,说明该方法是本地方法。

       System.loadLibrary ( "myjni" ); 这句就是用来加载我们的 c 动态库的。上面声明的方法,具体实现,就在我们加载的库中。
 

       建立好工程,再次编译,在 cygwin 中运行 ndk-build ,结果 OK 。

Administrator@lenovo-0e47e162 /android/android-ndk-r4/samples/myndk 

$ ndk-build 

Compile thumb  : myjni <= /android/android-ndk-r4/samples/myndk/jni/myjni.c 

SharedLibrary  : libmyjni.so 

Install        : libmyjni.so => /android/android-ndk-r4/samples/myndk/libs/armea 

bi 
 

 

  我们看到,需要的共享库已经生成,并且安装好了。下面就可以生成 apk 了。
       在 Cygwin 中进行工程的 build ,编译后,在工程的 bin 目录下,会看到我们的 apk 包。

 好,我们试试看,能否正常运行。在 Eclipse 选择执行方式为 Android Application ,点击 run ,以下 console 的输出:

[2010-07-07 14:26:18 - MyJNI] ------------------------------ 

[2010-07-07 14:26:18 - MyJNI] Android Launch! 

[2010-07-07 14:26:18 - MyJNI] adb is running normally. 

[2010-07-07 14:26:18 - MyJNI] Performing com.jpf.myjni.MyJNI activity launch 

[2010-07-07 14:26:18 - MyJNI] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'android21' 

[2010-07-07 14:26:18 - MyJNI] WARNING: Application does not specify an API level requirement! 

[2010-07-07 14:26:18 - MyJNI] Device API version is 7 (Android 2.1-update1) 

[2010-07-07 14:26:18 - MyJNI] Uploading MyJNI.apk onto device 'emulator-5554' 

[2010-07-07 14:26:18 - MyJNI] Installing MyJNI.apk... 

[2010-07-07 14:26:24 - MyJNI] Success! 

[2010-07-07 14:26:25 - MyJNI] Starting activity com.jpf.myjni.MyJNI on device 

[2010-07-07 14:26:29 - MyJNI] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.jpf.myjni/.MyJNI } 
 

 上面的 warning ,是我们没有指定 API 的版本号。如下指定一下就没有这个 warning 了。

 

  下图为执行的效果:

下图是我们查看 LogCat 的输出: 

 

可以看到我们的输出 MYJNI MyJNI is called   

摘自:http://blog.csdn.net/L____J/article/details/5787759

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics