본문 바로가기

JAVA/Android

안드로이드 Intent 예제 따라하기

- 안드로이드에서 다음기능으로 넘어갈때(다른용도로도 많이 쓴다) 쓰는 기능인 Intent 를 사용해서 전화걸기, 구글들어가기, 사진보기, 내가 만든 기능으로 넘어가기 를 해보자.
- 여러 화면을 사용하려면, AndroidManifest.xml 이라는 곳에서 엑티비티를 사용하겠다고 추가해줘야 한다.
 
- Java 소스 

public class Ex05_ImplictyIntentActivity extends Activity implements
OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button b1 = (Button) this.findViewById(R.id.bu1);
b1.setOnClickListener(this);
Button b2 = (Button) this.findViewById(R.id.bu2);
b2.setOnClickListener(this);
Button b3 = (Button) this.findViewById(R.id.bu3);
b3.setOnClickListener(this);
Button b4 = (Button) this.findViewById(R.id.bu4);
b4.setOnClickListener(this);
}

public void onClick(View arg0) {
// TODO Auto-generated method stub
// String path = getFilesDir().getAbsolutePath();
Intent intent = null;

switch (arg0.getId()) {

case R.id.bu1:
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com/"));
startActivity(intent);
break;
case R.id.bu2:
intent = new Intent(Intent.ACTION_DIAL,
Uri.parse("tel:010-1234-1234"));
startActivity(intent);
break;
case R.id.bu3:
intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File("/sdcard/picture1.jpg"));//해당위치에 이미지가 있으면 출력
intent.setDataAndType(uri, "image/jpg");
startActivity(intent);
break;
case R.id.bu4:
intent = new Intent(this, Ex05_Calulator.class); //내가 만든 클래스 이것은 매니페스트에 추가해                                                                                              줘야 한다.
startActivity(intent);
break;
}

}


- xml 소스

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/go"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Button
        android:id="@+id/bu1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="구글 접속" />

    <Button
        android:id="@+id/bu2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="전화 걸기" />

    <Button
        android:id="@+id/bu3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="사진 보기" />

    <Button
        android:id="@+id/bu4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="계산기" />

</LinearLayout>

-  AndroidManifest.xml

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Ex05_ImplictyIntentActivity"
                  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=".Ex05_Calulator"    // 이런식으로 추가해야한다.
                  android:label="@string/app_name">
        </activity>

    </application>

- 실행화면