From 096072bce8e6f61318b93671856f9ee738863cce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D1=88=D0=B5=20=D0=98=D0=BC=D1=8F?= Date: Wed, 24 Sep 2025 16:53:21 +0400 Subject: [PATCH] lab10 --- app/src/main/AndroidManifest.xml | 2 + .../com/example/myapplication/MainActivity.kt | 137 +++++++++++------- app/src/main/res/layout/activity_main.xml | 12 +- 3 files changed, 87 insertions(+), 64 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e2d8658..64a4d84 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + + if (isGranted) { + showNotification() + } + } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - tvAccelerometer = findViewById(R.id.tvAccelerometer) - tvLight = findViewById(R.id.tvLight) + createNotificationChannel() - // Получаем доступ к сенсорам - sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager - accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) - lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT) - - // Если сенсор отсутствует - if (accelerometer == null) { - tvAccelerometer.text = "Акселерометр не поддерживается" - } - if (lightSensor == null) { - tvLight.text = "Датчик освещённости не поддерживается" - } - } - - override fun onResume() { - super.onResume() - accelerometer?.also { - sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_NORMAL) - } - lightSensor?.also { - sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_NORMAL) - } - } - - override fun onPause() { - super.onPause() - sensorManager.unregisterListener(this) - } - - override fun onSensorChanged(event: SensorEvent?) { - when (event?.sensor?.type) { - Sensor.TYPE_ACCELEROMETER -> { - val x = event.values[0] - val y = event.values[1] - val z = event.values[2] - tvAccelerometer.text = "Акселерометр:\nX = %.2f\nY = %.2f\nZ = %.2f".format(x, y, z) - } - Sensor.TYPE_LIGHT -> { - val lux = event.values[0] - tvLight.text = "Освещённость: $lux люкс" + val btnNotify: Button = findViewById(R.id.btnNotify) + btnNotify.setOnClickListener { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + // Проверяем разрешение + if (ActivityCompat.checkSelfPermission( + this, + Manifest.permission.POST_NOTIFICATIONS + ) == PackageManager.PERMISSION_GRANTED + ) { + showNotification() + } else { + // Запрашиваем разрешение + requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) + } + } else { + showNotification() } } } - override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) { + private fun createNotificationChannel() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val name = "Lab10 Notifications" + val descriptionText = "Канал для лабораторной работы 10" + val importance = NotificationManager.IMPORTANCE_HIGH + val channel = NotificationChannel(channelId, name, importance).apply { + description = descriptionText + } + val notificationManager: NotificationManager = + getSystemService(NotificationManager::class.java) + notificationManager.createNotificationChannel(channel) + } + } + + private fun showNotification() { + if (ActivityCompat.checkSelfPermission( + this, + android.Manifest.permission.POST_NOTIFICATIONS + ) != PackageManager.PERMISSION_GRANTED + ) { + return + } + + // Интент для открытия MainActivity при клике + val intent = Intent(this, MainActivity::class.java).apply { + flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + } + val pendingIntent = PendingIntent.getActivity( + this, 0, intent, + PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT + ) + + val builder = NotificationCompat.Builder(this, channelId) + .setSmallIcon(android.R.drawable.ic_dialog_info) + .setContentTitle("Лабораторная работа 10") + .setContentText("Это пример уведомления") + .setPriority(NotificationCompat.PRIORITY_HIGH) + .setContentIntent(pendingIntent) + .setAutoCancel(true) + + with(NotificationManagerCompat.from(this)) { + notify(1001, builder.build()) + } } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4db57f4..067760e 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -4,15 +4,9 @@ android:orientation="vertical" android:padding="16dp"> - - - + android:text="Показать уведомление" /> \ No newline at end of file