Intent에는 명시적(Explicit)과 암시적(Implicit)가 존재한다.
보통 Activity간에 이동으로 자주 사용하는 Intent는 명시적(Explicit) Intent를 사용한다.
Intent를 이용하여 Service를 실행하거나 특정 App을 실행하기 위해서는 암시적(Implicit) Intent가 사용된다.
Explicit Intent의 기본 코드는 아래와 같다.
Intent intent = new Intent(getBaseContext(), 이동 할 Activity.class); startActivity(intent); |
Implicit Intent의 기본 코드는 아래와 같다.
Intent intent = new Intent(intent-filter의 action name); startActivity(intent); |
Explicit Intent는 이동 할 위치가 명확하게 지정되어 있는 반면에 Implicit Intent는 두리뭉실하게 지정되어 있다.
풀어서 설명하자면
Explicit Intent는 누구를 콕 찝어서 도와주세요...라고 한다. (Ex : A씨 이것 좀 도와주세요.)
Implicit Intent는 대중에게 도와주세요...라고 한다. (Ex : 이것 좀 도와주실 분 안계신가요?)
Implicit Intent를 이용하여 다른 앱을 호출 할 수 있다.
대표적으로 사용되는 것 중 전화걸기를 사용해보도록 하겠다.
특정 버튼을 클릭하였을때 지정된 전화번호로 전화걸기가 되는 앱 이다.
프로젝트를 생성하고 activity_main.xml에 버튼을 3개 추가한다.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ACTION_CALL"
android:onClick="onImpliit2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ACTION_DIAL"
android:onClick="onImplicit3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ACTION_VIEW"
android:onClick="onImplicit4" />
MainActivity.java에 클릭 이벤트에 사용 할 메서드를 정의한다.
public void onImplicit2(View v) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:010-0000-0000"));
try {
startActivity(intent);
} catch (SecurityException e) {
e.printStackTrace();
}
}
public void onImplicit3(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:010-0000-0000"));
startActivity(intent);
}
public void onImplicit4(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.hellohiro.net"));
startActivity(intent);
}
AndroidManifest.xml에 권한을 추가 한다.
<uses-permission android:name="android.permission.CALL_PHONE" />
onImplicit2는 바로 전화걸기가 되는 Intent
onImplicit3은 다이얼 창으로 전환되는 Intent
onImplicit4는 웹브라우저에 해당 주소로 연결되는 Intent
onImplicit2의 경우는 예외처리를 해줘야 정상적으로 동작을 한다.
예외처리가 싫다면...Intent.ACTION_CALL 대신 "android.intent.action.CALL"을 입력해주면 된다.
Intent.ACTION_DIAL도 "android.intent.action.DIAL"로 대체 가능하다. (편한 것으로 사용 하면 되겠다.)
'Android' 카테고리의 다른 글
Extra - PutExtra, getExtra (0) | 2016.08.05 |
---|---|
startActivityForResult (0) | 2016.08.05 |
Zxing 라이브러리를 사용한 바코드 스캔 (0) | 2016.01.12 |
Android Studio, GCM 3.0을 사용하여 Push 구현 (0) | 2015.12.04 |
APK 파일 디컴파일(Decompile) 하는 방법 (0) | 2015.12.01 |