GIT 提交消息:
重构浮动窗口管理和布局属性 - 调整浮动窗口的布局和样式,以改善用户体验。 - 重构 MainActivity 中的浮动窗口处理逻辑,以增强功能性和清晰度。 - 通过移除不必要的方法和优化状态管理,简化 MainViewModel。
This commit is contained in:
parent
2bec9e3628
commit
7fcaa954eb
|
@ -120,7 +120,7 @@ class MainActivity : AppCompatActivity() {
|
|||
configureWindowParams()
|
||||
windowBinding.root.setOnClickListener {
|
||||
if (isActive()) {
|
||||
viewModel.hideFloatingWindow()
|
||||
hideFloatingWindow()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -133,7 +133,7 @@ class MainActivity : AppCompatActivity() {
|
|||
}
|
||||
|
||||
/**
|
||||
* 隐藏悬浮窗
|
||||
* 隐藏悬浮窗并回到主窗口
|
||||
*/
|
||||
private fun hideFloatingWindow() {
|
||||
try {
|
||||
|
@ -161,13 +161,19 @@ class MainActivity : AppCompatActivity() {
|
|||
private fun bringMainActivityToFront() {
|
||||
val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
||||
val tasks = activityManager.getRunningTasks(10) // 获取最近的任务列表
|
||||
var found = false
|
||||
for (task in tasks) {
|
||||
if (task.topActivity?.packageName == packageName) {
|
||||
// 找到 MainActivity 的任务栈,将其带到前台
|
||||
activityManager.moveTaskToFront(task.id, ActivityManager.MOVE_TASK_WITH_HOME)
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if (found)
|
||||
Log.e("MainActivity", "MainActivity found")
|
||||
else
|
||||
Log.e("MainActivity", "MainActivity not found")
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -196,7 +202,9 @@ class MainActivity : AppCompatActivity() {
|
|||
flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
|
||||
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
|
||||
format = android.graphics.PixelFormat.TRANSLUCENT
|
||||
gravity = android.view.Gravity.TOP or android.view.Gravity.START
|
||||
gravity = android.view.Gravity.BOTTOM or android.view.Gravity.END // 修改为右下角
|
||||
x = 0 // 初始 x 坐标
|
||||
y = 0 // 初始 y 坐标
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -90,11 +90,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
|||
updateState { it.copy(floatingWindowVisible = hasPermission) }
|
||||
}
|
||||
|
||||
fun hideFloatingWindow() {
|
||||
updateState { it.copy(floatingWindowVisible = false) }
|
||||
removeFloatingView()
|
||||
}
|
||||
|
||||
fun cleanUpResources() {
|
||||
removeFloatingView()
|
||||
}
|
||||
|
|
|
@ -2,13 +2,14 @@
|
|||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="#22000000">
|
||||
android:background="#22000000"
|
||||
android:layout_gravity="bottom|end">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="悬浮窗"
|
||||
android:padding="16dp"
|
||||
android:textSize="18sp"/>
|
||||
android:text="悬浮窗"
|
||||
android:textSize="18sp" />
|
||||
|
||||
</FrameLayout>
|
Loading…
Reference in New Issue
Block a user