golang 行動裝置 App例子 example/basic 源碼架構分析

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

條件編譯

我們在源碼中可以看到2個檔案: main.go 和 main_x.go 這兩個包名都是 package main , 都有 main 函數。 不會衝突嗎?

答案是不會的,

main_x.go 檔案中有個注釋:

// +build !darwin,!linux,!windows

main.go 檔案中注釋如下:

// +build darwin linux windows
這裡來標示編譯適用的不同環境。只有滿足條件的才會被編譯進去, 所以這裡有2個 main 函數,編譯並不衝突。

參考:

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

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:
                // ...
            }
        }
    })
}

不同的事件隨後觸發不同的函數。完成這些功能。

從字面理解就可以知道這幾個事件是做啥的。

lifecycle.Event  Activity 生命週期相關的幾個事件;

size.Event  螢幕尺寸變化相關事件

paint.Event  繪畫螢幕的事件

touch.Event 觸屏或者滑鼠左鍵點擊和移動事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.