본문 바로가기
Android/Android

Android(5)_Intent

by 미티치 2016. 5. 31.

앞에 Activity에서 설명하면서 안드로이드 인텐트에 대해 간략하게나마 설명했었습니다. :)

이번 장에서는 Intent가 무엇인지, 어떻게 쓰는 건지 등에 대해 예제와 함께 부족하게나마 열심히 설명하겠습니다.

 

 

■ Intent란?

 

이전에 했던 설명에 앞부분을 반복하자면 인텐트란, 액티비티 전환시 액티비티 간의 정보교환 수단을 제공하는 것으로 한 액티비티에서 다른 액티비티 호출 시 실행에 필요한 여러 정보를 제공하는 것을 의미합니다. 정리하자면 앱 컴포넌트들 간의 정보 전달 수단을 제공하는 것이 인텐트이며, 수신 컴포넌트의 동작에 필요한 데이터를 information bundle을 포함하는 안드로이드의 비동기 IPC(Inter-Process Communication) 메커니즘입니다.

 

하지만 인텐트는 보안에 취약합니다. 즉, 스누핑 응용프로그램은 인텐트를 도청할 수 있기 때문입니다 ( any snooping application can sniff the intent ) 따라서 보안에 민감한 정보는 Intent에 두지 말 것을 권고한다고 합니다.

-> 네트워크 스누핑 프로그램은 네트워크에서 패킷을 인터셉트해서 때에 따라 변조하는 프로그램입니다. 그 안에서 메모리에 저장되어있는 데이터를 도청하기 때문에 인텐트에 중요한 정보가 담겨있다면 보안에 굉장히 취약할 수 밖에 없습니다.

 

 

 

■ 암시적 Intent

 

 암시적 인텐트란 특정 액티비티 명칭을 지정하지 않아 장치에서 지정된 해당 정보를 처리할 수 있는 어떤 컴포넌트를 알아서 찾아주는 것을 의미합니다. 앞서 액티비티에서 설명했던 예제를 그대로 가지고왔습니다. 여기 메인액티비티에서 버튼을 누르면 웹 브라우저를 켜고 www.naver.com 주소로 가기위해 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(http://www.naver.com)); 로 인텐트를 선언했습니다. 여기서 웹브라우저를 연다고 지정하지 않았지만 안드로이드는 uri 주소에 적합한 컴포넌트를 찾아서 연결해줍니다. 이게 암시적 인텐트입니다. 주로 내가 만든 앱에서 인텐트를 사용할 때는 앱 내부에 어떤 곳으로 이동할지 명확하게 클래스 이름을 알기때문에 명시적 인텐트를 사용하지만 이렇게 외부 앱에 연결할 때는 암시적 인텐트를 사용합니다.

 

♤ 예제

 

[ 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mijinpark.myapplication.MainActivity">
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="Mititch"
        android:layout_centerHorizontal="true" />
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Naver"
        android:id="@+id/buttonNaver"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true" />
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Call"
        android:id="@+id/buttonCall"
        android:layout_below="@+id/buttonNaver"
        android:layout_centerHorizontal="true" />
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Exit"
        android:id="@+id/buttonExit"
        android:layout_below="@+id/buttonGallary"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        />
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Gallary"
        android:id="@+id/buttonGallary"
        android:layout_below="@+id/buttonCall"
        android:layout_centerHorizontal="true"
        />
</RelativeLayout>
 
cs

 

 

[ 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
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
package com.example.mijinpark.myapplication;
 
import android.content.Intent;
import android.content.UriMatcher;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    Button button1;
    Button btnNaver;
    Button btnCall;
    Button btnExit;
    Button btnGallary;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button) findViewById(R.id.button1);  //레퍼런스 연결
        btnNaver = (Button) findViewById(R.id.buttonNaver);
        btnCall = (Button) findViewById(R.id.buttonCall);
        btnGallary = (Button) findViewById(R.id.buttonGallary);
        btnExit = (Button) findViewById(R.id.buttonExit);
 
        button1.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Toast.makeText(getApplicationContext(),"응~버튼눌렀어~",Toast.LENGTH_SHORT).show();   //lengthlong은 오래 뜨는거
            }
        });
 
        btnNaver.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.naver.com"));
                startActivity(intent);
            }
        });
 
        btnCall.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel://119"));
                startActivity(intent);
            }
        });
        btnGallary.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/internal/images/media"));
                startActivity(intent);
            }
        });
        btnExit.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                finish();
            }
        });
 
    }
 
    public void ooo1(View v){
 
    }
}
 
cs

 

 

 

 

실행 결과

 

    

 

- MITITCH 버튼은 Toast 실행을 위해 넣은 버튼

- Naver를 클릭하면 인터넷 브라우저가 켜지면서 naver로 연결됨

- Call을 클릭하면 장치 내부 전화 앱이 켜지면서 119가 찍혀있음

- Gallary 역시 갤러리 앱을 연결

- Exit는 앱 끝내기

 

 

 

 

 

 

■ 명시적 Intent

 

명시적 인텐트는 액티비티 전환시 정보 교환 컴포넌트를 명시해주는 것을 의미합니다. 아래 예제에서 보면 MainActivity에서 정보를 가지고 EndActivity로 이동합니다. 이때 인텐트를 Intent intent = new Intent(MainActivity.this, EndActivity.class); 이렇게 선언합니다. 위의 암시적 인텐트는 알아서 안드로이드가 적합한 컴포넌트를 찾아서 연결하지만 여기서는 내가 이동할 컴포넌트의 클래스 이름을 명시합니다. 이것이 명시적 인텐트입니다.

 

♤ 예

Package 구조입니다. MainActivity에서 Intent에 정보를 저장해서 EndActivity로 보내줄 것입니다.

 

 

 

[ AndroidManifest.xml ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mijinpark.a0531intentex">
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>
        <activity android:name=".EndActivity"></activity>
    </application>
 
</manifest>
cs

: 먼저 액티비티를 하나 더 생성했기 때문에 EndActivity를 manifest에 설정합니다.

 

[ 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
36
37
38
39
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
 
        <requestFocus>
        </requestFocus>
    </EditText>
 
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >
 
        <Button
            android:id="@+id/button_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="입력완료" >
        </Button>
 
        <Button
            android:id="@+id/button_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="취소" >
        </Button>
    </LinearLayout>
 
</LinearLayout>
cs

 

 

[ activity_end.xml ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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:orientation="vertical" >
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="25dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="받은 문자열" >
    </TextView>
 
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="확인" >
    </Button>
 
</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
package com.example.mijinpark.a0531intentex;
 
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 
    private static final int GET_STRING = 1;
    EditText editText;
    Button button_ok, button_cancel;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        editText = (EditText) findViewById(R.id.edit);
        
        Button button_ok = (Button) findViewById(R.id.button_ok);
        Button button_cancel = (Button) findViewById(R.id.button_cancel);
 
        button_ok.setOnClickListener(this);
        button_cancel.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.button_ok) {
            Intent intent = new Intent(MainActivity.this, EndActivity.class);
            intent.putExtra("edit", editText.getText().toString());
 
            startActivityForResult(intent, GET_STRING);
        } else {
            finish();
        }
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode==GET_STRING) {
            if (resultCode==RESULT_OK) {
                editText.setText("");
 
                String edit = data.getExtras().getString("edit").toString();
                Toast.makeText(this"이전에 받았던 문자는" + edit + "입니다.", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
cs

 

[ EndActivity.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
package com.example.mijinpark.a0531intentex;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
/**
 * Created by MiJin Park on 2016-06-02.
 */
public class EndActivity extends Activity{
    String edit;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_end);
 
        Intent intent = getIntent();
        edit = intent.getExtras().getString("edit").toString();
 
        TextView textView1 = (TextView) findViewById(R.id.textView1);
        textView1.setText("받아온 문자는 " + edit);
 
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                process();
                finish();
            }
        });
    }
 
    private void process() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("edit", edit);
        setResult(Activity.RESULT_OK, intent);
    }
}
 
 
cs

 

 

 

Tip & Tech

 

- 주로 명시적 인텐트를 더 많이 사용합니다. 구지 안드로이드가 적합한 컴포넌트를 찾아주는데 왜?라고 생각할 수 있지만 만약 내가 만든 앱 내부에서 이동할 때 암시적 인텐트를 사용한다면 앱 외부 컴포넌트로 연결될 수 있기 때문입니다.

 

- intent를 선언해서 정보를 처리할 때 액티비티를 호출하려면 startActivity()를 사용합니다. 여기서 startActivity()와 다른 메소드의 차이점은 다음과 같습니다.

startActivity()는 그냥 다른 액티비티를 호출하는 메소드

 

'Android > Android' 카테고리의 다른 글

Android 흑과백 게임  (0) 2016.06.13
Android(7)_Push  (0) 2016.06.01
Android 계산기  (0) 2016.05.31
Android(4)_고급Widget  (0) 2016.05.30
Android(3)_Layout  (0) 2016.05.27