android 纯c/c++开发(转)
转载自: http://jingyan.baidu.com/article/a501d80cf394dfec630f5e85.html
android 自ndk r8出来以后,就开始支持纯c/c++开发,android 的纯 c/c++ 开发更有些想 win32 开发,只不过是 WinMain 变成了 android_main, 消息处理函数变成了两个,下面开始详细的介绍如何进行纯 c/c++开发,里面附带一个多点触屏的例子,希望对大家有用,谢谢!
工具/原料
- win7 x64
- jdk1.8.0_11
- adt-bundle-windows-x86_64-20140702
- android-ndk-r10
新建一个Natvie工程
- 1
打开eclipse;
- 2
打开菜单->File->New->Android Application;

- 3
设置工程名,sdk版本,注意:主题设置为 None,点击next;

- 4
Configure Project 是取消 Create activity 的复选框,点击next;

- 5
Configure the attributes of the icon set, 直接点击 next;

- 6
Select whether to create an activity, and if so, what kind of activity. 点击 finish即可;

- 7
工程便创建出来了
END
配置Makefile
- 1
右键工程NativeTest->弹出菜单->Android Tools->Add Native Support...

- 2
Settings for generated native components for project.界面 直接点击Finish


- 3
将 android.mk 的内容补充完整:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := NativeTest
LOCAL_SRC_FILES := NativeTest.cpp
LOCAL_LDLIBS := -llog -landroid
LOCAL_STATIC_LIBRARIES := android_native_app_glue
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)

- 4
增加一个 Application.mk 文件(这一步可选),并写入:
APP_ABI := x86
APP_CPPFLAGS := --std=c++11
NDK_TOOLCHAIN_VERSION := 4.8
END
代码部分
- 1
1、android_main:这个函数类似于win32开发的WinMain函数
2、app->onAppCmd = onAppCmd;
app->onInputEvent = onInputEvent;
类似于win32中设置窗口的回掉函数
3、
while ((ident=ALooper_pollAll(-1, NULL, &events,
(void**)&source)) >= 0) {
// Process this event.
if (source != NULL) {
source->process(app, source);
}
// Check if we are exiting.
if (app->destroyRequested != 0) {
return;
}
}
这一段类似于win32的消息循环
4、为了方便大家粘贴,android_main 函数的代码如下:
void android_main(struct android_app* app) {
// Make sure glue isn't stripped.
app_dummy();
app->onAppCmd = onAppCmd;
app->onInputEvent = onInputEvent;
while (1) {
int ident;
int events;
struct android_poll_source* source;
while ((ident=ALooper_pollAll(-1, NULL, &events,
(void**)&source)) >= 0) {
// Process this event.
if (source != NULL) {
source->process(app, source);
}
// Check if we are exiting.
if (app->destroyRequested != 0) {
return;
}
}
}
}

- 2
onAppCmd 描述的是真个activity的生命周期,类似于win32开发的消息处理回掉函数:
static void onAppCmd(struct android_app* app, int32_t cmd) {
switch (cmd) {
case APP_CMD_SAVE_STATE:
// The system has asked us to save our current state. Do so.
__android_log_print(ANDROID_LOG_DEBUG, "fuke", "engine_handle_cmd APP_CMD_SAVE_STATE");
break;
case APP_CMD_INIT_WINDOW:
// The window is being shown, get it ready.
__android_log_print(ANDROID_LOG_DEBUG, "fuke", "engine_handle_cmd APP_CMD_INIT_WINDOW");
break;
case APP_CMD_TERM_WINDOW:
__android_log_print(ANDROID_LOG_DEBUG, "fuke", "engine_handle_cmd APP_CMD_TERM_WINDOW");
break;
case APP_CMD_GAINED_FOCUS:
// When our app gains focus, we start monitoring the accelerometer.
__android_log_print(ANDROID_LOG_DEBUG, "fuke", "engine_handle_cmd APP_CMD_GAINED_FOCUS");
break;
case APP_CMD_LOST_FOCUS:
// When our app loses focus, we stop monitoring the accelerometer.
// This is to avoid consuming battery while not being used.
__android_log_print(ANDROID_LOG_DEBUG, "fuke", "engine_handle_cmd APP_CMD_LOST_FOCUS");
break;
}
}

- 3
onInputEvent 主要是用来触屏相关事件,也类似于win32开发的消息处理回掉函数,函数有两部分组成:
1、检测多点触屏,并通过logcat打印出多点触屏的信息;
2、控制屏幕颜色变化,每次松开手时颜色变化
3、为方便大家粘贴,onInputEvent函数的代码记录如下:
static int32_t onInputEvent(struct android_app* app, AInputEvent* event) {
if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
int nNum = AMotionEvent_getPointerCount(event);
char szTrace[1024] = {0};
sprintf (szTrace, "engine_handle_input num=[%d]", nNum);
for (int nIdx = 0; nIdx < nNum; nIdx++)
{
int nX = AMotionEvent_getX(event, 0);
int nY = AMotionEvent_getY(event, 0);
sprintf (strrchr(szTrace, 0), " (%d %d)", nX, nY);
}
__android_log_print(ANDROID_LOG_DEBUG, "colorspace",
"%s", szTrace);
if (AKeyEvent_getAction(event) != AKEY_EVENT_ACTION_UP)
return 1;
ANativeWindow_Buffer nativeWindow = {0};
int nRet = ANativeWindow_lock(app->pendingWindow, &nativeWindow, NULL);
int nArea = nativeWindow.width * nativeWindow.height;
unsigned long* pdwScreen = (unsigned long*)nativeWindow.bits;
static int s_nClr = 0;
unsigned long pdwClr[] = {
0x00000000, 0x000000ff, 0x0000ffff, 0x0000ff00,
0x00ffff00, 0x00ff0000, 0x00ff00ff, 0x00ffffff};
s_nClr ++;
if (s_nClr > sizeof(pdwClr) / sizeof(unsigned long))
s_nClr = 0;
for (int nIdx = 0; nIdx < nArea; nIdx++)
{
pdwScreen[nIdx] = pdwClr[s_nClr];
}
ANativeWindow_unlockAndPost(app->pendingWindow);
return 1;
}
return 0;
}

END
设置工程属性
- 1
1、打开 AndroidManifest.xml
2、打开 Application 分页
3、增加一个 Activity
如下所示:

- 2
1、选择右边的 Browse;
2、取消 "Display classes from sources of ..." 前面的复选框;
3、在搜索栏输入"na",选中列出来的 "NativeActivity"
4、点击OK
效果如下:


- 3
1、选中 android.app.nativeActivity
2、点击 add
3、选择 Meta Data
4、点击Ok

- 4
输入:
android:name="android.app.lib_name"
android:value="NativeTest"

- 5
1、选中 android.app.nativeActivity
2、点击 add
3、选择 Intent Filter
4、点击Ok

- 6
1、选中 Intent Filter
2、点击 add
3、选择 Action
4、点击Ok
5、设置 android:name="android.intent.action.MAIN"


- 7
1、选中 Intent Filter
2、点击 add
3、选择 Category
4、点击Ok
5、设置 android:name="android.intent.category.LAUNCHER"

END
运行
- 1
启动模拟器,运行效果如下:

- 2
点击后效果:



- 3
这次整个程序完成
END
总结
- 1
整个程序实现:
1、android 下面的纯c/c++ 开发
2、实现了多点触屏的功能
3、实现了点击屏幕颜色的切换功能
- 2
源代码百度云链接:http://pan.baidu.com/s/1kTokdL1 密码:fmod
#HUABAN_WIDGETS .HUABAN-red-normal-icon-button, .HUABAN-red-large-icon-button, .HUABAN-red-small-icon-button, .HUABAN-white-normal-icon-button, .HUABAN-white-large-icon-button, .HUABAN-white-small-icon-button { background-image: url({{imgBase}}/widget_icons_ie6.png)
android 纯c/c++开发(转)的更多相关文章
- Android原生(Native)C开发之四:SDL移植笔记
http://www.apkbus.com/forum.php?mod=viewthread&tid=1989 SDL(Simple DirectMedia Layer)是一套开放源码的跨平台 ...
- Android与Swift iOS开发:语言与框架对比
Swift是现在Apple主推的语言,2014年新推出的语言,比Scala等“新”语言还要年轻10岁.2015年秋已经开源.目前在linux上可用,最近已经支持Android NDK:在树莓派上有Sw ...
- 详细对比IB开发与纯手码开发的优劣。
1.IB是什么? Interface Builder 是一种通过图形化界面搭建UI的方式,并把窗口.菜单栏以及窗口上的各种控件的对象都“冻结”在了一个 NIB文档里:程序运行时,这些对象将会“苏醒”. ...
- 【Android UI设计与开发】第05期:引导界面(五)实现应用程序只启动一次引导界面
[Android UI设计与开发]第05期:引导界面(五)实现应用程序只启动一次引导界面 jingqing 发表于 2013-7-11 14:42:02 浏览(229501) 这篇文章算是对整个引导界 ...
- 总结android项目的基本开发步骤(转帖)
总结android项目的基本开发步骤(转帖) 做了几个android企业应用项目后,总结了项目的基本开发步骤,希望能够交流.一 应用规划: ※确定功能. ※必须的界面及界面跳转的流程. ...
- Eclipse+ADT+Android SDK 搭建安卓开发环境
Eclipse+ADT+Android SDK 搭建安卓开发环境 要求 必备知识 windows 7 基本操作. 运行环境 windows 7(64位); eclipse-jee-luna-SR2 ...
- android音乐播放器开发教程
android音乐播放器开发教程 Android扫描sd卡和系统文件 Android 关于录音文件的编解码 实现米聊 微信一类的录音上传的功能 android操作sdcard中的多媒体文件——音乐列表 ...
- Android腾讯微博开发之随机字符串与签名实现
Android腾讯微博开发入门之随机字符串与签名实现 直接上代码 1.Utils类,包括签名和随机字符串 import java.util.Random; import javax.cry ...
- Android系统Google Maps开发实例浅析
Google Map(谷歌地图)是Google公司提供的电子地图服务.包括了三种视图:矢量地图.卫星图片.地形地图.对于Android系统来说,可以利用Google提供的地图服务来开发自己的一些应用. ...
随机推荐
- Mysql性能监控
show processlist; show global variables like 'max_allowed_packet'; // QPS计算(每秒查询数)show global status ...
- 开机提示grub可咋办啊
导读 GRUB是多启动规范的实现,它允许用户可以在计算机内同时拥有多个操作系统,并在计算机启动时选择希望运行的操作系统.GRUB可用于选择操作系统分区上的不同内核,也可用于向这些内核传递启动参数. 1 ...
- L18 如何快速查找文档获得帮助
原地址:http://www.howzhi.com/course/286/lesson/2121 查找文档快速 苹果提供了丰富的文档,以帮助您成功构建和部署你的应用程序,包括示例代码,常见问题解答,技 ...
- nginx: [emerg] getpwnam(“www”) failed
在配置nginx 时提示如下错误时:nginx: [emerg] getpwnam(“www”) failed 解决方案一 在nginx.conf中 把user nobody的注释去掉既可 解决方案二 ...
- Redis学习手册(Sorted-Sets数据类型)
一.概述: Sorted-Sets和Sets类型极为相似,它们都是字符串的集合,都不允许重复的成员出现在一个Set中.它们之间的主要差别是Sorted-Sets中的每一个成员都会有一个分数(score ...
- iOS 定制controller过渡动画 ViewController Custom Transition使用体会
最近学习了一下ios7比较重要的一项功能,就是 controller 的 custom transition. 在ios7中,navigation controller 中就使用了交互式过渡来返回上级 ...
- css form 表单组对齐
2014年7月1日 15:31:17 第一次写css,见谅 css: .form-box .form-group .form-label {text-align: right; width: 200p ...
- sphinx 增量索引与主索引使用测试
2013年10月28日 15:01:16 首先对新增的商品建立增量索引,搜索时只使用增量索引: array (size=1) 0 => array (size=6) 'gid' => st ...
- Linux系统排查3——I/O篇
当磁盘无法写入的时候,一般有以下可能: 文件系统只读 磁盘已满 I节点使用完 一. 遇到只读的文件系统 文件系统自动设置成只读可能是系统自我保护的一种机制,因此需要实现弄清究竟是什么原因造成了文件系统 ...
- Java for LeetCode 073 Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 解题思路: ...