본문 바로가기

안드로이드 Android

[모바일프로그래밍] 8-3 암시적 인텐트

MainActivity.java

package com.example.myapplication;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClicked(View view) {
        Intent myIntent = null;
        switch (view.getId()) {
            case R.id.button_callphone:
                myIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:(+82)12345678"));
                break;
            case R.id.button2:
                myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.naver.com"));
                break;
        }
        if (myIntent != null) {
            startActivity(myIntent);
        }
    }
}

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button_callphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="111dp"
        android:layout_marginTop="109dp"
        android:onClick="onClicked"
        android:text="Call Phone"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="111dp"
        android:layout_marginTop="94dp"
        android:text="Web Browser"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_callphone" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

</manifest>

 

AVD는 인터넷에 연결되어 있지 않기 때문에 Call Phone 버튼은 실제 디바이스에서 테스트해야 한다.