728x90
반응형
💡인텐트 필터와 암시적 인텐트 수신
◾ 액티비티 매니저가 하단에서 올라오는 액션을 실행시켜준다.
◾ 안드로이드 시스템은 이런 인텐트를 처리할 수 있는 다음과 같은 인텐트 필터를 포함한 액티비티를 찾아내 그를 시키므로 MainActivity2 액티비티가 다음의 인텐트 필터를 포함하고 있어야 한다.
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain"/>
</intent-filter>
◾ 만일 해당 인텐트를 처리할 액티비티가 여러 개인 경우라면 어떤 액티비티를 실행시킬 지 사용자가 선택할 수 있는 창이 열린다.
📃암시적 인텐트를 사용해 에디트 텍스트에 입력된 내용을 다른 액티비티로 전송하시오.
📝AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.project.test01">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Day38">
<activity
android:name=".MainActivity2"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plane"/>
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
📝activity_main.xml
<?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="20dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp" />
<Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="onClick"
android:text="OK"
android:textSize="25sp" />
</LinearLayout>
📝activity_main2.xml
<?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"
tools:context=".MainActivity2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv"
android:gravity="center"/>
</LinearLayout>
📝MainActivity.java
package com.project.test01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText et; // 에디트텍스트의 인스턴스를 저장할 변수
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText)findViewById(R.id.et_send);
}
public void onClick(View v) {
String str = et.getText().toString(); // 에디트텍스트의 입력 내용을 문자열로 저장
Intent intent = new Intent(); // 인텐트 생성
intent.setAction(intent.ACTION_SEND); // 인텐트 액션으로 ACTION_SEND 지정
intent.putExtra(intent.EXTRA_TEXT, str); // 저장된 문자열을 EXTRA_TEXT 키로 저장
intent.setType("text/plane"); // 전달하는 데이터의 타입을 설정
startActivity(Intent.createChooser(intent, "어떤 액티비티를 실행할까요?")); // 액티비티 실행 요청
}
}
📝MainActivity2.java
package com.project.test01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
String str = intent.getStringExtra(Intent.EXTRA_TEXT);
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(str);
}
}
728x90
반응형
'App > Android Studio' 카테고리의 다른 글
Android Studio - 태스크, 인텐트 플래그 (0) | 2022.08.10 |
---|---|
Android Studio - 사용 권한 (0) | 2022.08.10 |
프로필 수정 📌미완성 (0) | 2022.08.09 |
Android Studio - 복습 (0) | 2022.08.09 |
Android Studio - 액티비티 간의 상호작용 (0) | 2022.08.09 |
댓글