|
Android架构分析之基于Android系统的C应用程序开发
分类: Android架构分析 2013-01-14 17:31 206人阅读 评论(0) 收藏 举报
作者:刘昊昱
博客:http://blog.csdn.net/liuhaoyutz
Android版本:2.3.7_r1
Linux内核版本:android-goldfish-2.6.29
本文介绍基于Android系统的C应用程序开发。我们知道,Android应用程序开发使用的是JAVA语言,但有时候我们也需要一些基于命令行的小程序,这些小程序一般使用C语言开发,程序的写法与PC平台的C程序没有区别,但需要把C程序注册到Android系统中,使其能在Android平台上运行。在这篇博客中,我们就写一个基于Android平台的C应用程序,该程序用来访问我们在上篇博客中创建的底层Linux驱动程序example。
创建development/example_test/example_test.c文件,其代码如下:
[cpp] view plaincopy
1#include <stdio.h>
2#include <unistd.h>
3#include <fcntl.h>
4#include <string.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7
8#define DEVICE_FILE "/dev/example"
9
10int main(int argc, char *argv[])
11{
12 int ret = 0, num = 12, val = 0, fd = 0;
13
14 fd = open(DEVICE_FILE, O_RDWR);
15 if(fd < 0)
16 {
17 printf("open device error!\n");
18 return -1;
19 }
20
21 ret = write(fd, &num, sizeof(int));
22 if(ret < 0)
23 {
24 printf("write device error!\n");
25 return -1;
26 }
27
28 printf("write val %d to device!\n", num);
29
30 ret = read(fd, &val, sizeof(int));
31 if(ret < 0)
32 {
33 printf("read device error!\n");
34 return -1;
35 }
36
37 printf("read val = %d\n", val);
38
39 return 0;
40}
这个C程序打开/dev/example,向该设备寄存器写入一个数字12,再读取设备寄存器,所以读取得到的值应该也是12。 |
|