lab2
This commit is contained in:
@@ -2,6 +2,9 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.READ_CONTACTS" />
|
||||||
|
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
|
|||||||
@@ -1,27 +1,62 @@
|
|||||||
package com.example.myapplication
|
package com.example.myapplication
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
|
import android.content.pm.PackageManager
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.widget.Button
|
import android.widget.Button
|
||||||
import android.widget.EditText
|
import android.widget.Toast
|
||||||
import android.widget.TextView
|
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
|
|
||||||
|
// Запрос разрешения на контакты
|
||||||
|
private val requestContactsPermission = registerForActivityResult(
|
||||||
|
ActivityResultContracts.RequestPermission()
|
||||||
|
) { isGranted: Boolean ->
|
||||||
|
if (isGranted) {
|
||||||
|
Toast.makeText(this, "Доступ к контактам разрешён", Toast.LENGTH_SHORT).show()
|
||||||
|
} else {
|
||||||
|
Toast.makeText(this, "Доступ к контактам запрещён", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Запрос разрешения на галерею
|
||||||
|
private val requestGalleryPermission = registerForActivityResult(
|
||||||
|
ActivityResultContracts.RequestPermission()
|
||||||
|
) { isGranted: Boolean ->
|
||||||
|
if (isGranted) {
|
||||||
|
Toast.makeText(this, "Доступ к галерее разрешён", Toast.LENGTH_SHORT).show()
|
||||||
|
} else {
|
||||||
|
Toast.makeText(this, "Доступ к галерее запрещён", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
val etInput = findViewById<EditText>(R.id.etInput)
|
val btnContacts = findViewById<Button>(R.id.btnContacts)
|
||||||
val btnApply = findViewById<Button>(R.id.btnApply)
|
val btnGallery = findViewById<Button>(R.id.btnGallery)
|
||||||
val tvResult = findViewById<TextView>(R.id.tvResult)
|
|
||||||
|
|
||||||
btnApply.setOnClickListener {
|
// При нажатии на кнопку "Контакты"
|
||||||
val text = etInput.text.toString().trim()
|
btnContacts.setOnClickListener {
|
||||||
if (text.isEmpty()) {
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
|
||||||
tvResult.text = "Введите текст!"
|
== PackageManager.PERMISSION_GRANTED) {
|
||||||
|
Toast.makeText(this, "Уже есть доступ к контактам", Toast.LENGTH_SHORT).show()
|
||||||
} else {
|
} else {
|
||||||
tvResult.text = "РЕЗУЛЬТАТ: ${text.uppercase()}"
|
requestContactsPermission.launch(Manifest.permission.READ_CONTACTS)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// При нажатии на кнопку "Галерея"
|
||||||
|
btnGallery.setOnClickListener {
|
||||||
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_IMAGES)
|
||||||
|
== PackageManager.PERMISSION_GRANTED) {
|
||||||
|
Toast.makeText(this, "Уже есть доступ к галерее", Toast.LENGTH_SHORT).show()
|
||||||
|
} else {
|
||||||
|
requestGalleryPermission.launch(Manifest.permission.READ_MEDIA_IMAGES)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,24 +4,16 @@
|
|||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:padding="16dp">
|
android:padding="16dp">
|
||||||
|
|
||||||
<EditText
|
<Button
|
||||||
android:id="@+id/etInput"
|
android:id="@+id/btnContacts"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:hint="Введите текст" />
|
android:text="Доступ к контактам" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/btnApply"
|
android:id="@+id/btnGallery"
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginTop="12dp"
|
|
||||||
android:text="Преобразовать" />
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/tvResult"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="24dp"
|
android:layout_marginTop="16dp"
|
||||||
android:text="Здесь будет результат"
|
android:text="Доступ к галерее" />
|
||||||
android:textSize="18sp" />
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
Reference in New Issue
Block a user