ImplicitIntent.zip


Result


    





activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="32dp"
    tools:context=".MainActivity">
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="웹 사이트 열기"
        android:textSize="20sp"
        android:onClick="onClickOpenWebpageButton"/>
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="지도에서 위치 열기"
        android:textSize="20sp"
        android:onClick="onClickOpenAddressButton"/>
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="텍스트를 공유하기"
        android:textSize="20sp"
        android:onClick="onClickShareTextButton"/>
 
</LinearLayout>
cs




MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.tistory.qlyh8.implicitintent;
 
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.ShareCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
 
public class MainActivity extends AppCompatActivity {
 
    private String url = "http://qlyh8.tistory.com";
    private String address = "강남역";
    private String shareText = "공유할 텍스트";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    //버튼을 누르면 지정한 웹 페이지를 보여준다.
    public void onClickOpenWebpageButton(View view) {
        //파라미터로 받은 URL 을 파싱하여 ACTION_VIEW 를 통해 특정 콘텐츠를 볼 수 있다.
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
 
        // 경우에 따라 이 코드가 실행되는 장치에는 지정한 데이터로 작업을 수행하기 위한 활동이 없을 수도 있다.
        // 이 검사가 없으면 앱이 죽는다.
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
 
    //버튼을 누르면 해당되는 위치의 지도를 보여준다.
    public void onClickOpenAddressButton(View view) {
        //URI를 생성하는 방법 1. parse() 이용 2. builder 이용 (권장)
        //Builder 를 이용해 URI 를 생성한다.
        //Uri.Builder builder = new Uri.Builder();
        /*
        * geo URI 는 geo scheme, 위도와 경도, 그리고
        * 옵션으로 들어가는 도로명 주소나 회사명을 나타내는 쿼리 파라미터와 줌 레벨로 구성되어 있다.
        * 쿼리 파라미터가 있다면 위도와 경도 path 는 반드시 “0,0”이 되어야 한다.
        * geo:0,0?q=Antwerp,Belgium&z=10 를 예로 들었을 때
        * 벨기에의 Antwerp 라는 도시를 줌 레벨 10으로 보여주는 geo 의 URI 이다.
        * */
        /*Uri addressUri = builder.scheme("geo")
                                .path("0,0")
                                .query(address)
                                .build();*/
        Uri addressUri = Uri.parse("geo:0,0?q=" + address);
 
        //지정된 URL 을 요청하는 인텐트를 생성한다.
        Intent intent = new Intent(Intent.ACTION_VIEW);
        //인텐트에 URI 를 설정한다.
        intent.setData(addressUri);
 
        //인텐트를 시작할 수 있는지 검사한다.
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
 
    //공유 버튼을 누르면 디바이스에서 해당 타입의 인텐트를 처리할 수 있는 모든 앱을 보여준다.
    public void onClickShareTextButton(View view) {
        /*
        * MIME 형식은 어떤 응용 프로그램이 어떤 내용을 열 수 있는지 컴퓨터가 판단하는 데 도움이 된다.
        * MIME (Multipurpose Internet Mail Extension, 다목적 인터넷 메일 확장자)
        * 예를 들어 .pdf 파일을 두 번 클릭하면 PDF 를 열 수있는 프로그램 목록이 표시된다.
        * MIME 유형을 text/plain 을 지정하면 이 특정 인텐트에서 startActivity 를 호출 할 때
        * 텍스트 콘텐츠를 어떤 방식으로든 처리할 수 있는 모든 앱이 제공된다.
        *
        * top-level type name / subtype name [ ; parameters ]
        * top-level type 과 Sub type, 그리고 옵션 파라미터로 구성된다.
        * type/html; charset=UTF-8 을 예로 들면
        * 데이터의 타입은 HTML 텍스트, UTF-8 문자셋 인코딩으로 구성되어 있다.
        * 일반 텍스트 ex) text/plain
        * png 이미지 파일 ex) image/png
        *
        * 데이터를 공유할 때는 반드시 미디어 타입을 명시해서
        * 안드로이드가 요청을 어떻게 처리할 수 있는지,
        * 또는 처리가 가능한 지 여부를 결정할 수 있게 해줘야 한다.
        * */
        String mimeType = "text/plain";
 
        /*
        * ShareCompat 이라는 헬퍼 클래스와 이것의 내부 클래스 IntentBuilder 는
        * 데이터 타입 또는 파일의 개수 등의 고려할 사안들을 추상화해 일련의 작업을 쉽게 해준다.
        * */
        ShareCompat.IntentBuilder
                /* from 메서드는 공유가 들어오는 컨텍스트를 지정한다. */
                .from(this)
                /* 미디어 타입 */
                .setType(mimeType)
                /* startActivity 를 호출 할 때 나타나는 팝업 제목 */
                .setChooserTitle("Title for Sharing")
                .setText(shareText)
                .startChooser();
    }
}
 
cs



Android Course of Udacity - Lesson 4

+ Recent posts