Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAP 10. 액티비티와 인텐트.

Similar presentations


Presentation on theme: "CHAP 10. 액티비티와 인텐트."— Presentation transcript:

1 CHAP 10. 액티비티와 인텐트

2 4가지의 중요한 개념 애플리케이션(application) 액티비티(activities)
액티비티 스택(activity stack) 태스크(task)

3 애플리케이션 한 개 이상의 액티비티들로 구성된다. 액티비티들은 애플리케이션 안에서 느슨하게 묶여 있다.

4 액티비티 애플리케이션을 구성하는 빌딩 블록

5 태스크 스택에 있는 액티비티

6 액티비티 스택 Back 키를 누르면 현재 액티비티를 제거하고 이전 액티비티로 되돌아 간다.
사용자가 방문한 액티비티들은 어딘가에 기억

7 다음과 같은 상태도 가능하다

8 인텐트 각각의 화면은 별도의 액티비티로 구현된다.
하나의 액티비티(화면)에서 다른 액티비티(화면)로 전환하려면 어떻게 하여야 하는가?

9 인텐트 다른 액티비티를 시작하려면 액티비티의 실행에 필 요한 여러 가지 정보들을 보내주어야 한다.
정보를 인텐트에 실어서 보낸다.

10 인텐트의 종류 명시적 인텐트(explicit intent) 암시적 인텐트(implicit intent)
“애플리케이션 A의 컴포넌트 B를 구동시켜라“와 같이 명확하게 지정 암시적 인텐트(implicit intent) “지도를 보여줄 수 있는 컴포넌트이면 어떤 것이라도 좋다”

11 명시적인 인텐트 실행하고자 하는 액티비티의 이름을 적어 준다.
Intent intent = new Intent(this, NextActivity.class); startActivity(intent);

12 명시적인 인텐트 예제 여기서 두 개의 액티비티로 이루어진 애플리케이션 을 작성하여 보자. 첫 번째 액티비티는 Activity1, 두 번째 액티비티는 Activity2라고 하자.

13 레이아웃 파일 layout1.xml

14 레이아웃 파일 layout2.xml

15 Activity1.java

16 Activity2.java

17 메니페스트 파일

18 실행 결과

19 여러 페이지로 된 애플리케이션 작성

20 액티비티에서 결과받기

21 값을 저장하고, 값을 읽는 메소드

22 예제

23 레이아웃 파일 main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:text="서브 액티비티로부터 문자열 반환받기“ android:layout_height="wrap_content" </Button> <TextView android:layout_width="wrap_content" android:text="반환된 문자열“ </TextView> <TextView android:layout_height="wrap_content" android:text="____________________" android:layout_width="match_parent"> </LinearLayout>

24 레이아웃 파일 sub.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:layout_height="wrap_content" > <requestFocus></requestFocus> </EditText> …

25 레이아웃 파일 sub.xml <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" > <Button android:layout_width="wrap_content" android:layout_weight="1" android:text="입력완료" > </Button> android:text="취소" > </LinearLayout>

26 매니페스트 파일 수정 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=" ... <application <activity android:name="kr.co.company.activityforresult.MainActivity" > </activity> android:name=".SubActivity" android:label="SubActivity" > </application> </manifest>

27 Mainactivity.java ... public class MainActivity extends Activity { static final int GET_STRING = 1; TextView public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.button); text = (TextView) findViewById(R.id.text); button.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { Intent in = new Intent(MainActivity.this, SubActivity.class); startActivityForResult(in, GET_STRING); } }); }

28 Mainactivity.java protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == GET_STRING) { if (resultCode == RESULT_OK) { text.setText(data.getStringExtra("INPUT_TEXT")); }

29 Subactivity.java ... public class SubActivity extends Activity { EditText edit; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sub); edit = (EditText) findViewById(R.id.edit); Button button_ok = (Button) findViewById(R.id.button_ok); button_ok.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("INPUT_TEXT", edit.getText().toString()); setResult(RESULT_OK, intent); finish(); } });

30 Subactivity.java Button button_cancel = (Button) findViewById(R.id.button_cancel); button_cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { setResult(RESULT_CANCELED); finish(); } });

31 실행 결과

32 암시적인 인텐트 어떤 작업을 하기를 원하지만 그 작업을 담당하는 컴포넌트의 이름을 명확하게 모르는 경우에 사용

33 암시적인 인텐트의 형식 Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_ , recipientArray); startActivity(intent);

34 액션의 종류 상수 타겟 컴포넌트 액션 ACTIN_VIEW 액티비티 데이터를 사용자에게 표시한다. ACTION_EDIT
사용자가 편집할 수 있는 데이터를 표시한다. ACTION_MAIN 태스크의 초기 액티비티로 설정한다. ACTION_CALL 전화 통화를 시작한다. ACTION_SYNC 모바일 장치의 데이터를 서버 상의 데이터와 일치시킨다. ACTION_DIAL 전화 번호를 누르는 화면을 표시한다.

35 암시적인 인텐트 예

36 인텐트 예제

37 사용자 인터페이스 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_height="wrap_content" android:onClick="onClick" android:text="전화걸기" > </Button> android:text="지도보기" >

38 사용자 인터페이스 <Button android:id="@+id/web"
android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="웹브라우저" > </Button> android:text="연락처보기" > </LinearLayout>

39 암시적 인텐트 예제 public class ImplicitIntentActivity extends Activity {
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View view) { Intent intent = null; switch (view.getId()) { case R.id.web: intent = new Intent(Intent.ACTION_VIEW, Uri.parse(" break; case R.id.call: intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:(+82) "));

40 암시적 인텐트 예제 case R.id.map: intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:37.30,127.2?z=10")); break; case R.id.contact: Uri.parse("content://contacts/people/")); } if (intent != null) { startActivity(intent);

41 매니페스트 파일 수정 ... <uses-permission android:name="android.permission.CALL_PHONE" > </uses-permission> <uses-permission android:name="android.permission.CAMERA" > <uses-permission android:name="android.permission.READ_CONTACTS" > <uses-permission android:name="android.permission.INTERNET"/>

42 실행 결과

43 멀티태스킹 동시에 여러 태스크를 실행 현재의 태스크를 배경(background)으로 보내고 다 른 태스크를 전경(foreground)에서 시작할 수 있다.

44 멀티 태스킹의 예

45 HOME 키를 누르면 멀티 태스킹이 시작된다.

46 오버뷰 화면 최근에 사용된 액티비티들과 태스크들을 보여주는 화면

47 인텐트 필터 컴포넌트는 자신들이 처리할 수 있는 인텐트의 종류 를 인텐트 필터에 기록한다.

48 인텐트 필터

49 인텐트 필터 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=" package="kr.co.company.ImplicitIntent" android:versionCode="1" android:versionName="1.0"> <application <activity android:name=".ImplicitIntent" <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="9" /> <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> </manifest>

50 노트패드의 인텐트 필터 <activity android:name="NotesList" <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.EDIT" /> <action android:name="android.intent.action.PICK" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.GET_CONTENT" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </intent-filter> </activity>

51 액티비티 생애주기 실행 상태(resumed, running): 액티비티가 전경에 위치하고 있으며 사용자의 포커스를 가지고 있다. 일시멈춤 상태(paused): 다른 액티비티가 전경에 있 으며 포커스를 가지고 있지만 현재 액티비티의 일부 가 아직도 화면에서 보이고 있는 상태이다. 정지 상태(stopped): 액티비티는 배경에 위치한다.

52 액티비티 상태

53 액티비티 객체 생성 단계

54 일시 멈춤 상태

55 정지되었다가 다시 실행하는 경우

56 중요한 콜백 메소드 onCreate() onPause() 액티비티가 생성되면서 호출 중요한 구성요소들을 초기화
사용자가 액티비티를 떠나고 있을 때, 이 메소드가 호출 그 동안 이루어졌던 변경사항을 저장

57 액티비티 생애주기 예제 ... public class LifeCycleTest extends Activity {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onStart() { super.onStart(); Log.i("LifeCycle", "onStart() 호출"); public void onResume() { super.onResume(); Log.i("LifeCycle", "onResume() 호출");

58 액티비티 생애주기 예제 @Override public void onPause() { super.onPause();
Log.i("LifeCycle", "onPause() 호출"); } public void onStop() { super.onStop(); Log.i("LifeCycle", "onStop() 호출"); public void onDestroy() { super.onDestroy(); Log.i("LifeCycle", "onDestroy() 호출");

59 실행 결과

60 액티비티 상태 저장

61 액티비티 상태 저장 선택한 상품을 저장하는 애플리케이션 작성

62 상태 저장 및 복구 public class SaveRestoreTestActivity extends Activity { Button button1, button2; TextView text; int count = 0; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text = (TextView) findViewById(R.id.text); button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { count++; text.setText("현재 개수=" + count); } }); button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener() { public void onClick(View v) { count--;

63 상태 저장 및 복구 public class SaveRestoreTestActivity extends Activity { … public void onCreate(Bundle savedInstanceState) { if (savedInstanceState != null) { count = savedInstanceState.getInt("count"); text.setText("현재 개수=" + count); public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("count", count);

64 실행 결과


Download ppt "CHAP 10. 액티비티와 인텐트."

Similar presentations


Ads by Google