golang 移动应用例子 example/basic 源码框架分析

条件编译

咱们在源码中能够看到2个文件: main.go 和 main_x.go 这两个包名都是 package main , 都有 main 函数。 不会冲突么?linux

答案是不会的,android

main_x.go 文件中有个注释:git

// +build !darwin,!linux,!windowsgithub

main.go 文件中注释以下:golang

// +build darwin linux windows
这里来标示编译适用的不一样环境。只有知足条件的才会被编译进去, 因此这里有2个 main 函数,编译并不冲突。windows

参考:服务器

http://blog.csdn.net/varding/article/details/12675971 app

http://dave.cheney.net/2013/10/12/how-to-use-conditional-compilation-with-the-go-build-tool 函数

Android应用启动入口

APK文件自己是一个压缩包,直接用解压工具便可打开,但里面的文件都已被编码为二进制文件格式,不能直接看,好比程序描述文件AndroidManifest.xml,使用apktool工具能够将这些文件解码还原出来。
apktool(http://code.google.com/p/android-apktool/  如今地址是: http://ibotpeaches.github.io/Apktool/ )是一个很是著名的开源工具包,功能很强大,能够解包APK文件并从新打包,经常使用来汉化Android应用。工具

apktool 安装方法请看: http://ibotpeaches.github.io/Apktool/install/

参考: http://kenkao.iteye.com/blog/1890497

使用这个工具咱们能够看到basic.apk文件的 AndroidManifest.xml 文件的内容以下:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="org.golang.todo.basic" android:versionCode="1" android:versionName="1.0"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:label="Basic" android:debuggable="true">
        <activity android:name="org.golang.app.GoNativeActivity" android:label="Basic" android:configChanges="keyboardHidden|orientation">
            <meta-data android:name="android.app.lib_name" android:value="basic" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

这个就是apk文件的执行入口,而不是作服务器段开发的 main 函数入口。

这里隐含的实现了 org.golang.app.GoNativeActivity 这个Activity。

 

具体这里的逻辑被封装在 golang.org/x/mobile/app 代码中。

主要的核心代码以下:

func main() {
    app.Main(func(a app.App) {
        for e := range a.Events() {
            switch e := a.Filter(e).(type) {
            case lifecycle.Event:
                // ...
            case size.Event:
                // ...
            case paint.Event:
                // ...
            case touch.Event:
                // ...
            }
        }
    })
}

image

不一样的事件随后触发不一样的函数。完成这些功能。

从字面理解就能够知道这几个事件是作啥的。

lifecycle.Event  Activity 生命周期相关的几个事件;

size.Event  屏幕尺寸变化相关事件

paint.Event  绘画屏幕的事件

touch.Event 触屏或者鼠标左键点击和移动事件

相关文章
相关标签/搜索