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
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:orientation="vertical" >
-
-
<Button
-
android:id="@+id/button1"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="액션 호출 Intent(암시적 호출)" />
-
-
<Button
-
android:id="@+id/button2"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="액티비티 호출 Intent(명시적 호출)" />
-
-
<Button
-
android:id="@+id/button3"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="호출시 값 전달 Intent" />
-
-
<Button
-
android:id="@+id/button4"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="호출 후 값 받아오는 Intent" />
-
-
</LinearLayout>
[step4] res -> layout -> activity_sub.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical" >
-
-
<TextView
-
android:id="@+id/textView1"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="SubActivity" />
-
-
<Button
-
android:id="@+id/response"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="응답하기" />
-
-
</LinearLayout>
[step5] src -> package -> MainActivity.java
-
package zeroday.intent_test;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.net.Uri;
-
import android.os.Bundle;
-
import android.util.Log;
-
import android.view.View;
-
import android.widget.Button;
-
import android.widget.Toast;
-
-
public class MainActivity extends Activity {
-
public static final int REQUEST_CODE = 1;
-
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
// findViewById()
-
-
// 액션 호출 Intent(암시적 호출)
-
Uri uri = Uri.parse("http://cafe.naver.com/jjdev");
-
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
-
// Uri uri = Uri.parse("tel:01012345678");
-
// Intent intent = new Intent(Intent.ACTION_DIAL, uri);
-
startActivity(intent);
-
}
-
});
-
-
// 액티비티 호출 Intent(명시적 호출)
-
Intent intent = new Intent(MainActivity.this, SubActivity.class);
-
startActivity(intent);
-
}
-
});
-
-
// 호출시 값 전달 Intent
-
Intent intent = new Intent(MainActivity.this, SubActivity.class);
-
intent.putExtra("name", "zeroDay");
-
startActivity(intent);
-
}
-
});
-
-
// 호출 후 값 받아오는 Intent -> 응답이 있으면 onActivityResult 메서드를 콜백
-
Intent intent = new Intent(MainActivity.this, SubActivity.class);
-
startActivityForResult(intent , REQUEST_CODE); // RESULT_CODE = 1 는 MainActivity final 변수
-
-
}
-
});
-
}
-
-
// 인텐트 호출 후 응답값이 있으면 콜백
-
protected void onActivityResult(int requestCode, int resultCode, Intent data){
-
super.onActivityResult(requestCode, resultCode, data);
-
-
Log.d("intent_test", "resultCode:"+resultCode);
-
Log.d("intent_test", "requestCode:"+requestCode);
-
-
if(resultCode == Activity.RESULT_OK){
-
if(requestCode == this.REQUEST_CODE){
-
Toast.makeText(this,
-
data.getStringExtra("result"),
-
Toast.LENGTH_LONG).show();
-
}
-
}
-
}
-
}
- 코드설명
[step6] src -> package -> SubActivity.java
-
package zeroday.intent_test;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.Button;
-
import android.widget.Toast;
-
-
public class SubActivity extends Activity {
-
Button response;
-
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_sub);
-
-
Intent intent = getIntent();
-
Toast.makeText(this,
-
"NAME:"+intent.getStringExtra("name"),
-
Toast.LENGTH_LONG).show();
-
-
-
Intent responseIntent = new Intent();
-
responseIntent.putExtra("result", "SubActivity로 부터 받은 값");
-
setResult(Activity.RESULT_OK, responseIntent);
-
finish();
-
}
-
});
-
}
-
}
- 코드설명
[step5] AndroidManifest.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-
package="zeroday.intent_test"
-
android:versionCode="1"
-
android:versionName="1.0" >
-
-
<uses-sdk
-
android:minSdkVersion="8"
-
android:targetSdkVersion="18" />
-
-
<application
-
android:allowBackup="true"
-
android:icon="@drawable/ic_launcher"
-
android:label="@string/app_name"
-
android:theme="@style/AppTheme" >
-
<activity
-
android:name="zeroday.intent_test.MainActivity"
-
android:label="@string/app_name" >
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
-
</activity>
-
<activity
-
android:name="zeroday.intent_test.SubActivity"
-
android:label="SubActivity" >
-
</activity>
-
</application>
-
-
</manifest>
-
[step6] 실행
728x90
반응형
'날타그리드' 카테고리의 다른 글
트위터 부트스트랩 (0) | 2013.12.18 |
---|---|
부트스트랩 폼빌더 (0) | 2013.12.18 |
[SpringSecurity] 스프링 시큐리티 따라하기 - 기초 설정 (1) | 2013.12.18 |
안드로이드(Android) 스마트폰 USB 연결 디버깅 환경셋팅 (0) | 2013.11.11 |
[ 안드로이드 ] LogCat 사용법 및 Activity Life Cycle (0) | 2013.11.11 |
다음맵참조 (0) | 2013.11.11 |