通过系统框架的JNI读取文件例子-附源码

 android的JNI开发有两种,一种是基于应用程序的,一种是基于系统框架的。

基于应用程序的比较简单,之前已经有所了解,今天实践了一下基于系统框架的JNI读取文件的操作。


效果图:


总体框架:

1.应用程序调用API

2.JAVA层编写API接口,调用JNI层函数

3.JNI实现读取文件的操作,并根据要求返回读取的文件


主要层次为:

1.应用程序调用API

try {    
  1.     String mstr = helloService.getText();   //api for read file  
  2.   
  3.     new AlertDialog.Builder(JniTestActivity.this)  
  4.     .setMessage(mstr)  
  5.     .show();  
  6.     Log.i(JAVA_DEBUG, "done");        
  7. catch (RemoteException e) {    
  8.     Log.e(JAVA_DEBUG, "Remote Exception while reading value from helloService.sayHello().");    
  9. }   

2.JAVA层编写API接口,调用JNI层函数

aidl文件,定义接口规范:

package android.os;    
  1.      
  2. interface IHelloService {    
  3.     String getText();  
  4. }   
java文件,指定native函数来实现接口的功能

package com.android.server;    
  1. import android.content.Context;    
  2. import android.os.IHelloService;    
  3. import android.util.Slog;    
  4. public class HelloService extends IHelloService.Stub {    
  5.     private static final String TAG = "HelloService";    
  6.   
  7.     public String getText() {  
  8.         return getText_native();  
  9.         }  
  10.         
  11.     private static native String getText_native();  
  12. };  

3.JNI实现读取文件的操作,并根据要求返回读取的文件

#define LOG_TAG "HelloService"    
  1. #include "jni.h"    
  2. #include "JNIHelp.h"    
  3. #include "android_runtime/AndroidRuntime.h"    
  4. #include <utils/misc.h>    
  5. #include <utils/Log.h>    
  6. #include <stdio.h>   
  7.   
  8. #include <fcntl.h>  
  9.   
  10. namespace android    
  11. {    
  12.     #define TEXT_SUPPLY_PATH "/system/usr/getText.txt"  
  13.     #define PATH_MAX_LEN 4096  
  14.   
  15.     static int readFromFile(const char* path, char* buf, size_t size);  
  16.   
  17.     static jstring hello_getText(JNIEnv* env, jobject clazz) {    
  18.   
  19.     char path[PATH_MAX_LEN];   
  20.     char buf[50];  
  21.   
  22.         LOGI("Hello JNI: get text from device.");    
  23.   
  24.     snprintf(path, sizeof(path), "%s", TEXT_SUPPLY_PATH);  
  25.        int length = readFromFile(path, buf, sizeof(buf));  
  26.     LOGI("Hello JNI: file length = %d.", length);    
  27.   
  28.     return (env)->NewStringUTF(buf);  
  29.     }    
  30.   
  31.     static int readFromFile(const char* path, char* buf, size_t size)  
  32.     {  
  33.         if (!path)  
  34.             return -1;  
  35.         int fd = open(path, O_RDONLY, 0);  
  36.         if (fd == -1) {  
  37.             LOGE("Could not open '%s'", path);  
  38.             return -1;  
  39.         }  
  40.           
  41.         size_t count = read(fd, buf, size);  
  42.         if (count > 0) {  
  43.             count = (count < size) ? count : size - 1;  
  44.             while (count > 0 && buf[count-1] == '\n') count--;  
  45.             buf[count] = '\0';  
  46.         } else {  
  47.             buf[0] = '\0';  
  48.         }   
  49.   
  50.         close(fd);  
  51.         return count;  
  52.     }  
  53.                
  54.     static const JNINativeMethod method_table[] = {     
  55.         {"getText_native""()Ljava/lang/String;", (void*)hello_getText},    
  56.     };    
  57.   
  58.     int register_android_server_HelloService(JNIEnv *env) {    
  59.             return jniRegisterNativeMethods(env, "com/android/server/HelloService", method_table, NELEM(method_table));    
  60.     }    
  61. };    

注意事先把文件准备把,存放的路径为:TEXT_SUPPLY_PATH "/system/usr/getText.txt" 

主要是以上3个层次,省略了*.mk文件的修改细节,了解原理即可。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值