1 / 46

Android Application Development

Android Application Development. Lecture II: Application Fundamentals. Contents - Lectures. I . Startup. II . Application Fundamentals. II - 1. Layout. II - 2. Activity. II - 3. Intent. III . UI and data control. IV . Provider/service/location/map. Contents - Lecture II.

amalie
Download Presentation

Android Application Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Android Application Development • Lecture II: Application Fundamentals

  2. Contents - Lectures I. Startup II. Application Fundamentals II - 1. Layout II - 2. Activity II - 3. Intent III. UI and data control IV. Provider/service/location/map

  3. Contents - Lecture II II - 1. Layout (レイアウト) II - 1.1. View II - 1.2. ViewGroup II - 2. Activity (アクティビティ) II - 3. Intent (インテント)

  4. II - 1.1. レイアウト - View Viewとは、Button, Textなどの画面を構成する1個1個の部品のようなもの ImageButton CheckBox DatePicker RadioBox ToggleButton RatingBar Spinner EditText

  5. II - 1.2. レイアウト - ViewGroup LinearLayout 行単位 TabHost

  6. II - 1.2. レイアウト - ViewGroup GridView ListView

  7. Contents - Lecture II II - 1. Layout (レイアウト) II - 2. Activity (アクティビティ) II - 2.1. What is Activity II - 2.2. Lifecycle II - 2.3. UML modeling (base) II - 2.4. Improve the source code II - 2.5. UML modeling (improved) II - 3. Intent (インテント)

  8. II - 2.1. Activity (アクティビティ) とは  アクティビティは、ユーザーに対してUIを提供したり、ユーザーからのイベントに応答したりといった、ユーザーとアプリケーションとの間で行われるやり取り全般を仲介するものである。 App Title 0 00 00 0 OnCreate() Button Button UI OnClick() 操作 View レイアウト (By XML or source code) アクティビティ (..Activity.java) ユーザ側

  9. II - 2.1. Activity (アクティビティ) アクティビティの特徴 • 画面をもった機能の単位(画面クラスのようなもの)。多くのケースでは、1つの画面 • あたり1つのアクティビティ(TableViewクラスでは、1つの画面で複数のアクティビティを実装) • アプリケーションは、必ず1つのアクティビティを有し、その中のいずれかの • アクティビティはアプリケーションのEntryとなる(C言語のmain関数に相当) 主なメソッド http://developer.android.com/intl/ja/reference/android/app/Activity.html • protected void onCreate() • protected void onStart() • protected void onStop() • void setContentView(intlayoutResID) • void finish() • View findViewById(int id) • void startActivity(Intent intent) Activityのinstanceが生成される時に実行、初期処理 ActivityのUIが表示されるときに実行 ActivityのUIが消されるときに実行 layoutResIDに対するビューをUIにセットする Activityを終了する idに対するLayoutファイルにあるViewを取得する 他のactivityを起動する もう一度HelloOkinawaのソースを確認ましょう

  10. II - 2.2. アクティビティのライフサイクル Activity starts OnCreate() User navigates comes to the foreground OnStart() OnRestart() OnResume() Process is killed The activity comes to the foreground Activity is running Another activity comes in front of the activity The activity comes to the foreground OnPause() Other applications need memory The activity is no longer visible OnStop() OnDestroy() Activity is shut down

  11. II - 2.2. Activity lifecycle: ソースコードから確認 // Append Log.v() to OnCreate(), OnStart(), OnStop() method @Override public void onCreate(BundlesavedInstanceState) { … Log.v("TEST", "in onCreate method..."); } @Override protected void onStart() { super.onStart(); Log.v("TEST", "in onStart method..."); } @Override protected void onStop() { super.onStop(); Log.v("TEST", "in onStop method..."); } 上記のメソッドとOnPause(), OnResume() を下記の操作でLogCatで確認しましょう • ddmsから電話を書ける • Backバタンを押す • 画面を横/縦にする (Ctrl + F12, Ctrl+F11)

  12. II - 2.3. UML modeling (base) – UseCase diagram

  13. II - 2.3. UML modeling (base) – class diagram

  14. II - 2.3. UML modeling (base) – Sequence diagram

  15. II - 2.3. UML modeling (base) – Activity diagram

  16. II - 2.3. UML modeling (base) – State machine diagram

  17. II - 2.4. Improve the source code - Hello, Okinawa 機能追加 下記のウィジェットと機能を追加する • Click meボタンを追加し、押すとDialog Boxに「hello」文字を示す • Closeボタンを追加する。押すと、このアクティビティを終了させる

  18. II - 2.4. Improve the source code - main window: main.xml main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <TextViewandroid:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/> <Button android:id="@+id/button_clickMe" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/buttonTitle_clickMe" /> <Button android:id="@+id/button_close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="close" /> </LinearLayout> Point to buttonTitle_clickMein strings.xml Confirm the changes in R.java, and run the application again

  19. II - 2.4. Improve the source code HelloOkinawa_activity.java (1) In OnCreate() method Button button_clickme= (Button) findViewById(R.id.button_clickMe); Button button_close = (Button) findViewById(R.id.button_close); button_clickme.setOnClickListener(button_clickmeListener); button_close.setOnClickListener(button_closeListener); (2) Append two private variables button_clickmeListener,button_closeListener private View.OnClickListenerbutton_clickmeListener= new View.OnClickListener() { @Override public void onClick(Viewv) { // clicked the button "Click me” -- next slide } }; private View.OnClickListenerbutton_closeListener= new View.OnClickListener() { @Override public void onClick(Viewv) { HelloOkinawa_activity.this.finish(); } };

  20. II - 2.4. Improve the source code HelloOkinawa_activity.java // clicked the button "Click me” final AlertDialog.Builderbuilder = new AlertDialog.Builder (HelloOkinawa_activity.this); builder.setTitle("Hi...(title)"); builder.setPositiveButton("Dismiss", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { setResult(RESULT_OK); } }); builder.setMessage("hello"); builder.create(); builder.show();

  21. II - 2.5. UML modeling (improved) – UseCase diagram

  22. II - 2.5. UML modeling (improved) – class diagram Rが省略 属性ではなく、 Property(属性+関連方向)で定義

  23. II - 2.5. UML modeling (improved) – Sequence diagram View.OnClickListenerはこのアクティビティに所属するため、別のLifelineを作る必要がない

  24. II - 2.5. UML modeling (improved) – Activity diagram

  25. II - 2.5. UML modeling (improved) – State machine diagram

  26. Contents - Lecture II II - 1. Layout (レイアウト) II - 2. Activity (アクティビティ) II - 3. Intent (インテント) II - 3.1. What is Intent? II - 3.2. Explicit Intent (明示的インテント) II - 3.3. Implicit Intent (暗黙的インテント) II - 3.4. Action, data, etc. of intent II - 3.5. UML modeling for intent

  27. II - 3.1. Intent (インテント)  インテントには、なんらかの意図(やりたいこと)という意味がある。主にアクティビティの切り替え、そしてアクティビティを起動する際のパラメータに使われる。 Android OSはIntentのStackを管理する。 下記のウィジェットと機能を追加する Tap this button to show the result on the next window

  28. II - 3.1. Intent (インテント) HelloOkinawa activity Result activity Intentは、こんなことをおこなう 送信元 受信先 • x, yの値を取り出す • Result activity のオブジェクトを生成する • x, yの値をパラメータとして設定する • Result activity のinstanceを起動する

  29. II - 3.2. Intent (明示的インテント) 送信元(Intentに、ContextとClassをセットする。処理するActivityを指名)

  30. II - 3.2. Intent (明示的インテント) AndroidManifest.xml android:nameをコンパイルすると、packageのネームが付加される 受信先 Bundle extras = getIntent().getExtras(); if(extras != null) { doublex = extras.getDouble(“x”); // xの値を取り出す doubley = extras.getDouble("y"); // yの値を取り出す … }

  31. II - 3.2. ソースコードからみる

  32. II - 3.3. Intent (暗黙的インテント) 送信元(Intentに、処理するActivityのClassを指定しない) AndroidManifest.xml 受信先

  33. II - 3.3.暗黙的Intent例 Example 1 Example 2

  34. II - 3.4. インテントが持つ情報、クラスのコンストラクタ

  35. II - 3.4. 主なアクション、カテゴリ一覧

  36. II - 3.4. インテントクラスの主なメソッド

  37. II - 3.5. Intentを利用したプログラムのUML図は

  38. II - 3.5. UML model – UseCase diagram

  39. II - 3.5. UML model – class diagram

  40. II - 3.5. UML model – Sequence diagram(1) /* Log of activity lifecycle a01: the first activity a02: the second activity */ // Launch the application a01 onCreate a01 onStart a01 onResume // tap to enter the second activity a01 onPause a02 onCreate a02 onStart a02 onResume a01 onStop // tap "OK" to return the first activity a02 onPause a01 onRestart a01 onStart a01 onResume a02 onStop a02 onDestroy // press the Back key a01 onPause a01 onStop a01 onDestroy

  41. II - 3.5. UML model – Sequence diagram(2) First activity Second activity

  42. II - 3.5. UML model – Sequence diagram(3)

  43. II - 3.5. UML model – Sequence diagram(4)

  44. II - 3.5. UML model – Sequence diagram(5)

  45. II - 3.5. UML model – Activity diagram

  46. II - 3.5. UML model – State machine diagram

More Related