Skip to content

made intent #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
</intent-filter>
</activity>
</application>

</manifest>
42 changes: 40 additions & 2 deletions app/src/main/java/com/itis/androidlabproject/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
package com.itis.androidlabproject

import android.app.Activity
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.google.android.material.snackbar.Snackbar
import com.itis.androidlabproject.databinding.ActivityMainBinding
import java.util.zip.Inflater

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val REQUEST_CODE_1 = 123;

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater).also {
setContentView(it.root)
}

with(binding) {
btnText.setOnClickListener {
val intent = Intent(Intent.ACTION_VIEW)
intent.addCategory(Intent.CATEGORY_OPENABLE)

intent.putExtra(Intent.EXTRA_TEXT, "test text")
if (intent.resolveActivity(packageManager) != null) {
startActivityForResult(intent, 1)
// startActivity(intent) /* не работает*/
}
}
}
}

override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Успешный возврат", Toast.LENGTH_LONG).show()
} else {
super.onActivityResult(requestCode, resultCode, data)
}

}
}
}
44 changes: 44 additions & 0 deletions app/src/main/java/com/itis/androidlabproject/SecondActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.itis.androidlabproject

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.itis.androidlabproject.databinding.ActivitySecondBinding
import com.google.android.material.snackbar.Snackbar


class SecondActivity : AppCompatActivity() {
private lateinit var binding: ActivitySecondBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySecondBinding.inflate(layoutInflater).also {
setContentView(it.root)
}

with(binding) {
btnMessage.setOnClickListener {
val message = intent?.extras?.getString("MESSAGE") ?: "сообщение не дошло или пусто"

Snackbar.make(
root,
"Получено сообщение: $message",
Snackbar.LENGTH_LONG
).show()

/*Toast.makeText(
this@SecondActivity,
"Получено сообщение: $message",
Toast.LENGTH_LONG
).show()*/
}

btnReturn.setOnClickListener {
setResult(Activity.RESULT_OK)
finish()
}
}
}
}
6 changes: 4 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
<Button
android:id="@+id/btn_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:text="Второе окно"
android:backgroundTint="@color/teal_200"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/res/layout/activity_second.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btn_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/teal_700"
android:text="Получить сообщение"
app:layout_constraintBottom_toTopOf="@id/btn_return"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />

<Button
android:id="@+id/btn_return"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/teal_700"
android:text="Вернуться"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_message" />


</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.2"
classpath 'com.android.tools.build:gradle:7.0.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"

// NOTE: Do not place your application dependencies here; they belong
Expand Down