为Android SDK增加应用案例实现

为Android SDK增加应用案例实现是本文要介绍的内容,主要是来了解并学习Android SDK的内容,具体关于Android SDK的内容来看本文详解。


在开发Android开发应用的过程中,通常有两种方式:NDK和eclipse,或者直接在Android SDK增加。这里介绍如何在Android SDK环境中增加一个应用程序。其中涉及到JNI,JAVA对JNI的调用,其中JNI代码文件为C++。


第一步,建立JNI层代码,注意,JNI代码的路径与JAVA的代码路径有很大的关系,一定要保持一致。


Android SDK在frameworks/base/core/jni目录下建立JNI C++文件android_test.cpp,内容如下:


#define LOG_TAG "FMC"  
#include "jni.h"  
#include "android_runtime/AndroidRuntime.h"  
#include <nativehelper/JNIHelp.h> 
#include "utils/Log.h"  
extern "C" {  
      int test(void);//此函数可以来自c代码或c库  
};  
namespace android   //注意名称空间和JAVA调用JNI所有的路径有紧密的关系,不能随便取名称空间。  
 
                    //原作者这句话不对  
 
                    //这只是c++的命名空间和JNI没有关系,如果不用android namespace那么在  
 
                    //AndroidRuntime.cpp里面也要声明在android namespace外面。  
{  
 static jint android_test(JNIEnv *env, jobject clazz)  
 {  
  return test();  
 }  
 static JNINativeMethod method_table[] = {  
  { "my_test", "()I", (void*)android_test}, //第一个参数为字串,这是JAVA层看到的函数名;  
         //第二个参数为该函数的形参说明,  
         //JAVA代码就是靠这个参数知道所调用函数的形参情况;  
         //第三个参数为JNI代码中实际调用的C函数。  
 }  
 int register_android_test(JNIEnv *env)//这是JNI注册函数,android.test就是java调用层(第三步)看到的包路径,  
         //如果这里是register_android_hardware_test,  
         //那么JAVA调用层需要导入android.hardware.test包  
 {  
  return AndroidRuntime::registerNativeMethods(env, "android/test/Test",method_table,NELEM(method_table));  
 }  
}; 
第二步,把JNI代码注册函数添加到AndroidRuntime运行时库中。编辑frameworks/base/core/jni/AndroidRuntime.cpp文件:


Android SDK按照文件上的方法对应的增加以下两行:


..........  
extern int register_android_test(JNIEnv* env);//这是android_test.cpp文件中的注册函数  
..........................  
static const RegJNIRec gRegJNI[] = {  
.........  
.........  
REG_JNI(register_android_test),  
.........  
.......  
}; 
第三步,建立JAVA调用层,在frameworks/base/core/java/android/test 目录下建立文件Test.java,内容如下:


package android.test;  
public class Test {  
 private native int my_test();//Android 应用的代码中最终能调用的就是这个代码  
 public Test()//构造函数  
 {  
  //Add Your Code Here.............  
 }  
}; 
第四步,在packages/apps/test目录下建立你的android应用(具体建立方法这里就不再作介绍),假设test_app.java是应用的主文件,则内容如下:


package com.app;  
import android.test;//导入自己的包  
在代码中这样使用:  
test   test;  
test.my_test();//最终调用 
Android SDK JNI实现,Android SDK JNI C++代码,Android 调用C++,建立Android SDK下的JNI、JAVA应用完整步骤,Android JAVA调用C++代码。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
List of Sample Apps The list below provides a summary of the sample applications that are available with the Android SDK. Using the links on this page, you can view the source files of the sample applications in your browser. You can also download the source of these samples into your SDK, then modify and reuse it as you need. For more information, see Getting the Samples. API Demos A variety of small applications that demonstrate an extensive collection of framework topics. Backup and Restore A simple example that illustrates a few different ways for an application to implement support for the Android data backup and restore mechanism. Bluetooth Chat An application for two-way text messaging over Bluetooth. BusinessCard An application that demonstrates how to launch the built-in contact picker from within an activity. This sample also uses reflection to ensure that the correct version of the contacts API is used, depending on which API level the application is running under. Contact Manager An application that demonstrates how to query the system contacts provider using the ContactsContract API, as well as insert contacts into a specific account. Home A home screen replacement application. JetBoy A game that demonstrates the SONiVOX JET interactive music technology, with JetPlayer. Live Wallpaper An application that demonstrates how to create a live wallpaper and bundle it in an application that users can install on their devices. Lunar Lander A classic Lunar Lander game. Multiple Resolutions A sample application that shows how to use resource directory qualifiers to provide different resources for different screen configurations. Note Pad An application for saving notes. Similar (but not identical) to the Notepad tutorial. SampleSyncAdapter Demonstrates how an application can communicate with a cloud-based service and synchronize its data with data stored locally in a content provider. The sample uses two related parts of the Android framework — the account manager and the synchronization manager (through a sync adapter). Searchable Dictionary A sample application that demonstrates Android's search framework, including how to provide search suggestions for Quick Search Box. Snake An implementation of the classic game "Snake." Soft Keyboard An example of writing an input method for a software keyboard. Spinner A simple application that serves as an application-under-test for the SpinnerTest sample application. SpinnerTest An example test application that contains test cases run against the Spinner sample application. To learn more about the application and how to run it, please read the Activity Testing tutorial. TicTacToeLib An example of an Android library project that provides a game-play Activity to any dependent application project. For an example of how an application can use the code and resources in an Android library project, see the TicTacToeMain sample application. TicTacToeMain An example of an Android application that makes use of code and resources provided in an Android library project. Specifically, this application uses code and resources provided in the TicTacToeLib library project. Wiktionary An example of creating interactive widgets for display on the Android home screen. Wiktionary (Simplified) A simple Android home screen widgets example.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值