Я разработал приложение для Android для Smart TV. Цель программы - запустить в фонах и настройках блоков с помощью PIN -кода. Если пользовательские вводы правильный PIN -код, например 1234 Настройки должны открыть. Я установил на TCL Smart TV, но он не блокирует настройки. Я также включил приложение из настройки доступности. Я также тестировал на эмуляторе Android TV (1080p). Похоже, «переопределить веселую доходность (событие: доступновидение?)» Функция никогда не вызывается. < /P>
--AndroidManifest.xml
-------------------------------------------------------------------------------------------
--activity_main.xml
---------------------------------------------------------------
--activity_lock_screen.xml
---------------------------------------------------------------------------
-- MainActivity.kt
package com.sup.securesettings
import android.content.Intent
import android.os.Bundle
import android.os.Build
import android.net.Uri
import android.provider.Settings
import android.util.Log
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
class MainActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnSettings : Button = findViewById(R.id.btn_accessibility)
btnSettings.setOnClickListener {
openSettings()
}
val btnExit : Button = findViewById(R.id.btn_exit)
btnExit.setOnClickListener {
finish()
}
val btn_enable_security : Button = findViewById(R.id.btn_enable_security)
btn_enable_security.setOnClickListener {
enableSecurity()
}
//checkOverlayPermission()
}
private fun enableSecurity() {
try {
Log.i("AppLock", "Enabling Service")
val intent = Intent(applicationContext, AppLockService::class.java)
startService(intent)
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show()
} catch (e: Exception) {
Log.d("AppLock", "Error starting service: $e")
findViewById(R.id.error).text = e.message ?: "Error starting service"
}
}
private fun openSettings() {
val txtError : TextView = findViewById(R.id.error)
try {
startActivity(Intent(Settings.ACTION_SETTINGS))
//startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))
Toast.makeText(this, "Go to Settings → Accessibility → Installed Services", Toast.LENGTH_LONG).show()
//startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))
} catch (e: Exception) {
txtError.text = e.message ?: "Unknown error"
}
}
private fun lockScreen() {
try {
val intent = Intent(this, LockScreenActivity::class.java)
startActivity(intent)
} catch (e: Exception) {
val err : String = e.message ?: "Unknown error"
Toast.makeText(this, err, Toast.LENGTH_LONG).show()
}
}
private fun checkOverlayPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
val intent = Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:$packageName")
)
startActivity(intent)
}
}
}
}
---------------------------------------------------------------------------
--AppLockService.kt
package com.sup.securesettings
import android.accessibilityservice.AccessibilityService
import android.content.Intent
import android.util.Log
import android.view.accessibility.AccessibilityEvent
import android.widget.Toast
class AppLockService : AccessibilityService() {
override fun onAccessibilityEvent(event: AccessibilityEvent?) {
Log.i("AppLock","onAccessibilityEvent")
if (event == null || event.packageName == null) return
val packageName = event.packageName?.toString()
val className = event.className?.toString()
val eventType = event.eventType?.toString()
Log.i("AppLock", "Event Type: ${eventType}, Package Name: ${packageName}, Class Name: ${className}")
if(event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED ||
event.eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED ||
event.eventType == AccessibilityEvent.TYPE_WINDOWS_CHANGED) {
if ((packageName == "com.android.tv.settings") ||
(packageName == "com.android.settings") ||
(packageName == "com.samsung.tv.settings") ||
(packageName == "com.google.android.tv.settings")||
(packageName == "com.samsung.tv.settings") ||
(packageName == "com.tcl.settings") ||
(packageName == "com.tcl.tv.settings")
) {
Log.i("AppLock", "Lock Screen Initiating: $packageName")
performGlobalAction(GLOBAL_ACTION_BACK)
lockScreen()
}
}
}
override fun onInterrupt() {
Log.w("AppLock", "Service interrupted")
}
private fun lockScreen() {
try {
val intent = Intent(this, LockScreenActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)
} catch (e: Exception) {
val err : String = e.message ?: "Unknown error"
Toast.makeText(this, err, Toast.LENGTH_LONG).show()
}
}
}
---------------------------------------------------------------------------
--BootReceiver.kt
package com.sup.securesettings
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
class BootReceiver : BroadcastReceiver() {
override
fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
context.startService(Intent(context,AppLockService::class.java))
}
}
}
---------------------------------------------------------------------------
--LockScreenActivity.kt
package com.sup.securesettings
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.view.View
import androidx.fragment.app.FragmentActivity
class LockScreenActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_lock_screen)
val etPin = findViewById(R.id.et_pin)
val btnUnlock = findViewById(R.id.btn_unlock)
val tvError = findViewById(R.id.tv_error)
btnUnlock.setOnClickListener {
if (etPin.text.toString() == "1234") { // Replace with secure PIN check
finish()
} else {
tvError.text = "Wrong PIN"
tvError.visibility = View.VISIBLE
}
}
}
override fun onBackPressed() {} // Disable back button
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... -smart-tvs