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. 使用內部類別法

2012年5月15日 星期二

Android 4.0.3 ICS release for Beaglebone, Beagleboard-XM


前陣子在 TI 的網站發現 TI 公佈了 Cortex-A8 處理器的 Android 4.0.3 (ICS),共有兩個版本,一個是 TI_Android_ICS_4_0_3_DevKit_3_0_0,這是給 Beagleboard-XM 使用的,可以由下面這個網頁查看 DeveloperGuide:

另一個版本是 TI_Android_ICS_4_0_3_DevKit_3_0_1,則是提供給 Beaglebone 使用的:

手邊有這些開發板的朋友也可以試試。 ^^

在記憶體少於 2GB 的環境編譯 Android 4.0 (ICS)

雖然現在大多數人的電腦都裝了大於 2GB 的記憶體,甚至可能已經在電腦上安裝到 8GB、16GB 了。但,有時若使用 VMware 的產品或 VirtualBox 等軟體建立虛擬機器時,可能在虛擬機器裡多半會配置 1GB 或 2GB 的記憶體。

不過,這麼一來編譯 Android 4.0 (ICS) 時可能會遇上如下的錯誤:


target Dex: core
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.
make: *** [out/target/common/obj/JAVA_LIBRARIES/core_intermediates/noproguard.classes-with-local.dex] Error 1 

這個容易處理,修改 build/core/definitions.mk 這個檔案是可以解決的,修改方式如下:

diff --git a/build/core/definitions.mk b/build/core/definitions.mk
index b678158..b83b299 100644
--- a/build/core/definitions.mk
+++ b/build/core/definitions.mk
@@ -1525,7 +1525,7 @@ define transform-classes.jar-to-dex
 @echo "target Dex: $(PRIVATE_MODULE)"
 @mkdir -p $(dir $@)
 $(hide) $(DX) \
-    $(if $(findstring windows,$(HOST_OS)),,-JXms16M -JXmx2048M) \
+    $(if $(findstring windows,$(HOST_OS)),,-JXms16M -JXmx1024M) \
     --dex --output=$@ \
     $(incremental_dex) \
     $(if $(NO_OPTIMIZE_DX), \