Android Application

Kotlin 인텐트(intent) : 명시적 인텐트(Explicit Intent)- 다른 앱으로 데이터 전달하기

작성자 임베디드코리아 작성일23-10-23 22:52 조회1,466회 댓글0건

첨부파일

Kotlin 인텐트(intent) : 명시적 인텐트(Explicit Intent)
다른 프로젝트로 데이터 전달하기
>> 보기 와  받기 두 개의 프로젝트를 제작하여야 함
>> 받는 앱을 먼저 설치하고 보내는 엡을 실행하여양한다
    - 보내는 앱을 실행하여 받는 앱을 실행 시키다.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*******  보내기 앱 *******************************************


-------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="352dp"
        android:layout_marginEnd="20dp"
        android:hint="텍스트를 입력하세요"
        app:layout_constraintBottom_toTopOf="@+id/btnGoSubActivity"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnGoSubActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="236dp"
        android:text="Go SubActivity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    lateinit var btnGoSubActivity: Button
    lateinit var Package_NAME: String
    lateinit var Activity_NAME: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setTitle("명시적 인텐트 호출");

        btnGoSubActivity = findViewById(R.id.btnGoSubActivity)
        Package_NAME = "ek.example.explicitintentapprecivedata"
        //Activity_NAME = "ExplicitIntentAppReciveData.MainActivity"
        Activity_NAME  =Package_NAME + ".RecevSecondActivity"

        btnGoSubActivity.setOnClickListener {
            var compName : ComponentName =
                //ComponentName([패키지명],[액티비티 패키지명])
                //ComponentName("ek.example.explicitintentapprecivedata","ek.example.explicitintentapprecivedata.RecevSecondActivity")
                ComponentName("$Package_NAME","$Activity_NAME")

            var intent : Intent = Intent(Intent.ACTION_MAIN)
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            //intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
            //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            intent.addCategory(Intent.CATEGORY_LAUNCHER)
            intent.putExtra("KEY", "쫓기는 짐승이 되지 말고, 싸우는 영웅이 되자 !")//키, 전달하고자 하는 값.
            intent.setComponent(compName)
            startActivity(intent)
            startActivityForResult(intent,101)


        }

    }
}

------------------------------------------------------------------------------------
    themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.ExplicitIntentAppSendData" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">#FF9800</item>
        <item name="colorPrimaryDark">#00897B</item>
    </style>

    <style name="Theme.ExplicitIntentAppSendData" parent="Base.Theme.ExplicitIntentAppSendData" />
</resources>


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*******  받기 위한  앱  *****************************************

-------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="인텐트로 데이터를 받기위한 프로젝트!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

-------------------------------------------------------------------------------------
*****      activity_second.xml **
--- res/layout 에서  ---> New  --> XML --> Layout XML File
--- File : activity_second  ,  Root Tag : LinearLayout
-------------------------------------------------------------------------------------
<?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" >

    <TextView
        android:id="@+id/reciveData"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"/>

    <Button
        android:id="@+id/btnReturn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="돌아가기" />
</LinearLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var msg = this.intent.getStringExtra("KEY") //전송부의 키값의 value를 가져온다.
        Toast.makeText(applicationContext, "$msg", Toast.LENGTH_LONG).show()
        setResult(2001)
        //finish();
    }
}

------------------------------------------------------------------------------------
*****    RecevSecondActivity.kt **
-- New --> Kotlin Class/File
-- Name : RecevSecondActivity
------------------------------------------------------------------------------------
class RecevSecondActivity  : AppCompatActivity() {
    private lateinit var btnReturn: Button
    private lateinit var textView: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        title = "Second 액티비티"

        btnReturn = findViewById<Button>(R.id.btnReturn)
        textView = findViewById<TextView>(R.id.reciveData)

    }
    override fun onResume() {
        super.onResume()

        var ReciveMSG = this.intent.getStringExtra("KEY") //전송부의 키값의 value를 가져온다.
        Toast.makeText(applicationContext, "외부로부터 데이터를 받았습니다.", Toast.LENGTH_LONG).show()
        textView.text="$ReciveMSG"
        setResult(2001)
        //finish()
    }
}

------------------------------------------------------------------------------------
    AndroidManifest.xml
------------------------------------------------------------------------------------
      <activity
            android:name=".RecevSecondActivity"
            android:exported="true"
            android:launchMode="standard">
            <intent-filter >
                <action android:name="androidx.appcompat.app.AppCompatActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>

------------------------------------------------------------------------------------
    themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.ExplicitIntentAppReciveData" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">#2196F3</item>
        <item name="colorPrimaryDark">#00897B</item>
    </style>

    <style name="Theme.ExplicitIntentAppReciveData" parent="Base.Theme.ExplicitIntentAppReciveData" />
</resources>