본문 바로가기
  • We are looking for banner advertisers Please refer to the email below
카테고리 없음

안드로이드 스튜디오 버튼 클릭 Intent ACTION_VIEW Uri.parse 활용방법

by 크론크롱 2022. 5. 4.
반응형

액티비티에서의 버튼 클릭시 버튼에 Intent 함수를 사용하여 브라우저, 전화걸기, 구글맵, SMS전송 등의 기능구현 하는 방법에 대해 알아보도록 합니다

 

기능구현의 방법

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/buton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="ButtonClick1"
        android:text="버튼1"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/buton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="ButtonClick2"
        android:text="버튼2"
        tools:ignore="MissingConstraints"
        app:layout_constraintTop_toBottomOf="@+id/buton1"
        />

    <Button
        android:id="@+id/buton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="ButtonClick3"
        android:text="버튼3"
        tools:ignore="MissingConstraints"
        app:layout_constraintTop_toBottomOf="@+id/buton2"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

버튼을 위와같이 생성해주도록 한 뒤, 액티비티로 이동하여 Intent 함수를 활용해 값을 넘겨주도록 합니다

 

Intent를 사용하여 값을 넘겨주는 함수를 사용합니다 소스는 아래와 같습니다

package com.example.hello;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

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


    public void ButtonClick1(View v) {
        Toast.makeText(this, "버튼클릭완료", Toast.LENGTH_SHORT).show();
    }

    public void ButtonClick2(View v) {
        Intent MyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.naver.com"));
        startActivity(MyIntent);
    }

    public void ButtonClick3(View v) {
        Intent MyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:xxx@abc.com"));
        startActivity(MyIntent);
    }

}

 

실행 화면

버튼 2 버튼을 누른 경우 값을 http://m.naver.com 로 이동시켰기 때문에 위와같이 인터넷 창이 확인되며 네이버로 이동됩니다

 

 

버튼3의 경우 이메일주소로 파라미터 값을 넘겨주었기 때문에 이메일을 보낼 수 있는 앱이 실행됩니다

 

mailto:xxx@abc.com 으로 값 전송

 

 

Uri.parse 값 여러가지 활용 방법

 

웹페이지 이동

    public void ButtonClick2(View v) {
        Intent MyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.naver.com"));
        startActivity(MyIntent);
    }

 

 

    public void ButtonClick2(View v) {
        Intent MyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:38.899533,-77.036476"));
        startActivity(MyIntent);
    }

 

구글 길찾기 실행

    public void ButtonClick2(View v) {
        Intent MyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com/maps?f=d&saddr=startLat+startLng&daddr=endLat+endLng&hl=en"));
        startActivity(MyIntent);
    }

 

 

전화걸기 실행

    public void ButtonClick2(View v) {
        Intent MyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:070-0000-0000"));
        startActivity(MyIntent);
    }

 

문자보내기 실행

    public void ButtonClick2(View v) {
        Intent MyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("smsto:010-0000-0000"));
        startActivity(MyIntent);
    }

 

파일 보내기

    public void ButtonClick2(View v) {
        Intent MyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://파일보내기"));
        startActivity(MyIntent);
    }

 

이메일 보내기

    public void ButtonClick2(View v) {
        Intent MyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:id@email.com"));
        startActivity(MyIntent);
    }

 

파일 실행

    public void ButtonClick2(View v) {
        Intent MyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("file://folder/file.mp4"));
        startActivity(MyIntent);
    }

 

참고자료 : https://developer.android.com/reference/android/net/Uri

 

Uri  |  Android Developers

android.net.wifi.hotspot2.omadm

developer.android.com

 

반응형

댓글