2012年8月2日 星期四

hello-jni 測試

今天休颱風假,測試一下hello-jni,跟大家分享。
測試步驟如下:
1. 匯入hello-jni專案
 2.執行結果
3.新增NDK編譯功能
4. 刪除自動產生的檔案
5. 查看C程式,標示許多錯誤 
 6.在專案檔上按下右鍵,選擇對話盒上C/C++ General->Paths and Symbols
 7. 選擇在cygwin上ndk平台的include目錄  C:\cygwin\home\john\android-ndk-r8\platforms\android-14\arch-arm\usr\include
 8.再看一次C程式,已沒有錯誤
9.修改HelloJni.java程式,呼叫未實現的函式
10.編譯成功
11. 再執行有錯誤
 12. 查看LogCat發現錯誤
 13. 編寫未實現的函式

 14.重新執行

2012年8月1日 星期三

PhoneGap 介紹

在看了老師所寫的 "Web App設計- Web 和Android的互動技巧" 這篇文章,我想到之前接觸到的 PhoneGap 這玩意,也寫出來給各位讀者當個參考。

PhoneGap 是一個開放源碼 (open source) 的 Framework, 以 HTML5 為基礎,可以說是目前跨平台行動開發解決方案的熱門話題之一。經由 PhoneGap 封裝,採用 HTML + JavaScript 的應用程式可以存取不同手機的原生 API。

有興趣的朋友可以由 http://phonegap.com/ 下載最新版本的 PhoneGap,目前的版本是不久前發表的 2.0.0 版。



下載回來後,解開會得像下圖的目錄:


接著可以由 Eclipse 將 lib\android\example 目錄的這個專案載入:


org.apache.cordova.example.cordovaExample 這個專案在模擬器裡執行的畫面,建議可以直接下載到手機上執行,它的範例可以直接控制手機上的感知器。


範例程式所示範的 "Call 411" 這個動作,它會直接呼叫手機的撥號程式,並帶入 411 這個號碼:


PhoneGap 所提供的範例,有幾個主要的目錄及檔案:

  • assets/www/
  • libs/cordova-2.0.0.jar
  • res/xml/

而 Java 程式碼的部份,單純在繼承自 DroidGap 的 Class 裡,透過 loadUrl() 載入 assets/www/index.html 這個主畫面:


import org.apache.cordova.*;

public class cordovaExample extends DroidGap
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
    }
}


2012年7月31日 星期二

一起來玩 X86 版本的 Android 4.0.4 (Ice Cream Sandwich branch)


最近在搞 X86 版本的 Android,目前用的是由 cwhuang (黃志偉) 等開發者維護的 Android-x86。

根據 "ReleaseNote 4.0-RC2" 這篇文章,下載 Ice Cream Sandwich 的程式碼:

$ repo init -u http://git.android-x86.org/manifest -b ics-x86
$ repo sync 

由於我打算先用 VMware Player 和 VirtualBox 來執行 Android 4.0。所以在編譯時,選擇的裝置是 eeepc:

$ lunch eeepc-eng
$ make iso_img 

接著請耐心等待它編譯完成。編譯 Android 4.0 比起編譯 Android 2.3 更花時間,所以建議參考 android.com 上的說明設定使用 CCACHE,可以讓再次編譯時稍快一些。不過,使用 CCACHE 在第一次編譯時似乎會拉長需要的時間。

編譯完成後,可以得到 out/target/product/eeepc/eeepc.iso 這個檔案。接著,可以利用 VMware Player 或 Virtualbox 建立一個虛擬機器,並執行這個 ISO 檔。

載入 Android 4..0 後,使用 ethernet 前必須設定 eth0 的 IP,可以開啟一個 Terminal,先執行 su 取得 root 權限,然後執行 dhcpcd eth0 或 netcfg eth0 dhcp 來取得 IP 位址。也要使用 setprop 指令設定 net.dns1,我目前慣用 8.8.8.8 這個伺服器。

然後就可以開始瀏覽網頁了,也可以在 Windows 端開個 DOS 視窗,利用 adb 指令連進來。



補充一點,VMware Player 若採用預設的選項建立虛擬機器,會發現啟動 Android 4.0 後無法使用 eth0 這個介面。這時,請關閉虛擬機器,然後修改 .vmx 檔,將 ethernet0.virtualDev 改成 "vlance"。


2012年7月29日 星期日

Web App設計- Web 和Android的互動技巧


上圖是Android官方網站介紹如何在手機上利用HTML, CSS等網頁技術來設計Web APPs,最簡單的方法和傳統的PC瀏覽網頁一樣,使用一般的網頁設計,把網頁放在伺服器上,然後利用瀏覽器來閱讀,如上圖左邊方式。今天我們要來討論的是如何整合網頁及Android APP,如上圖右邊的方式,這種方式有一種讓人期待和創新的設計。
1. 先建立一個專案
2. 刪除TextView,新增WebView
3. 修改程式
package com.example.webtest;
import android.os.Bundle;
import android.app.Activity;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends Activity {
    WebView web;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        web = (WebView) findViewById(R.id.webView1);
        WebSettings webSettings = web.getSettings();
        webSettings.setJavaScriptEnabled(true);
        web.loadUrl("file:///mnt/sdcard/test.html");
        web.addJavascriptInterface(new JavaScriptInterface(this), "Android");
       
    }
 
}
4. 新增JavaScriptInterface.java,並加入下列程式。

package com.example.webtest;
import android.content.Context;
import android.widget.Toast;
public class JavaScriptInterface {
    Context mContext;
    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }
    /** Show a toast from the web page */
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}


5. 在AndroidManifest.xml檔中加入Internet使用權限。
6.用記事本編輯,並儲存為test.html
Test/title></span></p> <p style="margin-top: 4.8pt; margin-bottom: 0pt; margin-left: 0in; text-indent: 0in; direction: ltr; unicode-bidi: embed; word-break: normal; "><span style="font-family: Calibri; "><body>Hello World</span></p> <p style="margin-top: 4.8pt; margin-bottom: 0pt; margin-left: 0in; text-indent: 0in; direction: ltr; unicode-bidi: embed; word-break: normal; "><span style="font-family: Calibri; "><input type="button" value="Say hello" </span><span style="font-family: Calibri; ">onClick</span><span style="font-family: Calibri; ">="</span><span style="font-family: Calibri; ">showAndroidToast</span><span style="font-family: Calibri; ">('Hello Android!')" /></span></p> <p style="margin-top: 4.8pt; margin-bottom: 0pt; margin-left: 0in; text-indent: 0in; direction: ltr; unicode-bidi: embed; word-break: normal; "></p> <p style="margin-top: 4.8pt; margin-bottom: 0pt; margin-left: 0in; text-indent: 0in; direction: ltr; unicode-bidi: embed; word-break: normal; "><span style="font-family: Calibri; "><script type="text/</span><span style="font-family: Calibri; ">javascript</span><span style="font-family: Calibri; ">"></span></p> <p style="margin-top: 4.8pt; margin-bottom: 0pt; margin-left: 0in; text-indent: 0in; direction: ltr; unicode-bidi: embed; word-break: normal; "><span style="font-family: Calibri; ">    function </span><span style="font-family: Calibri; ">showAndroidToast</span><span style="font-family: Calibri; ">(toast) {</span></p> <p style="margin-top: 4.8pt; margin-bottom: 0pt; margin-left: 0in; text-indent: 0in; direction: ltr; unicode-bidi: embed; word-break: normal; "><span style="font-family: Calibri; ">        </span><span style="font-family: Calibri; ">Android.showToast</span><span style="font-family: Calibri; ">(toast);</span></p> <p style="margin-top: 4.8pt; margin-bottom: 0pt; margin-left: 0in; text-indent: 0in; direction: ltr; unicode-bidi: embed; word-break: normal; "><span style="font-family: Calibri; ">    }</span></p> <p style="margin-top: 4.8pt; margin-bottom: 0pt; margin-left: 0in; text-indent: 0in; direction: ltr; unicode-bidi: embed; word-break: normal; "><span style="font-family: Calibri; "></script></span></p> <p style="margin-top: 4.8pt; margin-bottom: 0pt; margin-left: 0in; text-indent: 0in; direction: ltr; unicode-bidi: embed; word-break: normal; "><span style="font-family: Calibri; "></body></span></p> <p style="margin-top: 4.8pt; margin-bottom: 0pt; margin-left: 0in; text-indent: 0in; direction: ltr; unicode-bidi: embed; word-break: normal; "><span style="font-family: Calibri; "></html></span></p><p style="margin-top: 4.8pt; margin-bottom: 0pt; margin-left: 0in; text-indent: 0in; direction: ltr; unicode-bidi: embed; word-break: normal; "><span style="font-family: Calibri; ">7.利用DDMS</span><span style="background-color: white; font-family: 新細明體; ">開啟</span><span style="background-color: white; font-family: Calibri; ">File explorer</span><span style="background-color: white; font-family: 新細明體; ">在</span><span style="background-color: white; font-family: Calibri; ">sdcard</span><span style="background-color: white; font-family: 新細明體; ">上放</span><span style="background-color: white; font-family: Calibri; ">test.html</span><span style="background-color: white; font-family: 新細明體; ">檔案。</span></p><p class="separator" style="text-align: center; clear: both; "><a imageanchor="1" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjv1GTN14kMO_ZWsCgRNVAjne9YOdXsxlWHnT1lDYxEX3gcepLZNQFViBH4tG6uvZC5XYwjx8iFtqa-TE4nz6U068xJommb6yj7YVp7Rg04cnqhBnYBxE7zyVGdXPHGSvWZ1y8Bqn3BXvkz/s1600/web4.png" style="margin-left: 1em; margin-right: 1em; "><img style="" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjv1GTN14kMO_ZWsCgRNVAjne9YOdXsxlWHnT1lDYxEX3gcepLZNQFViBH4tG6uvZC5XYwjx8iFtqa-TE4nz6U068xJommb6yj7YVp7Rg04cnqhBnYBxE7zyVGdXPHGSvWZ1y8Bqn3BXvkz/s320/web4.png" border="0" width="320" height="272"></a></p><p class="separator" style="text-align: left;clear: both; ">8. 執行結果</p><p class="separator" style="text-align: center; clear: both; "><a imageanchor="1" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO1OtNxeYa6GsYhWCXTPYytIO7qVhTp1E97M5_nY-QdEq5O86OLweskw9U2M95WZ_1zFf5EkC1Kc1BZ8hhAVd4x-ICUWHHpHAoELNKtzeGZHTcNDImrRZ8cETQtVUkszmr2s_nkLmd9YwB/s1600/web5.png" style="margin-left: 1em; margin-right: 1em; "><img style="" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO1OtNxeYa6GsYhWCXTPYytIO7qVhTp1E97M5_nY-QdEq5O86OLweskw9U2M95WZ_1zFf5EkC1Kc1BZ8hhAVd4x-ICUWHHpHAoELNKtzeGZHTcNDImrRZ8cETQtVUkszmr2s_nkLmd9YwB/s320/web5.png" border="0" width="191" height="320"></a></p><p class="separator" style="text-align: left;clear: both; "><br></p><p style="margin-top: 4.8pt; margin-bottom: 0pt; margin-left: 0in; text-indent: 0in; direction: ltr; unicode-bidi: embed; word-break: normal; "><span style="background-color: white; font-family: 新細明體; "><br></span></p><p></p><div><br></div>

2012年7月28日 星期六

解決DMA-210L開發平台在Ubuntu 12.04 LTS下編譯Android 2.3.4出現的問題


近日因緣際會測試到長高科技的 ARM Cortex A8 DMA-210L開發平台,由於該平台目前使用的是Android 2.3.4(Gingerbread)作業系統,加上Gingerbread在Ubuntu 12.04編譯上有些許地方需要修改,在這裡整理出來順便做個紀錄。


2012年6月29日 星期五

新版第一支Android程式(Android SDK r20) -- HelloWorld 包含 NDK Plugin


每年的Google I/O大會都會帶一些新的震撼出來,今年也不例外除了Android 4.1(Jelly Bean,果凍糖),Nexus 7,Nexus Q..外最主要因應Android 4.1在SDK Tools部分發佈了r20版本,由於初次使用感覺上跟以前的操作介面改變了許多故在部落格上整理分享一下。

2012年6月20日 星期三

[ Android Programming] 按鈕事件 (Button Event) 處理

在設計Android程式設計,按鈕及文字盒算是最常見的人機介面(UI),本文將介紹如何設計按鈕單擊事件處理函式的技巧。
事先準備的工作如下:
1. 新增專案,命名為ButtonTest
2. 點選上圖的 Next >鍵,選擇Android Target為Android 4.0.3

3. 點選上圖的 Next >鍵,在Package Name上輸入com.example.button


4. 點選上圖Finish鍵,開啟res/main.xml,加入按鈕。

5. 切換至XML觀察模式,為T extView輸入Id, android:id="@+id/text1"

6. 打開ButtonTestActitity.java,其程式碼如下圖:

完成上述5步驟後,我們可以來玩玩,按鈕事件處理函式,一般事件函式都是以on開頭,而且單擊函式則為onClick(),在呼叫單擊事件處理函式前,必須先裝監聽器onClickListener(),以下則是以單擊事件監聽器各種存在樣式來說明程式設計。

1. 使用外部類別方法
使用此種方法需要另外建立檔案,在呼叫上也需要透過View parent = (View) v.getParent();來協助取得父視域,才能找到TextView的物件。

2. 使用匿名物件法
3. 使用匿名區域變數法

4. 使用匿名屬性變數法
5. 使用介面繼承法

7. 使用內部類別法