728x90
반응형

실습 테스트 환경

OS : Mac OS 10.x, Ubuntu 12.04, Windows XP

Eclipse : Android Developer Tools Build: v22.0.5-757759

Project Build Target : Android 4.12 API Level 16

Emulator API Level : 본문 참조

[step1] File -> New -> Project -> Android Application Project

[step2] res -> values -> strings.xml

※ 프로젝트의 문자열은 리소스화(strings.xml)하여 사용하는 것이 원칙이나 예제파일에선 편의상 리터럴 사용

[step3] res -> layout -> activity_main.xml


  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:orientation="vertical" >
  5. <Button
  6. android:id="@+id/button1"
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="액션 호출 Intent(암시적 호출)" />
  10. <Button
  11. android:id="@+id/button2"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:text="액티비티 호출 Intent(명시적 호출)" />
  15. <Button
  16. android:id="@+id/button3"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:text="호출시 값 전달 Intent" />
  20. <Button
  21. android:id="@+id/button4"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:text="호출 후 값 받아오는 Intent" />
  25. </LinearLayout>


[step4] res -> layout -> activity_sub.xml


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:id="@+id/textView1"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="SubActivity" />
  11. <Button
  12. android:id="@+id/response"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:text="응답하기" />
  16. </LinearLayout>

[step5] src -> package -> MainActivity.java

  1. package zeroday.intent_test;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.Toast;
  10. public class MainActivity extends Activity {
  11. public static final int REQUEST_CODE = 1;
  12. private Button button1, button2, button3, button4;
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. // findViewById()
  17. button1 = (Button)findViewById(R.id.button1);
  18. button2 = (Button)findViewById(R.id.button2);
  19. button3 = (Button)findViewById(R.id.button3);
  20. button4 = (Button)findViewById(R.id.button4);
  21. // 액션 호출 Intent(암시적 호출)
  22. button1.setOnClickListener(new View.OnClickListener() {
  23. public void onClick(View v) {
  24. Uri uri = Uri.parse("http://cafe.naver.com/jjdev");
  25. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  26. // Uri uri = Uri.parse("tel:01012345678");
  27. // Intent intent = new Intent(Intent.ACTION_DIAL, uri);
  28. startActivity(intent);
  29. }
  30. });
  31. // 액티비티 호출 Intent(명시적 호출)
  32. button2.setOnClickListener(new View.OnClickListener() {
  33. public void onClick(View v) {
  34. Intent intent = new Intent(MainActivity.this, SubActivity.class);
  35. startActivity(intent);
  36. }
  37. });
  38. // 호출시 값 전달 Intent
  39. button3.setOnClickListener(new View.OnClickListener() {
  40. public void onClick(View v) {
  41. Intent intent = new Intent(MainActivity.this, SubActivity.class);
  42. intent.putExtra("name", "zeroDay");
  43. startActivity(intent);
  44. }
  45. });
  46. // 호출 후 값 받아오는 Intent -> 응답이 있으면 onActivityResult 메서드를 콜백
  47. button4.setOnClickListener(new View.OnClickListener() {
  48. public void onClick(View v) {
  49. Intent intent = new Intent(MainActivity.this, SubActivity.class);
  50. startActivityForResult(intent , REQUEST_CODE); // RESULT_CODE = 1 는 MainActivity final 변수
  51. }
  52. });
  53. }
  54. // 인텐트 호출 후 응답값이 있으면 콜백
  55. protected void onActivityResult(int requestCode, int resultCode, Intent data){
  56. super.onActivityResult(requestCode, resultCode, data);
  57. Log.d("intent_test", "resultCode:"+resultCode);
  58. Log.d("intent_test", "requestCode:"+requestCode);
  59. if(resultCode == Activity.RESULT_OK){
  60. if(requestCode == this.REQUEST_CODE){
  61. Toast.makeText(this,
  62. data.getStringExtra("result"),
  63. Toast.LENGTH_LONG).show();
  64. }
  65. }
  66. }
  67. }

- 코드설명


[step6] src -> package -> SubActivity.java

  1. package zeroday.intent_test;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.Toast;
  8. public class SubActivity extends Activity {
  9. Button response;
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_sub);
  13. Intent intent = getIntent();
  14. Toast.makeText(this,
  15. "NAME:"+intent.getStringExtra("name"),
  16. Toast.LENGTH_LONG).show();
  17. response = (Button)findViewById(R.id.response);
  18. response.setOnClickListener(new View.OnClickListener() {
  19. public void onClick(View v) {
  20. Intent responseIntent = new Intent();
  21. responseIntent.putExtra("result", "SubActivity로 부터 받은 값");
  22. setResult(Activity.RESULT_OK, responseIntent);
  23. finish();
  24. }
  25. });
  26. }
  27. }

- 코드설명


[step5] AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="zeroday.intent_test"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="18" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="zeroday.intent_test.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. <activity
  23. android:name="zeroday.intent_test.SubActivity"
  24. android:label="SubActivity" >
  25. </activity>
  26. </application>
  27. </manifest>

[step6] 실행


 

 

728x90
반응형
블로그 이미지

nineDeveloper

안녕하세요 현직 개발자 입니다 ~ 빠르게 변화하는 세상에 뒤쳐지지 않도록 우리모두 열심히 공부합시다 ~! 개발공부는 넘나 재미있는 것~!

,