일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- max apache
- 객체지향
- 제이쿼리연결
- 배열 분해 할당
- jquery 사용하기
- 객체
- 자바스크립트 객체
- 한글입력 안됨
- 자바스크립트 배열 할당
- 제이펍 출판
- 황반석 옮김
- 한글잘림
- python GUI 사용하기
- max MySQL
- 한글입력 씹힘
- 한글입력 잘림
- 핵심만 골라 배우는 SwiftUI 기반의 iOS 프로그래밍
- 맥 mysql
- 기획자랑 사이좋게 지내고 싶다
- 자바스크립트 객체 만들기
- 한글입력 오류
- SwiftUI 기반의 iOS 프로그래밍
- 자바스크립트
- 비주얼스튜디오 코드
- 맥 아파치
- 블록 스코프
- 자바스크립트 class
- 닐 스미스 지음
- 생활코딩
- python tkinter 인터페이스
- Today
- Total
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- max apache
- 객체지향
- 제이쿼리연결
- 배열 분해 할당
- jquery 사용하기
- 객체
- 자바스크립트 객체
- 한글입력 안됨
- 자바스크립트 배열 할당
- 제이펍 출판
- 황반석 옮김
- 한글잘림
- python GUI 사용하기
- max MySQL
- 한글입력 씹힘
- 한글입력 잘림
- 핵심만 골라 배우는 SwiftUI 기반의 iOS 프로그래밍
- 맥 mysql
- 기획자랑 사이좋게 지내고 싶다
- 자바스크립트 객체 만들기
- 한글입력 오류
- SwiftUI 기반의 iOS 프로그래밍
- 자바스크립트
- 비주얼스튜디오 코드
- 맥 아파치
- 블록 스코프
- 자바스크립트 class
- 닐 스미스 지음
- 생활코딩
- python tkinter 인터페이스
- Today
- Total
java,javascript,android,php,sql,공부용,메모용
안드로이드앱 인텐트 intent() / 인텐트 값 전달하기, 인텐트 값 받아오기 / 앱에서 새창 띄우기 / Android Studio / 안드로이드 앱만들기 공부 21 본문
안드로이드앱 인텐트 intent() / 인텐트 값 전달하기, 인텐트 값 받아오기 / 앱에서 새창 띄우기 / Android Studio / 안드로이드 앱만들기 공부 21
yy_dd2 2021. 3. 9. 19:35Intent() 인텐트
- 인텐트는 androuid.content 패키지 안에 정의되어있다
- 앱 구성 요소 간에 작업 수행을 위한 정보를 전달하는 역할
- 인텐트 전달 대표 메서드
startActivity() 또는 startActivityForResult()
startService() 또는 bindService()
brodcastIntent()
- startActivity() : 액티비티 화면을 띄울 때 사용
- startService() : 서비스 시작할 때
- brodcastIntent() : 브로드캐스트 방식으로 전송할 때
이 메서드들을 호출하면 파라미터로 intent가 전달된다.
- 인텐트의 기본요소 : 액션(Action)과 데이터(Data)
Action의 대표적인 속성 | |
ACTION_DIAL tel:01011112222 | 주어진 번호로 전화걸기 화면 보여줌 |
ACTION_VIEW tel:01011112222 | 주어진 번호로 전화걸기 화면 보여줌 URI 값의 유형에 따라 VIEW액션이 다른 기능을 수행 |
ACTION_EDIT content://contacts/people/2 | 전화번호부 데이터베이스에 있는 정보 중 ID 값이 2인 정보를 편집하기 위한 화면 보여줌 |
ACTION_VIEW content://contacts/peple | 전화번호 데이터베이스 내용 보여줌 |
- 인텐트에 포함된 데이터가 어떤 포맷인지 자동으로 찾아서 액티비티를 띄워주기도 함
- http 같은 포맷을 쓴다면 URL이라고 인식한다
- 인텐트의 생성자
인텐트는 객체는 액션과 데이터를 인수로 만든다
인텐트는 클래스 객체를 인수로 만든다
Intent()
Intent(Intent o)
Intent(String action[,Uri uri])
Intent(Context packageContenxt, Class<?> cls)
Intent(String action,Uri uri, Context packageContext, Class<?> cls)
- 명시적 인텐트 : 인텐트에 클래스 객체나 컴포넌트 이름을 지정해 호출할 대상을 아는 경우
- 암시적 인텐트 : 액션과 데이터를 지정했지만 호출 대상이 달라질 수 있는 경우 (범주,타입,컴포넌트,부가데이트)
1) 범주(Category)
- 액션이 실행되는데 필요한 추가적인 정보 제공
(CATEGORY_LAUNCHER : 최상위 앱으로 설치된 앱들의 목록을 보여주는 애플리케이션 런처화면에 앱을 보여주어야 한다는 것)
2) 타입(Type)
- 인텐트에 들어가는 데이터의 MIME 타입을 명시적으로 지정
(MIME 타입은 데이터만으로 구별이 가능하나 명시적으로 지정할 필요가 있을때도 있다)
3) 컴포넌트(Component/구성요소)
인텐트에 사용될 컴포넌트 클래스 이름을 명시적으로 지정
이 속성이 지정되면 지정ㄷ한 컴포넌트가 실행되도록 한다
(컴포넌트 지정방식과 같은 방식 : 새 액티비티 정의 -> 액티비티의 클래스 객체 인텐트에 전달 -> 실행)
4) 부가데이터(Extra Data)
- 추가적인 정보를 담을 수 있게 번들(Bundle) 객체를 담고있다
(이베일을 보내는 액션이 있다면 이메일에 제목,내용 등을 부가데이터로 넣어 전달해야 이메일 앱이 데이터를 받고 처리할 수 있다)
인텐트의 내용이 길지만 이걸 기억하고
위 내용은 코드할때 인텐트가 필요할때마다 찾아서 보면 될듯...
인텐트
앱구성요소간의 작업 수행을 위한 정보를 전달한다 (데이터 전달)
1. 인텐트의 액션과 데이터를 이용해서 새 액티비티를 띄운다
2. 인텐트의 컴포넌트 이름을 이용해서 새 액티비티를 띄운다
3. 인텐트는 정보를 전달한다 category, type, component, extra data
intent로 액션과 데이터를 넣어 앱의 액티비티를 띄우기 (전화번호 연결하기)
1. SampleCallIntent 프로젝트 생성
2. activity_main.xml 파일 텍스트뷰 삭제 최상위 레이아웃 LinearLayout 속성 orientation="verical"
3. 전화번호 입력상자와 버튼하나 추가
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="tel:01011112222"
android:textSize="24sp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="전화걸기" />
</LinearLayout>
4. Mainactivity.java 파일에 버튼 누르면
입력상자에 입력한 전화번호로 전화를 걸도록 코드 입력
package com.togapp.samplecallintent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // view 객체 참조
editText = findViewById(R.id.editText);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String data = editText.getText().toString(); // 입력상자에 입력된 전화번호 확인
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data)); // 전화걸기 화면 보여줄 인텐트 객체 생성
startActivity(intent); // 액티비티 띄우기
}
});
}
}
![]() |
![]() |
Component 이름을 이용해 새 액티비티 띄우기
1. activity_main.xml 파일에 버튼 추가 "메뉴 화면 띄우기"
2. app 우클릭 new - activity - Empty Activity /activity name : MenuActivity
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="메뉴화면 띄우기" />
3. MeinActivity.java 파일에 버튼 누르면 새 액티비티 띄우기 만들기
Button button2 = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
// 컴포넌트 이름을 지정할 수 있는 객체 생성
ComponentName componentName =
new ComponentName("com.togapp.samplecallintent",
"com.togapp.samplecallintent.MenuActivity");
intent.setComponent(componentName);
startActivityForResult(intent, 201); // 액티비티 띄우기
}
});
메뉴화면을 띄우는 것은 했다 그리고 값을 가져오는것도 tog-code.tistory.com/42 여기서 했었는데
정확히는 intent안에 임의 값을 지정해서 저장 한뒤에 가져오는 것을 했다.
editText 안에 들어있는 값을 도로 가져오는 것도 해봤다
1. 새 액티비티를 띄워줄 버튼3을 추가했다
2. 새 액티비티에서 받아온 email 값을 나타내줄 TextView를 추가했다
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="tel:01011112222"
android:textSize="24sp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="전화걸기" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="메뉴화면 띄우기" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="대화상자 새 액티비티 띄우기 복습" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="이메일이 작성될곳"
android:text=""
android:textSize="24sp" />
</LinearLayout>
새 activity 화면을 만들었다
3. /app 우클릭 - new -activity - Empty Activity - MessageActivity
4. 코드작성
MainActivity.java 화면띄우기
// button2 아래 작성됨
Button button3 = findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Intent intent = new Intent(getApplicationContext(), MessageActivity.class);
//startActivityForResult(intent, REQUEST_CODE_MESSAGE);
Intent intent = new Intent();
ComponentName componentName = new ComponentName("com.togapp.samplecallintent","com.togapp.samplecallintent.MessageActivity");
intent.setComponent(componentName);
startActivityForResult(intent, 301);
}
});
activity_message.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MessageActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="* 이메일을 입력해주세요 *"
android:textColor="#3F51B5"
android:textSize="24sp" />
<EditText
android:id="@+id/editTextTextEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text=""
android:hint="email@email.com"
android:textColorHint="#FF4545" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="완료" />
</LinearLayout>
MessageActivity.java
package com.togapp.samplecallintent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
EditText emailText = findViewById(R.id.editTextTextEmailAddress);
Log.v("email",emailText.getText().toString());
intent.putExtra("email", emailText.getText().toString());
intent.putExtra("aint", "1234");
setResult(RESULT_OK, intent);
finish();
}
});
}
}
5. MainActivity.java 에서 값을 받아오는 코드를 작성했다 작성했다
package com.togapp.samplecallintent;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//public static final int REQUEST_CODE_MESSAGE = 301;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // view 객체 참조
//////////////내용생략///////////
} // onCreate end
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 301){
Toast.makeText(getApplicationContext(), "onActivityResult 메서드 호출됨. 요청코드 : " +
requestCode + "결과 코드 : " + requestCode, Toast.LENGTH_SHORT).show();
if (resultCode == RESULT_OK){
TextView emailText = findViewById(R.id.textView2);
String em = data.getStringExtra("email");
if (em != null)
emailText.setText(em);
Toast.makeText(getApplicationContext(),"응답으로 em :" + em, Toast.LENGTH_LONG).show();
}
};
} // onActivityResult end
실행화면
![]() |
![]() |
![]() |
![]() |
이걸 해봤으니 A액티비티에서 작성한 내용을 B액티비티로 전달하는 것도 가능할것
또는 C액티비티에서 A액티비티를 띄우고 작성한 내용을 C액티비티로 또 전달하는 것도 가능
MainActivity.java 코드 전문
package com.togapp.samplecallintent;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//public static final int REQUEST_CODE_MESSAGE = 301;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // view 객체 참조
editText = findViewById(R.id.editText);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String data = editText.getText().toString(); // 입력상자에 입력된 전화번호 확인
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data)); // 전화걸기 화면 보여줄 인텐트 객체 생성
startActivity(intent); // 액티비티 띄우기
}
});
Button button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
// 컴포넌트 이름을 지정할 수 있는 객체 생성
ComponentName componentName = new ComponentName("com.togapp.samplecallintent", "com.togapp.samplecallintent.MenuActivity");
intent.setComponent(componentName);
startActivityForResult(intent, 201); // 액티비티 띄우기
}
});
Button button3 = findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Intent intent = new Intent(getApplicationContext(), MessageActivity.class);
//startActivityForResult(intent, REQUEST_CODE_MESSAGE);
Intent intent = new Intent();
ComponentName componentName = new ComponentName("com.togapp.samplecallintent","com.togapp.samplecallintent.MessageActivity");
intent.setComponent(componentName);
startActivityForResult(intent, 301);
}
});
} // onCreate end
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 301){
Toast.makeText(getApplicationContext(), "onActivityResult 메서드 호출됨. 요청코드 : " +
requestCode + "결과 코드 : " + requestCode, Toast.LENGTH_SHORT).show();
if (resultCode == RESULT_OK){
TextView emailText = findViewById(R.id.textView2);
String em = data.getStringExtra("email");
if (em != null)
emailText.setText(em);
Toast.makeText(getApplicationContext(),"응답으로 em :" + em, Toast.LENGTH_LONG).show();
}
};
} // onActivityResult end
}