Работа со вторым дисплеем (Android)
Мне требуется в проекте для POS-системы, реализовать экран покупателя. В интернете я нашел статью, в которой предлагают работать с Presentation (вот источник - https://medium.com/@umarmakhdoomi007/setting-up-secondary-screen-display-in-android-be964fc51eed). Вот мой код (для проверки работоспособности): layout_main_activity:
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
layout_second_screen
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFF"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="vertical">
<TextView
android:id="@+id/presentation_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Hello, Secondary Display!"
android:textSize="24sp" />
</LinearLayout>
MainActivity:
class MainActivity : AppCompatActivity() {
private var presentation: SecondaryScreen? = null
@RequiresApi(Build.VERSION_CODES.S)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
val secondaryDisplay = getCustomerDisplay(this)
if (secondaryDisplay != null) {
presentation = SecondaryScreen(this, secondaryDisplay)
presentation?.show()
}
}
private fun getCustomerDisplay(context: Context): Display? {
val displayManager = context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
val displays = displayManager.displays
if (displays.size <= 1) {
return null
}
return displays[1]
}
override fun onDestroy() {
super.onDestroy()
presentation?.dismiss()
}
}
SecondaryScreen:
class SecondaryScreen(context: Context, display: Display) : Presentation(context, display) {
private var _binding: SecondaryScreenBinding? = null
private val binding get() = _binding!!
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initMethod()
}
fun initMethod() {
_binding = SecondaryScreenBinding.inflate(LayoutInflater.from(context))
setContentView(binding.root)
binding.presentationText.text = "Hello, this is Secondary Display"
}
}
При запуске приложения, на главном экране отображается информация c MainActivity, а на втором экране - информация с SecondScree. Проблема заключается в том, что при попытке взаимодействия с основным экраном, события нажатий не регистрируются. Они регистрируются на втором экране, т. е. на SecondScreen. Пробовал сбрасывать фокус window?.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) window?.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)
(в данном случае события переставали регистрироваться на SecondScreen), устанавливать фокус принудительно для вьюшек MainActivity - ничего не помогло. Может кто-нибудь знает, в чем может быть проблема? Требуется, чтобы главный экран, на котором отображается MainActivity оставался активным, т.е. регистрировались события нажатий. А второй экран - он чисто демонстрационный (он даже не сенсорный).