Delphi Android程序启动过程
activity->callbacks->onStart = onStart;
activity->instance = android_app_create(activity, savedState, savedStateSize);
// --------------------------------------------------------------------
// Native activity interaction (called from main thread)
// -------------------------------------------------------------------- function android_app_create(activity: PANativeActivity; savedState: Pointer; savedStateSize: size_t): Pandroid_app;
var
android_app: Pandroid_app;
PipeDescriptors: TPipeDescriptors;
attr: pthread_attr_t;
thread: pthread_t;
begin
android_app := Pandroid_app(__malloc(SizeOf(TAndroid_app)));
FillChar(android_app^, SizeOf(TAndroid_app), );
android_app^.activity := activity; pthread_mutex_init(android_app^.mutex, nil);
pthread_cond_init(android_app^.cond, nil);
if savedState <> nil then
begin
android_app^.savedState := __malloc(savedStateSize);
android_app^.savedStateSize := savedStateSize;
Move(PByte(savedState)^, PByte(android_app^.savedState)^, savedStateSize);
end; pipe(PipeDescriptors);
android_app^.msgread := PipeDescriptors.ReadDes;
android_app^.msgwrite := PipeDescriptors.WriteDes; pthread_attr_init(attr);
pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED);
pthread_create(thread, attr, @android_app_entry, android_app); pthread_mutex_lock(android_app^.mutex);
while android_app^.running = do
pthread_cond_wait(android_app^.cond, android_app^.mutex);
pthread_mutex_unlock(android_app^.mutex); Result := android_app;
end;
function android_app_entry(param: Pointer): Pointer; cdecl; // Delphi: init system unit and RTL.
procedure SystemEntry;
type
TMainFunction = procedure;
var
DlsymPointer: Pointer;
EntryPoint: TMainFunction;
begin
DlsymPointer := dlsym(RTLD_DEFAULT, '_NativeMain');
if DlsymPointer <> nil then
begin
EntryPoint := TMainFunction(DlsymPointer);
EntryPoint;
end;
end; var
android_app: Pandroid_app;
looper: PALooper;
begin
android_app := Pandroid_app(param);
android_app^.config := AConfiguration_new;//创建应用程序config
AConfiguration_fromAssetManager(android_app^.config, android_app^.activity^.assetManager); //从主线程获取消息用
android_app^.cmdPollSource.id := LOOPER_ID_MAIN;
android_app^.cmdPollSource.app := android_app;
android_app^.cmdPollSource.process := @process_cmd;//设置处理cmd的命令的函数
android_app^.inputPollSource.id := LOOPER_ID_INPUT;
android_app^.inputPollSource.app := android_app;
android_app^.inputPollSource.process := @process_input;//输入事件处理的函数 //创建一个looper消息循环,用来抓取消息
looper := ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
ALooper_addFd(looper, android_app^.msgread, LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT, nil, @android_app^.cmdPollSource);
android_app^.looper := looper; pthread_mutex_lock(android_app^.mutex);
android_app^.running := ; //设置,让线程从等待开启运行中退出,也就是从android_app_create中退出
pthread_cond_broadcast(android_app^.cond);
pthread_mutex_unlock(android_app^.mutex); { Delphi: this will initialize System and any RTL related functions, call unit initialization sections and then
project main code, which will enter application main loop. This call will block until the loop ends, which is
typically signalled by android_app^.destroyRequested. }
SystemEntry;//这里才是调用了工程文件的Begin End之间的代码,是Delphi的一个封装,实际上,在Android Native中调用的是android_main(android_app);然后再这里执行一些消息的处理,等待程序运行结束
{ This place would be ideal to call unit finalization, class destructors and so on. }
// Halt; android_app_destroy(android_app);//销毁android_app退出线程。这里实际上才是Android程序的终结 Result := nil;
end;
function android_app_read_cmd(android_app: Pandroid_app): ShortInt; cdecl;
var
cmd: ShortInt;
begin
Result := -;
if __read(android_app^.msgread, @cmd, sizeof(cmd)) = SizeOf(cmd) then
begin
case cmd of
APP_CMD_SAVE_STATE:
free_saved_state(android_app);//释放当前的保存状态
end;
Result := cmd;
end;
end; procedure android_app_pre_exec_cmd(android_app: Pandroid_app; cmd: ShortInt); cdecl; //准备执行命令状态
begin
case cmd of
APP_CMD_INPUT_CHANGED:
begin
pthread_mutex_lock(android_app^.mutex);
if android_app^.inputQueue <> nil then
AInputQueue_detachLooper(android_app^.inputQueue);
android_app^.inputQueue := android_app^.pendingInputQueue;
if android_app^.inputQueue <> nil then
AInputQueue_attachLooper(android_app^.inputQueue, android_app^.looper, LOOPER_ID_INPUT, nil,
@android_app^.inputPollSource);
pthread_cond_broadcast(android_app^.cond);
pthread_mutex_unlock(android_app^.mutex);
end; APP_CMD_INIT_WINDOW:
begin
pthread_mutex_lock(android_app^.mutex);
android_app^.window := android_app^.pendingWindow;
pthread_cond_broadcast(android_app^.cond);
pthread_mutex_unlock(android_app^.mutex);
end; APP_CMD_TERM_WINDOW:
pthread_cond_broadcast(android_app^.cond); APP_CMD_RESUME, APP_CMD_START, APP_CMD_PAUSE, APP_CMD_STOP:
begin
pthread_mutex_lock(android_app^.mutex);
android_app^.activityState := cmd;
pthread_cond_broadcast(android_app^.cond);
pthread_mutex_unlock(android_app^.mutex);
end; APP_CMD_CONFIG_CHANGED:
AConfiguration_fromAssetManager(android_app^.config, android_app^.activity^.assetManager); APP_CMD_DESTROY:
android_app^.destroyRequested := ;
end;
end; procedure android_app_post_exec_cmd(android_app: Pandroid_app; cmd: ShortInt); cdecl;
begin
case cmd of
APP_CMD_TERM_WINDOW:
begin
pthread_mutex_lock(android_app^.mutex);
android_app^.window := nil;
pthread_cond_broadcast(android_app^.cond);
pthread_mutex_unlock(android_app^.mutex);
end; APP_CMD_SAVE_STATE:
begin
pthread_mutex_lock(android_app^.mutex);
android_app^.stateSaved := ;
pthread_cond_broadcast(android_app^.cond);
pthread_mutex_unlock(android_app^.mutex);
end; APP_CMD_RESUME:
{ Delphi: It is unclear why this line is necessary in original AppGlue, but it prevents FireMonkey applications
from recovering saved state. FireMonkey recovers saved state usually after APP_CMD_INIT_WINDOW, which happens
much later after CMD_RESUME. }
{ free_saved_state(android_app) };
end;
end; procedure process_cmd(app: Pandroid_app; source: Pandroid_poll_source); cdecl;
var
cmd: ShortInt;
begin
cmd := android_app_read_cmd(app);//先读取命令
android_app_pre_exec_cmd(app, cmd);//准备命令
if Assigned(app^.onAppCmd) then //程序内部的指令处理
app^.onAppCmd(app, cmd);
android_app_post_exec_cmd(app, cmd);//执行处理
end;
这些代码将在Android消息循环中处理调用
然后程序进入工程文件的Begin End之间,先进入SysInit.pas单元的_InitExe,然后会调用GetThisModuleHandle,这个会调用dlopen(Info.dli_fname, RTLD_LAZY)就是相当于加载Windows的DLL,会先加载(Lib+程序名.so)获得当前句柄,然后dlClose关闭,最后程序以这个工程的库句柄作为程序的Hinstance,也就是说我们的好多资源应该会都打包到这个so中去,最后如果是Android环境,会调用_StartExe,来启动程序,_StartExe中会调用InitUnits来初始化一些单元,这里就会调用程序所引用到的各个单元的Initialization中的内容,在这个过程中会初始化FMX.PlatForm这个跨平台单元的TPlatformServices类库,本库是跨平台单元服务管理,然后就会调用FMX.PlatForm下的initialization,里面的RegisterCorePlatformServices会根据选择的平台来判定到底调用哪个平台下的RegisterCorePlatformServices,这个判定通过编译预处理指令在Implemention下的Use中
procedure TApplication.Run;
var
AppService: IFMXApplicationService;
begin
{$IFNDEF ANDROID}
AddExitProc(DoneApplication);
{$ENDIF}
FRunning := True;
try
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationService, AppService) then
AppService.Run;
finally
FRunning := False;
end;
end;
procedure TPlatformAndroid.Run;
begin
{ Although calling this routine is not really necessary, but it is a way to ensure that "Androidapi.AppGlue.pas" is
kept in uses list, in order to export ANativeActivity_onCreate callback. }
app_dummy; repeat
InternalProcessMessages;
until FAndroidApp^.destroyRequested <> ;
end;
Delphi Android程序启动过程的更多相关文章
- Android程序启动过程深入解析
当按下Android设备电源键时究竟发生了什么? Android的启动过程是怎么样的? 什么是Linux内核? 桌面系统linux内核与Android系统linux内核有什么区别? 什么是引导装载程序 ...
- Zygote和System进程的启动过程、Android应用进程启动过程
1.基本过程 init脚本的启动Zygote Zygote进程的启动 System进程的启动 Android应用进程启动过程 2.init脚本的启动 +------------+ +-------+ ...
- Envoy 源码分析--程序启动过程
目录 Envoy 源码分析--程序启动过程 初始化 main 入口 MainCommon 初始化 服务 InstanceImpl 初始化 启动 main 启动入口 服务启动流程 LDS 服务启动流程 ...
- Android应用程序启动过程源代码分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6689748 前文简要介绍了Android应用程 ...
- Android 应用程序启动过程源代码分析
本文转自:http://blog.csdn.net/luoshengyang/article/details/6689748 前文简要介绍了Android应用程序的Activity的启动过程.在And ...
- Android应用程序启动过程(二)分析
本文依据Android6.0源码,从点击Launcher图标,直至解析到MainActivity#OnCreate()被调用. Launcher简析 Launcher也是个应用程序,不过是个特殊的应用 ...
- Delphi开发 Android 程序启动画面简单完美解决方案
原文在这里 还是这个方法好用,简单!加上牧马人做的自动生成工具,更是简单. 以下为原文,向波哥敬礼! 前面和音儿一起研究 Android 下启动画面的问题,虽然问题得到了解决,但是,总是感觉太麻烦,主 ...
- Android应用程序启动过程(一)总结
一.App启动方式 1,冷启动 冷启动:当启动应用时,后台没有该应用的进程,这时系统会重新创建一个新的进程分配给该应用. 冷启动的特点:因为系统会重新创建一个新的进程分配给它,所以会创建和初始化App ...
- 比较windows phone程序启动和android程序启动原理
windows phone 程序是如何启动的了,他和android程序有什么区别,我们重点从native code 层面来分析 在windows phone 程序启动的时候是: 在XAML中使用应用程 ...
随机推荐
- module中build.gradle文件参数含义
主要是module的build.gradle,截图如下: 01行:apply plugin: 'com.android.application' 表示该module是这个应用程序的module ...
- Android性能优化方法(八)
Android SDK tools目录下提供一个观察布局的工具,层级观察器(Hierarchy Viewer).Hierarchy Viewer工具是一个非常好的布局优化工具,同时,你也可以通过它学习 ...
- delphi 处理图片(剪切,压缩)
剪切bmp:效果为指定的rect大小,若图片比rect小,则会放大. 都要uses Vcl.Imaging.jpeg; 需要注意的是FMX里也需要jpeg的支持,虽然没引用编译器不会报错,但用到jpg ...
- 多重背包问题:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(HDU 2191)(二进制优化)
悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 HDU 2191 一道裸的多重背包问题: #include<iostream> #include<algorithm> #i ...
- CentOS 一个网卡设置多个IP
方法1:少量IP手动绑定: (这里以绑定IP到eth0为例,其它网卡的话修改相应的文件名即可) 1.复制ifcfg-eth0的网卡配置文件并改名为ifcfg-eth0:0 [root@akinlau ...
- linux-4 虚拟机安装VMwareTOOls工具包
第一步:在虚拟机中选择“安装.重新安装VMwareTools(T)” 第2步: 安装VMwareTools包 1.用root登录 2. 创建 /media/cdrom [root@localhos ...
- 对象池与.net—从一个内存池实现说起
本来想写篇关于System.Collections.Immutable中提供的ImmutableList里一些实现细节来着,结果一时想不起来源码在哪里--为什么会变成这样呢--第一次有了想写分析的源码 ...
- 如何使不同主机上的docker容器互相通信
docker启动时,会在宿主主机上创建一个名为docker0的虚拟网络接口,默认选择172.17.42.1/16,一个16位的子网掩码给容器提供了65534个IP地址.docker0只是一个在绑定到这 ...
- [ucgui] 对话框6——触屏位置简单例子
>_<:直接调用函数获得触屏位置: xPhys = GUI_TOUCH_GetxPhys(); /* Get the A/D mesurement result in x */ yPhys ...
- 插件~使用ECharts动态在地图上标识点~动态添加和删除标识点
之前写过一个Echarts的文章,没有基础的同学可以先看这<上一篇>,对于一个地图和说,我们在初始化之后,你可能被在地图上标识出一些点,当然这根据你的业务去标识,而如果每次更新数据都加载全 ...