{"id":2369,"date":"2015-04-27T16:01:14","date_gmt":"2015-04-27T08:01:14","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=2369"},"modified":"2015-04-27T16:09:20","modified_gmt":"2015-04-27T08:09:20","slug":"%e8%bd%ac%e4%b8%8d%e5%be%97%e9%97%b2-delphi-android%e7%a8%8b%e5%ba%8f%e5%90%af%e5%8a%a8%e8%bf%87%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=2369","title":{"rendered":"[\u8f6c]\u4e0d\u5f97\u95f2-Delphi Android\u7a0b\u5e8f\u542f\u52a8\u8fc7\u7a0b"},"content":{"rendered":"<p><script src=\"\/\/www.google-analytics.com\/analytics.js\" async=\"\"><\/script><\/p>\n<p>\u8f6c\u81ea\u4e0d\u5f97\u95f2\u5728cnblog\u7684\u535a\u5ba2\uff1a<a href=\"http:\/\/www.cnblogs.com\/DxSoft\/p\/4460236.html\" target=\"_blank\">Delphi Android\u7a0b\u5e8f\u542f\u52a8\u8fc7\u7a0b\u00a0<\/a><script src=\"http:\/\/common.cnblogs.com\/script\/encoder.js\" type=\"text\/javascript\"><\/script><script src=\"http:\/\/www.googletagservices.com\/tag\/js\/gpt.js\" async=\"\" type=\"text\/javascript\"><\/script><script src=\"http:\/\/common.cnblogs.com\/script\/jquery.js\" type=\"text\/javascript\"><\/script><script type=\"text\/javascript\">\/\/ <![CDATA[\nvar currentBlogApp = 'DxSoft', cb_enable_mathjax=true;\n\/\/ ]]><\/script><script src=\"\/bundles\/blog-common.js?v=C7PSZcWNET_Um_X0jLClvw3B2fe8Q5iIyhiIzfhWQtI1\" type=\"text\/javascript\"><\/script><script type=\"text\/x-mathjax-config;executed=true\">\/\/ <![CDATA[\nMathJax.Hub.Config({\n  tex2jax: { inlineMath: [['$','$'], ['\\\\(','\\\\)']] },\n  TeX: { equationNumbers: { autoNumber: ['AMS'], useLabelIds: true } },\n  'HTML-CSS': { linebreaks: { automatic: true } },\n  SVG: { linebreaks: { automatic: true } }});\n\/\/ ]]><\/script><script src=\"http:\/\/mathjax.b0.upaiyun.com\/MathJax.js?config=TeX-AMS-MML_HTMLorMML\" type=\"text\/javascript\"><\/script><script src=\"http:\/\/partner.googleadservices.com\/gpt\/pubads_impl_60.js\" async=\"\" type=\"text\/javascript\"><\/script><script src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/osd.js\" type=\"text\/javascript\"><\/script><\/p>\n<div id=\"centercontent\">\n<div class=\"post\">\n<div id=\"cnblogs_post_body\">\n<div>Delphi\u7684Android\u7a0b\u5e8f\u662f\u539f\u751f\u7684\u7a0b\u5e8f\uff0c\u4e5f\u5c31\u662fNativeActivity\u3002\u90a3\u4e48\u5c31\u9700\u8981\u5148\u770b\u4e00\u4e0bNativeActivity\u7684\u539f\u7406\uff0c<\/div>\n<div>\u5728AndroidManifest.xml\u6587\u4ef6\u91cc\u9762\u6307\u5b9a\u5165\u53e3activity\u4e3anativeactivity,\u8fd9\u6837\u5e94\u7528\u7a0b\u5e8f\u4e00\u542f\u52a8\uff0cjava\u865a\u62df\u673a\u8fd9\u8fb9\u5c31\u5f00\u4e00\u4e2a\u4e3b\u7ebf\u7a0b\uff0c\u4e3b\u7ebf\u7a0b\u521b\u5efa\u4e00\u4e2a\u6d3b\u52a8\uff0c\u5c31\u662fnativeactivity\uff0c\u8fd9\u4e2anativeactivity\u5728\u521b\u5efa\u7684\u8fc7\u7a0b\u4e2d\u5c31\u4f1a\u53bb\u5e94\u7528\u7a0b\u5e8f\u7684.so\u52a8\u6001\u94fe\u63a5\u5e93\u4e2d\u5bfb\u627e\u4e00\u4e2a\u51fd\u6570\uff1a<\/div>\n<div>__ANativeActivity_onCreate(ANativeActivity, void* size_t)\uff0c\u7136\u540e\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u5c31\u662fC++\u4ee3\u7801\u4e2d\u7684\u771f\u6b63\u7684\u5165\u53e3\u51fd\u6570\uff0c\u5728\u8fd9\u4e2a\u5165\u53e3\u51fd\u6570\u91cc\u9762<\/div>\n<div>\u505a\u4e9b\u4ec0\u4e48\u4e8b\u60c5\u5462\uff0c\u8bf7\u53c2\u8003ndk\u91cc\u9762\u7684Native_app_glue\u3002\u5b83\u662f\u8fd9\u6837\u6765\u5b9e\u73b0\u7684\uff1a\u5bf9\u8fd9\u4e2a\u4f20\u8fdb\u6765\u7684ANativeActivity, \u8bbe\u7f6e\u8fd9\u4e2aactivity\u7684\u5404\u79cd\u662f\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\uff1a<\/div>\n<div>activity-&gt;callbacks-&gt;onDestroy = onDestroy;<br \/>\n activity-&gt;callbacks-&gt;onStart = onStart;<\/div>\n<div>\u8bbe\u7f6e\u5b8c\u4e86\u4e4b\u540e\u5c31\u8c03\u7528\uff1a<br \/>\n activity-&gt;instance = android_app_create(activity, savedState, savedStateSize);<\/div>\n<div>\u8fd9\u4e2a\u5728Delphi\u7684Androidapi.AppGlue\u5355\u5143\u4e2d\u5b9e\u73b0\uff0c\u8fd9\u4e2a\u5c31\u662fDelphi\u4e0b\u7684Android NDK\u4e00\u4e9b\u5bf9\u5e94\u7684\u7b80\u5355\u5c01\u88c5\u3002Delphi\u4e2d\u5728\u8fd9\u4e2a\u5355\u5143\u4e2d\u5bfc\u51fa\u4e86\u4e00\u4e2aANativeActivity_onCreate\u5c31\u662f\u524d\u9762\u4ecb\u7ecd\u7684__ANativeActivity_onCreate\uff0c\u8fd9\u4e2a\u51fd\u6570\u5c31\u76f8\u5f53\u4e8e\u662fDelphi\u7684Android\u8fd0\u884c\u7a0b\u5e8f\u7684\u5165\u53e3\u51fd\u6570\uff0c\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2dDelphi\u4fdd\u5b58\u4e86\u4e00\u4e2aDelphiActivity\uff0c\u7528\u6765\u4fdd\u5b58\u8fd9\u4e2aActivity\u7ed3\u6784\u3002Android\u7684\u5165\u53e3\u4f4d\u7f6e\u4e0d\u518d\u662fDelphi\u7684\u5de5\u7a0b\u6587\u4ef6\u4f4d\u7f6e\u7684Begin \u00a0end\u4e4b\u95f4\u7684\u4ee3\u7801\u3002\u8fd9\u4e2a\u51fd\u6570\u88ab\u6253\u5305\u5230Lib+\u5de5\u7a0b\u540d.So\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u4f5c\u4e3a\u4e00\u4e2a\u5230\u5904\u51fd\u6570\uff0c\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019\u4f1a\u52a0\u8f7d\u8fd9\u4e2aSo\u52a8\u6001\u5e93\uff0c\u7136\u540e\u52a0\u8f7dANativeActivity_onCreate\u6267\u884cAndroid\u5165\u53e3\u3002\u6b64\u8fc7\u7a0b\u8c03\u7528android_app_create\uff0c\u540c\u6837\u5728Androidapi.AppGlue\u5355\u5143\u4e2d\u3002\u8fd9\u4e2a\u4ee3\u7801\u5982\u4e0b\uff1a<\/div>\n<div>\n<pre class=\"lang:delphi decode:true \">\/\/ --------------------------------------------------------------------\r\n\/\/ Native activity interaction (called from main thread)\r\n\/\/ --------------------------------------------------------------------\r\n \r\nfunction android_app_create(activity: PANativeActivity; savedState: Pointer; savedStateSize: size_t): Pandroid_app;\r\nvar\r\nandroid_app: Pandroid_app;\r\nPipeDescriptors: TPipeDescriptors;\r\nattr: pthread_attr_t;\r\nthread: pthread_t;\r\nbegin\r\nandroid_app := Pandroid_app(__malloc(SizeOf(TAndroid_app)));\r\nFillChar(android_app^, SizeOf(TAndroid_app), 0);\r\nandroid_app^.activity := activity;\r\n \r\npthread_mutex_init(android_app^.mutex, nil);\r\npthread_cond_init(android_app^.cond, nil);\r\nif savedState &lt;&gt; nil then\r\nbegin\r\nandroid_app^.savedState := __malloc(savedStateSize);\r\nandroid_app^.savedStateSize := savedStateSize;\r\nMove(PByte(savedState)^, PByte(android_app^.savedState)^, savedStateSize);\r\nend;\r\n \r\npipe(PipeDescriptors);\r\nandroid_app^.msgread := PipeDescriptors.ReadDes;\r\nandroid_app^.msgwrite := PipeDescriptors.WriteDes;\r\n \r\npthread_attr_init(attr);\r\npthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED);\r\npthread_create(thread, attr, @android_app_entry, android_app);\r\n \r\npthread_mutex_lock(android_app^.mutex);\r\nwhile android_app^.running = 0 do\r\npthread_cond_wait(android_app^.cond, android_app^.mutex);\r\npthread_mutex_unlock(android_app^.mutex);\r\n \r\nResult := android_app;\r\nend;<\/pre>\n<p><script src=\"\/\/www.google-analytics.com\/analytics.js\" async=\"\" type=\"mce-mce-mce-mce-mce-mce-no\/type\"><\/script>Delphi Android\u7a0b\u5e8f\u542f\u52a8\u8fc7\u7a0b &#8211; \u4e0d\u5f97\u95f2 &#8211; \u535a\u5ba2\u56ed<script src=\"http:\/\/common.cnblogs.com\/script\/encoder.js\" type=\"mce-mce-mce-mce-mce-mce-text\/javascript\"><\/script><script src=\"http:\/\/www.googletagservices.com\/tag\/js\/gpt.js\" async=\"\" type=\"mce-mce-mce-mce-mce-mce-text\/javascript\"><\/script><script src=\"http:\/\/common.cnblogs.com\/script\/jquery.js\" type=\"mce-mce-mce-mce-mce-mce-text\/javascript\"><\/script><script type=\"mce-mce-mce-mce-mce-mce-text\/javascript\">\/\/ <![CDATA[\nvar currentBlogApp = 'DxSoft', cb_enable_mathjax=true;\n\/\/ ]]><\/script><script src=\"\/bundles\/blog-common.js?v=C7PSZcWNET_Um_X0jLClvw3B2fe8Q5iIyhiIzfhWQtI1\" type=\"mce-mce-mce-mce-mce-mce-text\/javascript\"><\/script><script type=\"mce-mce-mce-mce-mce-mce-text\/x-mathjax-config;executed=true\">\/\/ <![CDATA[\nMathJax.Hub.Config({\n  tex2jax: { inlineMath: [['$','$'], ['\\\\(','\\\\)']] },\n  TeX: { equationNumbers: { autoNumber: ['AMS'], useLabelIds: true } },\n  'HTML-CSS': { linebreaks: { automatic: true } },\n  SVG: { linebreaks: { automatic: true } }});\n\/\/ ]]><\/script><script src=\"http:\/\/mathjax.b0.upaiyun.com\/MathJax.js?config=TeX-AMS-MML_HTMLorMML\" type=\"mce-mce-mce-mce-mce-mce-text\/javascript\"><\/script><script src=\"http:\/\/partner.googleadservices.com\/gpt\/pubads_impl_60.js\" async=\"\" type=\"mce-mce-mce-mce-mce-mce-text\/javascript\"><\/script><script src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/osd.js\" type=\"mce-mce-mce-mce-mce-mce-text\/javascript\"><\/script><\/p>\n<div id=\"centercontent\">\n<div class=\"post\">\n<div id=\"cnblogs_post_body\">\n<div>\u53ef\u89c1\u5728\u524d\u9762\u53c8\u6ce8\u91ca\uff0c\u5199\u4e86Native activity interaction (called from main thread)\uff0c\u8bf4\u660e\u8fd9\u4e2a\u662f\u4e3b\u7ebf\u7a0b\u8c03\u7528\u7684\u3002\u7a0b\u5e8f\u9996\u5148\u5148\u521b\u5efa\u4e86\u4e00\u4e2aandroid_app\u7ed3\u6784\u4f53\uff0c\u7136\u540e\u8bbe\u7f6e app\u7684activity\u3002<\/div>\n<div>\n<div>pthread_mutex_init(android_app^.mutex, nil);\/\/\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u540c\u6b65\u5bf9\u8c61 mutex\u4e92\u65a5\u4f53\uff0c<\/div>\n<div>pthread_cond_init(android_app^.cond, nil);\/\/\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u901a\u4fe1\u7684\u5bf9\u8c61\u3002\u7528\u4e8e\u4e3b\u7ebf\u7a0b\uff08UI\u7ebf\u7a0b\uff09\u548c\u6211\u4eec\u7684\u7ebf\u7a0b\u901a\u4fe1\u3002<\/div>\n<div>\u7136\u540e\u68c0\u67e5\u770b\u770bandroid\u7cfb\u7edf\u4e4b\u524d\u662f\u5426\u5df2\u7ecf\u4e3a\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u4fdd\u5b58\u8fc7\u72b6\u6001\u3002\u6709\u7684\u8bdd\u76f4\u63a5\u6062\u590d\u5c31\u597d\u4e86\u3002\u53e6\u5916\u6bd4\u8f83\u91cd\u8981\u7684\u662fandroid\u5e94\u7528\u7a0b\u5e8f\u7684\u5c4f\u5e55\u65b9\u5411\u53d8\u5316\u7684\u8bdd\uff0cactivity\u4e5f\u8981\u4ece\u65b0\u5efa\u7acb\uff01\uff01\uff01\uff01\uff01<\/div>\n<div>\u7136\u540e\u521b\u5efa\u4e24\u4e2a\u7ba1\u9053\u5bf9\u8c61\uff0c\u4e00\u4e2a\u8ba9\u7ebf\u7a0b\u7528\u6765\u8bfb\u53d6\u6d88\u606f\uff0c\u4e00\u4e2a\u7528\u6765\u5199\u5165\u6d88\u606f<\/div>\n<div>\n<div>pipe(PipeDescriptors);<\/div>\n<div>android_app^.msgread \u00a0:= PipeDescriptors.ReadDes; \/\/\u8bfb\u53d6\u6d88\u606f<\/div>\n<div>android_app^.msgwrite := PipeDescriptors.WriteDes; \/\/\u5199<\/div>\n<\/div>\n<div>\u7136\u540e\u521b\u5efa\u7ebf\u7a0b\uff0c\u8fd0\u884c\u7a0b\u5e8f\u4ee3\u7801<\/div>\n<div>\n<div>pthread_attr_init(attr);<\/div>\n<div>pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED);<\/div>\n<div>pthread_create(thread, attr, @android_app_entry, android_app);\/\/\u521b\u5efa\u7ebf\u7a0b\uff0c\u7ebf\u7a0b\u5165\u53e3\u4e3aandroid_app_entry\uff0c\u5165\u53e3\u7684\u53c2\u6570\u4e3aandroid_app<\/div>\n<\/div>\n<div>\/\/\u5f00\u59cb\u7b49\u5f85\u7ebf\u7a0b\u8fd0\u884c\u8d77\u6765<\/div>\n<div>\n<div>pthread_mutex_lock(android_app^.mutex);<\/div>\n<div>\u00a0 while android_app^.running = 0 do<\/div>\n<div>\u00a0 \u00a0 pthread_cond_wait(android_app^.cond, android_app^.mutex);<\/div>\n<div>\u00a0 pthread_mutex_unlock(android_app^.mutex);<\/div>\n<\/div>\n<\/div>\n<div>\u8fd0\u884c\u5b8c\u4e86\u4e4b\u540e\u8fd4\u56deandroid_app\u7ed3\u6784,\u8fd9\u6837\u5c31\u76f8\u5f53\u4e8e\u6211\u4eec\u7684\u8fd9\u4e2aactivity\u7684oncreate\u5b8c\u6210\u4e86\u3002<\/div>\n<div>\u7136\u540e\u770b\u4e00\u4e0bandroid_app_entry\u7684\u8fd9\u4e2a\u7ebf\u7a0b\u5165\u53e3\u51fd\u6570<\/div>\n<div>\n<pre class=\"lang:delphi decode:true \">function android_app_entry(param: Pointer): Pointer; cdecl;\r\n \r\n\/\/ Delphi: init system unit and RTL.\r\nprocedure SystemEntry;\r\ntype\r\nTMainFunction = procedure;\r\nvar\r\nDlsymPointer: Pointer;\r\nEntryPoint: TMainFunction;\r\nbegin\r\nDlsymPointer := dlsym(RTLD_DEFAULT, '_NativeMain');\r\nif DlsymPointer &lt;&gt; nil then\r\nbegin\r\nEntryPoint := TMainFunction(DlsymPointer);\r\nEntryPoint;\r\nend;\r\nend;\r\n \r\nvar\r\nandroid_app: Pandroid_app;\r\nlooper: PALooper;\r\nbegin\r\nandroid_app := Pandroid_app(param);\r\nandroid_app^.config := AConfiguration_new;\/\/\u521b\u5efa\u5e94\u7528\u7a0b\u5e8fconfig\r\nAConfiguration_fromAssetManager(android_app^.config, android_app^.activity^.assetManager);\r\n \r\n \/\/\u4ece\u4e3b\u7ebf\u7a0b\u83b7\u53d6\u6d88\u606f\u7528\r\nandroid_app^.cmdPollSource.id := LOOPER_ID_MAIN;\r\nandroid_app^.cmdPollSource.app := android_app;\r\nandroid_app^.cmdPollSource.process := @process_cmd;\/\/\u8bbe\u7f6e\u5904\u7406cmd\u7684\u547d\u4ee4\u7684\u51fd\u6570\r\nandroid_app^.inputPollSource.id := LOOPER_ID_INPUT;\r\nandroid_app^.inputPollSource.app := android_app;\r\nandroid_app^.inputPollSource.process := @process_input;\/\/\u8f93\u5165\u4e8b\u4ef6\u5904\u7406\u7684\u51fd\u6570\r\n \r\n \/\/\u521b\u5efa\u4e00\u4e2alooper\u6d88\u606f\u5faa\u73af\uff0c\u7528\u6765\u6293\u53d6\u6d88\u606f\r\nlooper := ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);\r\nALooper_addFd(looper, android_app^.msgread, LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT, nil, @android_app^.cmdPollSource);\r\nandroid_app^.looper := looper;\r\n \r\npthread_mutex_lock(android_app^.mutex);\r\nandroid_app^.running := 1; \/\/\u8bbe\u7f6e\uff0c\u8ba9\u7ebf\u7a0b\u4ece\u7b49\u5f85\u5f00\u542f\u8fd0\u884c\u4e2d\u9000\u51fa\uff0c\u4e5f\u5c31\u662f\u4eceandroid_app_create\u4e2d\u9000\u51fa\r\npthread_cond_broadcast(android_app^.cond);\r\npthread_mutex_unlock(android_app^.mutex);\r\n \r\n{ Delphi: this will initialize System and any RTL related functions, call unit initialization sections and then\r\nproject main code, which will enter application main loop. This call will block until the loop ends, which is\r\ntypically signalled by android_app^.destroyRequested. }\r\nSystemEntry;\/\/\u8fd9\u91cc\u624d\u662f\u8c03\u7528\u4e86\u5de5\u7a0b\u6587\u4ef6\u7684Begin End\u4e4b\u95f4\u7684\u4ee3\u7801\uff0c\u662fDelphi\u7684\u4e00\u4e2a\u5c01\u88c5\uff0c\u5b9e\u9645\u4e0a\uff0c\u5728Android Native\u4e2d\u8c03\u7528\u7684\u662fandroid_main(android_app);\u7136\u540e\u518d\u8fd9\u91cc\u6267\u884c\u4e00\u4e9b\u6d88\u606f\u7684\u5904\u7406\uff0c\u7b49\u5f85\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u675f\r\n{ This place would be ideal to call unit finalization, class destructors and so on. }\r\n\/\/ Halt;\r\n \r\nandroid_app_destroy(android_app);\/\/\u9500\u6bc1android_app\u9000\u51fa\u7ebf\u7a0b\u3002\u8fd9\u91cc\u5b9e\u9645\u4e0a\u624d\u662fAndroid\u7a0b\u5e8f\u7684\u7ec8\u7ed3\r\n \r\nResult := nil;\r\nend;<\/pre>\n<p>\u7136\u540e\u662f<\/p>\n<pre class=\"lang:delphi decode:true \">function android_app_read_cmd(android_app: Pandroid_app): ShortInt; cdecl;\r\nvar\r\ncmd: ShortInt;\r\nbegin\r\nResult := -1;\r\nif __read(android_app^.msgread, @cmd, sizeof(cmd)) = SizeOf(cmd) then\r\nbegin\r\ncase cmd of\r\nAPP_CMD_SAVE_STATE:\r\nfree_saved_state(android_app);\/\/\u91ca\u653e\u5f53\u524d\u7684\u4fdd\u5b58\u72b6\u6001\r\nend;\r\nResult := cmd;\r\nend;\r\nend;\r\n \r\nprocedure android_app_pre_exec_cmd(android_app: Pandroid_app; cmd: ShortInt); cdecl; \/\/\u51c6\u5907\u6267\u884c\u547d\u4ee4\u72b6\u6001\r\nbegin\r\ncase cmd of\r\nAPP_CMD_INPUT_CHANGED:\r\nbegin\r\npthread_mutex_lock(android_app^.mutex);\r\nif android_app^.inputQueue &lt;&gt; nil then\r\nAInputQueue_detachLooper(android_app^.inputQueue);\r\nandroid_app^.inputQueue := android_app^.pendingInputQueue;\r\nif android_app^.inputQueue &lt;&gt; nil then\r\nAInputQueue_attachLooper(android_app^.inputQueue, android_app^.looper, LOOPER_ID_INPUT, nil,\r\n@android_app^.inputPollSource);\r\npthread_cond_broadcast(android_app^.cond);\r\npthread_mutex_unlock(android_app^.mutex);\r\nend;\r\n \r\nAPP_CMD_INIT_WINDOW:\r\nbegin\r\npthread_mutex_lock(android_app^.mutex);\r\nandroid_app^.window := android_app^.pendingWindow;\r\npthread_cond_broadcast(android_app^.cond);\r\npthread_mutex_unlock(android_app^.mutex);\r\nend;\r\n \r\nAPP_CMD_TERM_WINDOW:\r\npthread_cond_broadcast(android_app^.cond);\r\n \r\nAPP_CMD_RESUME, APP_CMD_START, APP_CMD_PAUSE, APP_CMD_STOP:\r\nbegin\r\npthread_mutex_lock(android_app^.mutex);\r\nandroid_app^.activityState := cmd;\r\npthread_cond_broadcast(android_app^.cond);\r\npthread_mutex_unlock(android_app^.mutex);\r\nend;\r\n \r\nAPP_CMD_CONFIG_CHANGED:\r\nAConfiguration_fromAssetManager(android_app^.config, android_app^.activity^.assetManager);\r\n \r\nAPP_CMD_DESTROY:\r\nandroid_app^.destroyRequested := 1;\r\nend;\r\nend;\r\n \r\nprocedure android_app_post_exec_cmd(android_app: Pandroid_app; cmd: ShortInt); cdecl;\r\nbegin\r\ncase cmd of\r\nAPP_CMD_TERM_WINDOW:\r\nbegin\r\npthread_mutex_lock(android_app^.mutex);\r\nandroid_app^.window := nil;\r\npthread_cond_broadcast(android_app^.cond);\r\npthread_mutex_unlock(android_app^.mutex);\r\nend;\r\n \r\nAPP_CMD_SAVE_STATE:\r\nbegin\r\npthread_mutex_lock(android_app^.mutex);\r\nandroid_app^.stateSaved := 1;\r\npthread_cond_broadcast(android_app^.cond);\r\npthread_mutex_unlock(android_app^.mutex);\r\nend;\r\n \r\nAPP_CMD_RESUME:\r\n{ Delphi: It is unclear why this line is necessary in original AppGlue, but it prevents FireMonkey applications\r\nfrom recovering saved state. FireMonkey recovers saved state usually after APP_CMD_INIT_WINDOW, which happens\r\nmuch later after CMD_RESUME. }\r\n{ free_saved_state(android_app) };\r\nend;\r\nend;\r\n \r\nprocedure process_cmd(app: Pandroid_app; source: Pandroid_poll_source); cdecl;\r\nvar\r\ncmd: ShortInt;\r\nbegin\r\ncmd := android_app_read_cmd(app);\/\/\u5148\u8bfb\u53d6\u547d\u4ee4\r\nandroid_app_pre_exec_cmd(app, cmd);\/\/\u51c6\u5907\u547d\u4ee4\r\nif Assigned(app^.onAppCmd) then \/\/\u7a0b\u5e8f\u5185\u90e8\u7684\u6307\u4ee4\u5904\u7406\r\napp^.onAppCmd(app, cmd);\r\nandroid_app_post_exec_cmd(app, cmd);\/\/\u6267\u884c\u5904\u7406\r\nend;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u4e9b\u4ee3\u7801\u5c06\u5728Android\u6d88\u606f\u5faa\u73af\u4e2d\u5904\u7406\u8c03\u7528<\/p>\n<p>\u5176\u5b9e\u8fd9\u4e9b\u4ee3\u7801\u4e2d\u5f88\u591a\u5730\u65b9\u90fd\u662f\u6709\u6ce8\u91ca\u7684\u3002\u4ed4\u7ec6\u770b\u770b\u5c31\u660e\u767d\u4e86\u3002<\/p>\n<p>\u7136\u540e\u7a0b\u5e8f\u8fdb\u5165\u5de5\u7a0b\u6587\u4ef6\u7684Begin End\u4e4b\u95f4\uff0c\u5148\u8fdb\u5165SysInit.pas\u5355\u5143\u7684_InitExe\uff0c\u7136\u540e\u4f1a\u8c03\u7528GetThisModuleHandle\uff0c\u8fd9\u4e2a\u4f1a\u8c03\u7528dlopen(Info.dli_fname, RTLD_LAZY)\u5c31\u662f\u76f8\u5f53\u4e8e\u52a0\u8f7dWindows\u7684DLL\uff0c\u4f1a\u5148\u52a0\u8f7d(Lib+\u7a0b\u5e8f\u540d.so)\u83b7\u5f97\u5f53\u524d\u53e5\u67c4\uff0c\u7136\u540edlClose\u5173\u95ed,\u6700\u540e\u7a0b\u5e8f\u4ee5\u8fd9\u4e2a\u5de5\u7a0b\u7684\u5e93\u53e5\u67c4\u4f5c\u4e3a\u7a0b\u5e8f\u7684Hinstance\uff0c\u4e5f\u5c31\u662f\u8bf4\u6211\u4eec\u7684\u597d\u591a\u8d44\u6e90\u5e94\u8be5\u4f1a\u90fd\u6253\u5305\u5230\u8fd9\u4e2aso\u4e2d\u53bb,\u6700\u540e\u5982\u679c\u662fAndroid\u73af\u5883\uff0c\u4f1a\u8c03\u7528_StartExe\uff0c\u6765\u542f\u52a8\u7a0b\u5e8f\uff0c_StartExe\u4e2d\u4f1a\u8c03\u7528InitUnits\u6765\u521d\u59cb\u5316\u4e00\u4e9b\u5355\u5143\uff0c\u8fd9\u91cc\u5c31\u4f1a\u8c03\u7528\u7a0b\u5e8f\u6240\u5f15\u7528\u5230\u7684\u5404\u4e2a\u5355\u5143\u7684Initialization\u4e2d\u7684\u5185\u5bb9\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u4f1a\u521d\u59cb\u5316FMX.PlatForm\u8fd9\u4e2a\u8de8\u5e73\u53f0\u5355\u5143\u7684TPlatformServices\u7c7b\u5e93\uff0c\u672c\u5e93\u662f\u8de8\u5e73\u53f0\u5355\u5143\u670d\u52a1\u7ba1\u7406\uff0c\u7136\u540e\u5c31\u4f1a\u8c03\u7528FMX.PlatForm\u4e0b\u7684initialization\uff0c\u91cc\u9762\u7684RegisterCorePlatformServices\u4f1a\u6839\u636e\u9009\u62e9\u7684\u5e73\u53f0\u6765\u5224\u5b9a\u5230\u5e95\u8c03\u7528\u54ea\u4e2a\u5e73\u53f0\u4e0b\u7684RegisterCorePlatformServices\uff0c\u8fd9\u4e2a\u5224\u5b9a\u901a\u8fc7\u7f16\u8bd1\u9884\u5904\u7406\u6307\u4ee4\u5728Implemention\u4e0b\u7684Use\u4e2d<\/p>\n<p>uses<\/p>\n<p>{$IFDEF IOS}<\/p>\n<p>FMX.Platform.iOS,<\/p>\n<p>{$ELSE}<\/p>\n<p>{$IFDEF MACOS}<\/p>\n<p>FMX.Platform.Mac,<\/p>\n<p>{$ENDIF MACOS}<\/p>\n<p>{$ENDIF IOS}<\/p>\n<p>{$IFDEF MSWINDOWS}<\/p>\n<p>FMX.Platform.Win,<\/p>\n<p>{$ENDIF}<\/p>\n<p>{$IFDEF ANDROID}<\/p>\n<p>FMX.Platform.Android,<\/p>\n<p>{$ENDIF}\uff0c\u5728ANdroid\u4e0b\uff0c\u5c31\u4f1a\u8c03\u7528FMX.PlatForm.Android\u4e2d\u7684RegisterCorePlatformService\u6765\u6ce8\u518c\u6838\u5fc3\u5e73\u53f0\u670d\u52a1,\u7136\u540e\u91cc\u9762\u6709<\/p>\n<p>if Assigned(PlatformAndroid.FAndroidApp) then<\/p>\n<p>begin<\/p>\n<p>PlatformAndroid.FAndroidApp^.onAppCmd := @HandleAndroidCmd;<\/p>\n<p>PlatformAndroid.FAndroidApp^.onInputEvent := @HandleAndroidInputEvent;<\/p>\n<p>end;<\/p>\n<p>\u8fd9\u6837\u5c31\u5c06\u524d\u9762\u6240\u8bf4\u7684Android\u7684\u6d88\u606f\u548c\u8f93\u5165\u4e8b\u4ef6\u8f6c\u5230FMX\u5e73\u53f0\u7684HandleAndroidCmd\u548cHandleAndroidInputEvent\u51fd\u6570\u4e2d\u7136\u540eHandleApplicationEvent\u4e2d\u5c31\u4f1a\u548c\u6d88\u606f\u7ba1\u7406\u5668TMessageManager\u7ed3\u5408\u5bf9\u6d88\u606f\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>\u628a\u8fd9\u4e9b\u5355\u5143\u4e2d\u7684\u4e00\u4e9b\u521d\u59cb\u5316\u8fc7\u7a0b\u90fd\u641e\u5b8c\u4e86\uff0c\u5c31\u6267\u884c\u5230\u5de5\u7a0b\u6587\u4ef6\u4e2d\u7684Begin ENd\u4e4b\u95f4\u53bb\u5904\u7406\u4ee3\u7801\u4e86\uff0c\u6700\u540e\u6267\u884c\u4e86Application.Run\uff1b<\/p>\n<p>\u7136\u540e\u6211\u4eec\u770b\u8fd9\u4e2a\u6267\u884c\u7684\u4ee3\u7801<\/p>\n<pre class=\"lang:delphi decode:true \">procedure TApplication.Run;\r\nvar\r\nAppService: IFMXApplicationService;\r\nbegin\r\n{$IFNDEF ANDROID}\r\nAddExitProc(DoneApplication);\r\n{$ENDIF}\r\nFRunning := True;\r\ntry\r\nif TPlatformServices.Current.SupportsPlatformService(IFMXApplicationService, AppService) then\r\nAppService.Run;\r\nfinally\r\nFRunning := False;\r\nend;\r\nend;<\/pre>\n<p>\u5b9e\u9645\u4e0a\u4ed6\u662f\u901a\u8fc7\u8de8\u5e73\u53f0\u670d\u52a1\u83b7\u5f97\u5f53\u524d\u8fd0\u884c\u7a0b\u5e8f\u7684\u670d\u52a1\u5e73\u53f0\uff0c\u4e5f\u5c31\u662fFMX.PlatForm.Android\u4e2d\u7684TPlatformAndroid\uff0c\u6b64\u7c7b\u7ee7\u627f\u4e86IFMXApplicationService\uff0c\u5e76\u4e14\u5728\u524d\u9762\u5df2\u7ecf\u901a\u8fc7RegisterCorePlatformServices\u6ce8\u518c\u4e86TPlatformAndroid,\u6240\u4ee5\u8fd9\u91cc\u5b9e\u9645\u4e0a\u8c03\u7528\u7684\u5c31\u662fTPlatformAndroid\u7684Run\u8fc7\u7a0b\uff0c\u8fd9\u4e2aRun\u5b9e\u9645\u4e0a\u5c31\u662f\u5f00\u59cb\u8dd1Android\u7684\u6d88\u606f\u5faa\u73af\u5904\u7406\u4e86\u3002\u4ee3\u7801\u5f88\u77ed\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u8c03\u7528\u4e86InternalProcessMessages\u6765\u8fdb\u884c\u5185\u90e8\u6d88\u606f\u5faa\u73af\u5904\u7406\u00a0<\/p>\n<pre class=\"lang:delphi decode:true \">procedure TPlatformAndroid.Run;\r\nbegin\r\n{ Although calling this routine is not really necessary, but it is a way to ensure that \"Androidapi.AppGlue.pas\" is\r\nkept in uses list, in order to export ANativeActivity_onCreate callback. }\r\napp_dummy;\r\n \r\nrepeat\r\nInternalProcessMessages;\r\nuntil FAndroidApp^.destroyRequested &lt;&gt; 0;\r\nend;<\/pre>\n<p>\u9996\u5148InternalProcessMessages\u4e2d\u6709<\/p>\n<p>EventPollValue := ALooper_pollAll(GetAndUpdateTimeout, nil, nil, PPointer(@PEventPollSource));<\/p>\n<p>\u5148\u901a\u8fc7\u8fd9\u4e2a\u83b7\u5f97\u6d88\u606f\u4fe1\u606f,\u5982\u679c\u83b7\u5f97\u5230\u4e86\u5c31\u6709<\/p>\n<p>if (PEventPollSource &lt;&gt; nil) and Assigned(PEventPollSource^.process) then<\/p>\n<p>begin<\/p>\n<p>PEventPollSource^.process(FAndroidApp, PEventPollSource);<\/p>\n<p>if EventPollValue = LOOPER_ID_MAIN then<\/p>\n<p>HasEvents := True;<\/p>\n<p>end<\/p>\n<p>PEventPollSource^.process(FAndroidApp, PEventPollSource);\u8fd9\u53e5\u4f1a\u8c03\u7528\u4e4b\u524d\u8bbe\u5b9a\u521d\u59cb\u5316\u7684Process_Cmd\u6216\u8005Process_Input<\/p>\n<p>\u8fd9\u6837\uff0c\u5c31\u5c06\u6d88\u606f\u548c\u5b9e\u9645\u5173\u8054\u8d77\u6765\u4e86\u3002\u81f3\u4e8e\u663e\u793aAndroid\u7684\u754c\u9762\uff0c\u5219\u4f1a\u901a\u8fc7TWindowManager.Current.RenderIfNeeds\u6765\u5224\u5b9a\u662f\u5426\u9700\u8981\u6e32\u67d3\u754c\u9762\u4fe1\u606f\uff0c\u5982\u679c\u9700\u8981\u5219\u4f1a\u8c03\u7528TWindowManager.Render\u6765\u8fdb\u884c\u754c\u9762\u6e32\u67d3\uff0c\u6700\u540eRender\u51fd\u6570\u4e2d<\/p>\n<p>procedure RenderNormalWindows;<\/p>\n<p>var<\/p>\n<p>I: Integer;<\/p>\n<p>PaintControl: IPaintControl;<\/p>\n<p>begin<\/p>\n<p>for I := FWindows.Count &#8211; 1 downto 0 do<\/p>\n<p>if FWindows[I].Form.Visible and (not FWindows[I].RequiresComposition) and Supports(FWindows[I].Form,<\/p>\n<p>IPaintControl, PaintControl) then<\/p>\n<p>begin<\/p>\n<p>PaintControl.PaintRects([TRectF.Create(0, 0, FContentRect.Width, FContentRect.Height)]);<\/p>\n<p>Break;<\/p>\n<p>end;<\/p>\n<p>end;<\/p>\n<p>PaintRects\u8fd9\u4e2a\u5219\u5f00\u59cb\u5339\u914dFMXForm\u7684PaintRects\u51fd\u6570\u6765\u8fdb\u884c\u754c\u9762\u4fe1\u606f\u7ed8\u5236\u3002\u4e8e\u662f\u4e00\u4e2aAndroid\u7a0b\u5e8f\u5f00\u542f\u8fd0\u884c\u5230\u663e\u793a\u57fa\u672c\u5b8c\u6210\u3002<\/p>\n<p><script src=\"\/\/www.google-analytics.com\/analytics.js\" async=\"\" type=\"mce-mce-mce-mce-mce-no\/type\"><\/script><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u81ea\u4e0d\u5f97\u95f2\u5728cnblog\u7684\u535a\u5ba2\uff1aDelp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[145,8,21],"tags":[],"class_list":["post-2369","post","type-post","status-publish","format-standard","hentry","category-android","category-delphi","category-misc"],"views":3520,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2369"}],"version-history":[{"count":4,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2369\/revisions"}],"predecessor-version":[{"id":2374,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/2369\/revisions\/2374"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}