顯示具有 Android 標籤的文章。 顯示所有文章
顯示具有 Android 標籤的文章。 顯示所有文章

2009年6月9日 星期二

常用的android commands in console mode

模擬器有關的commands
  • 顯示可用的android 模擬器 - android list avd
  • 啟動模擬器 - emulator -avd
  • 利用shell連線到模擬器 - abd shell
檔案存取有關的commands
  • 從device當中拷貝出檔案 - adb pull local_path remote_path
  • 將檔案拷貝到device當中 - adb push  remote_path local_path

Install and debug Application in HTC Magic

第一步 -裝好連接HTC Magic的相關軟韌體-

由於我的電腦的OS是Windows Vista, 所以需要到HTC網站download 相關的USB driver. 只要將網頁當中的兩個檔案HTC Sync.zip與 HTCDriver_Update_Vista_32bits.exe(或是HTCDriver_Update_Vista_64bits.exe)download下來,依序安裝即可.
安裝完畢後到控制台->裝置管理員 中, 可看到(如畫面所示)多了個Android USB Devices的項目.








第二步 HTC Magic手機上進行應用程式開發相關設定

需要開啟相關設定, 這樣PC端的IDE(如: Eclipse)才可以將程式灌到手機並可以在實機上Debug Application(應用程式), 也就是不用在模擬器上進行程式的Debug.
請到手機中 設定->應用程式->開發 當中進行設定. 你將在手機當中看到這畫面



若是你要Debug你的應用程式, 就點選"USB除錯中", 可樣稍帶你就可以在實機上Debug你的應用程式.


第三步 PC端的IDE(我是用Eclipse)的設定


讓IDE知道是要用模擬器還是實機(HTC Magic)上安裝與Debug你設計的應用程式. 程式設計師需要到Eclipse中的Main Menu 點選 Run->Debug Configurations->Android Application或是Run->Run Configurations->Android Application. 在Android Application下有多個你所開發的android 應用程式, 你就點選你要安裝到時機上的應用程式, 並點選右畫面中的Target Tab. 在此頁當中(如圖), 可以設定你是要用安裝到模擬器或是要安裝到實機上. 當然, 我們這邊是要在實機上測試程式. 所以選擇manual項目. 到時候就可以連上實機.






第四步 用Eclipse 執行你的應用程式


在Eclipse的主畫面當中, 點選Package Explorer裡的應用程式, 並按滑鼠右鍵可以看到Menu視窗, 點選當中的Debug As->1 Android Application, 就可以開始在你的HTC Magic上Trace 應用程式的code.

2009年5月1日 星期五

How to lock your Android App's orientation?


Ans: You can lock your app's orientation to portrait or landscape using the 'android: screenOrientation' attribute in your AndroidManifest.xml in the folder of app project.









2009年4月29日 星期三

How to generate GUI Layout by a xml file?

Step 1: 先找到Project的fodler位置. 子目錄res/layout/中產生一main.xml檔案。若已經存在就直接打開.

Step 2: 修改內容,填入想要的畫面結構. 如
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"/>


Step 3: 打開另一個xml檔案(string.xml in res/values). 此檔案用來設定main.xml檔中的變數初始值. 如
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello, Android! I am a string resource!</string>
<string name="app_name">Hello, Android</string>
</resources>


Step 4: 修改主要的java程式(我們的例子為helloandroid.java). 使得此java程式會去載入main.xml檔案內容以建立畫面結構.
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
若是你用Eclipse去產生android project. Eclipse會產生一R.java class 內含所有resource(也包含main.xml). R.layout.main就是一個compiled object代表著main.xml的內容。A project's R.java file is an index into all the resources defined in the file.


Step 5: 執行程式(在此我們利用Eclipse執行此project). 如此就可以在android emulator中看到畫面。

What is Activity?

l   Each activity is independent of the others. Each one is implemented as a subclass of the Activity base class.

l   An application might consist of just one activity or, like the text messaging application just mentioned, it may contain several.

l   One of the activities is marked as the first one that should be presented to the user when the application is launched.

l   Each activity is given a default window to draw in.

l   As an activity transitions from state to state, it is notified of the change by calls to the following protected methods:

void onCreate(Bundle savedInstanceState) 

void onStart() 

void onRestart() 

void onResume()

void onPause()

void onStop()

void onDestroy()

l   All of these methods are hooks that you can override to do appropriate work when the state changes. All activities must implement onCreate() to do the initial setup when the object is first instantiated. Many will also implementonPause() to commit data changes and otherwise prepare to stop interacting with the user.