IT/Android

android - 암시적 인텐트

노마드오브 2018. 9. 25. 20:50
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/btnDial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="전화걸기" />
<Button
android:id="@+id/btnWeb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="홈페이지 열기" />
<Button
android:id="@+id/btnGoogle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="구글 맵 열기" />
<Button
android:id="@+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="구글 검색하기" />
<Button
android:id="@+id/btnSms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="문자 보내기" />
<Button
android:id="@+id/btnPhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="사진 보기" />

</LinearLayout>



package com.example.it.myapplication8_6;

import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.File;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("암시적 인텐트 예제");

Button btnDial = (Button) findViewById(R.id.btnDial);
Button btnWeb = (Button) findViewById(R.id.btnWeb);
Button btnGoogle = (Button) findViewById(R.id.btnGoogle);
Button btnSearch = (Button) findViewById(R.id.btnSearch);
Button btnSms = (Button) findViewById(R.id.btnSms);
Button btnPhoto = (Button) findViewById(R.id.btnPhoto);

btnDial.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("tel:010-1234-5678");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);
}
});

btnWeb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("http://www.hanb.co.kr");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});

btnGoogle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("geo:37.5543, 126.9135");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "안드로이드");
startActivity(intent);
}
});
btnSms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra("sms_body", "안녕하세요?");
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
}
});
btnPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File("/sdcard/jeju13.jpg"));
intent.setDataAndType(uri, "image/jpeg");
startActivity(intent);
}
});

}
}