拿掉其他懸浮窗範例
This commit is contained in:
parent
bbe99a1ca5
commit
7ac9063248
@ -26,7 +26,6 @@ import com.ray650128.floatwindowdemo.utils.ViewModelMain
|
|||||||
*/
|
*/
|
||||||
class MainActivity : AppCompatActivity(), View.OnClickListener {
|
class MainActivity : AppCompatActivity(), View.OnClickListener {
|
||||||
|
|
||||||
private var floatRootView: View? = null//悬浮窗View
|
|
||||||
private var isReceptionShow = false
|
private var isReceptionShow = false
|
||||||
|
|
||||||
private lateinit var binding: ActivityMainBinding
|
private lateinit var binding: ActivityMainBinding
|
||||||
@ -39,85 +38,20 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
|
|||||||
startService(Intent(this, SuspendwindowService::class.java))
|
startService(Intent(this, SuspendwindowService::class.java))
|
||||||
|
|
||||||
binding.apply {
|
binding.apply {
|
||||||
bt01.setOnClickListener(this@MainActivity)
|
|
||||||
bt02.setOnClickListener(this@MainActivity)
|
|
||||||
bt03.setOnClickListener(this@MainActivity)
|
bt03.setOnClickListener(this@MainActivity)
|
||||||
bt04.setOnClickListener(this@MainActivity)
|
|
||||||
bt05.setOnClickListener(this@MainActivity)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onClick(v: View?) {
|
override fun onClick(v: View?) {
|
||||||
when (v?.id) {
|
when (v?.id) {
|
||||||
R.id.bt_01 -> {
|
|
||||||
showCurrentWindow()
|
|
||||||
}
|
|
||||||
R.id.bt_02 -> {
|
|
||||||
Utils.checkSuspendedWindowPermission(this) {
|
|
||||||
isReceptionShow = false
|
|
||||||
ViewModelMain.isShowSuspendWindow.postValue(true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
R.id.bt_03 -> {
|
R.id.bt_03 -> {
|
||||||
Utils.checkAccessibilityPermission(this) {
|
Utils.checkAccessibilityPermission(this) {
|
||||||
ViewModelMain.isShowWindow.postValue(true)
|
ViewModelMain.isShowWindow.postValue(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
R.id.bt_04 -> {
|
|
||||||
Utils.checkSuspendedWindowPermission(this) {
|
|
||||||
isReceptionShow = true
|
|
||||||
ViewModelMain.isShowSuspendWindow.postValue(true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
R.id.bt_05 -> {
|
|
||||||
closeAllSuspendWindow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 应用界面内显示悬浮球
|
|
||||||
*/
|
|
||||||
@SuppressLint("ClickableViewAccessibility")
|
|
||||||
private fun showCurrentWindow() {
|
|
||||||
var layoutParam = WindowManager.LayoutParams().apply {
|
|
||||||
//设置大小 自适应
|
|
||||||
width = WRAP_CONTENT
|
|
||||||
height = WRAP_CONTENT
|
|
||||||
flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|
|
||||||
x = 10
|
|
||||||
y = 10
|
|
||||||
}
|
|
||||||
// 新建悬浮窗控件
|
|
||||||
floatRootView = LayoutInflater.from(this).inflate(R.layout.activity_float_item, null)
|
|
||||||
floatRootView?.findViewById<ImageView>(R.id.iv_close)?.setOnClickListener {
|
|
||||||
windowManager?.removeView(floatRootView)
|
|
||||||
}
|
|
||||||
//设置拖动事件
|
|
||||||
floatRootView?.setOnTouchListener(ItemViewTouchListener(layoutParam, windowManager))
|
|
||||||
floatRootView?.focusable = View.FOCUSABLE
|
|
||||||
// 将悬浮窗控件添加到WindowManager
|
|
||||||
windowManager.addView(floatRootView, layoutParam)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 关闭所有悬浮窗
|
|
||||||
*/
|
|
||||||
fun closeAllSuspendWindow() {
|
|
||||||
if (!Utils.isNull(floatRootView)) {
|
|
||||||
if (!Utils.isNull(floatRootView?.windowToken)) {
|
|
||||||
if (!Utils.isNull(windowManager)) {
|
|
||||||
windowManager?.removeView(floatRootView)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ViewModelMain.isShowSuspendWindow.postValue(false)
|
|
||||||
ViewModelMain.isShowWindow.postValue(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
if (requestCode == REQUEST_FLOAT_CODE) {
|
if (requestCode == REQUEST_FLOAT_CODE) {
|
||||||
if (Settings.canDrawOverlays(this)) {
|
if (Settings.canDrawOverlays(this)) {
|
||||||
@ -136,8 +70,8 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
super.onPause()
|
|
||||||
binding.textView.text = "onPause"
|
binding.textView.text = "onPause"
|
||||||
|
super.onPause()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStop() {
|
override fun onStop() {
|
||||||
@ -146,5 +80,4 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
|
|||||||
ViewModelMain.isVisible.postValue(false)
|
ViewModelMain.isVisible.postValue(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,9 +5,13 @@ import android.annotation.SuppressLint
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.graphics.PixelFormat
|
import android.graphics.PixelFormat
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
import android.os.Handler
|
||||||
|
import android.os.Looper
|
||||||
import android.util.DisplayMetrics
|
import android.util.DisplayMetrics
|
||||||
import android.view.*
|
import android.view.*
|
||||||
import android.view.accessibility.AccessibilityEvent
|
import android.view.accessibility.AccessibilityEvent
|
||||||
|
import android.widget.ImageView
|
||||||
|
import android.widget.TextView
|
||||||
import androidx.lifecycle.Lifecycle
|
import androidx.lifecycle.Lifecycle
|
||||||
import androidx.lifecycle.LifecycleOwner
|
import androidx.lifecycle.LifecycleOwner
|
||||||
import androidx.lifecycle.LifecycleRegistry
|
import androidx.lifecycle.LifecycleRegistry
|
||||||
@ -15,6 +19,7 @@ import com.ray650128.floatwindowdemo.R
|
|||||||
import com.ray650128.floatwindowdemo.utils.ItemViewTouchListener
|
import com.ray650128.floatwindowdemo.utils.ItemViewTouchListener
|
||||||
import com.ray650128.floatwindowdemo.utils.Utils.isNull
|
import com.ray650128.floatwindowdemo.utils.Utils.isNull
|
||||||
import com.ray650128.floatwindowdemo.utils.ViewModelMain
|
import com.ray650128.floatwindowdemo.utils.ViewModelMain
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @功能:利用無障礙打開懸浮視窗 無侷限性 任何界面可以顯示
|
* @功能:利用無障礙打開懸浮視窗 無侷限性 任何界面可以顯示
|
||||||
@ -23,9 +28,23 @@ import com.ray650128.floatwindowdemo.utils.ViewModelMain
|
|||||||
* @Compony 永遠相信美好的事情即將發生
|
* @Compony 永遠相信美好的事情即將發生
|
||||||
*/
|
*/
|
||||||
class WorkAccessibilityService : AccessibilityService(), LifecycleOwner {
|
class WorkAccessibilityService : AccessibilityService(), LifecycleOwner {
|
||||||
|
|
||||||
private lateinit var windowManager: WindowManager
|
private lateinit var windowManager: WindowManager
|
||||||
private var floatRootView: View? = null//懸浮窗View
|
private var floatRootView: View? = null//懸浮窗View
|
||||||
private val mLifecycleRegistry = LifecycleRegistry(this)
|
private val mLifecycleRegistry = LifecycleRegistry(this)
|
||||||
|
|
||||||
|
|
||||||
|
private val timeUpdateHandler = Handler(Looper.getMainLooper())
|
||||||
|
private val timeUpdateRunnable = object : Runnable {
|
||||||
|
@SuppressLint("SimpleDateFormat")
|
||||||
|
override fun run() {
|
||||||
|
val now = System.currentTimeMillis()
|
||||||
|
val simpleDateFormat = SimpleDateFormat("HH:mm:ss")
|
||||||
|
floatRootView?.findViewById<TextView>(R.id.tv_time)?.text = simpleDateFormat.format(now)
|
||||||
|
timeUpdateHandler.postDelayed(this, 1000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||||
@ -44,6 +63,7 @@ class WorkAccessibilityService : AccessibilityService(), LifecycleOwner {
|
|||||||
if (!isNull(floatRootView?.windowToken)) {
|
if (!isNull(floatRootView?.windowToken)) {
|
||||||
if (!isNull(windowManager)) {
|
if (!isNull(windowManager)) {
|
||||||
windowManager.removeView(floatRootView)
|
windowManager.removeView(floatRootView)
|
||||||
|
timeUpdateHandler.removeCallbacks(timeUpdateRunnable)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -68,13 +88,18 @@ class WorkAccessibilityService : AccessibilityService(), LifecycleOwner {
|
|||||||
}
|
}
|
||||||
flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|
flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|
||||||
//flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|
//flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|
||||||
width = WindowManager.LayoutParams.WRAP_CONTENT
|
width = 260
|
||||||
height = WindowManager.LayoutParams.WRAP_CONTENT
|
height = 60
|
||||||
format = PixelFormat.TRANSPARENT
|
format = PixelFormat.TRANSPARENT
|
||||||
}
|
}
|
||||||
floatRootView = LayoutInflater.from(this).inflate(R.layout.activity_float_item, null)
|
floatRootView = LayoutInflater.from(this).inflate(R.layout.activity_float_item, null)
|
||||||
|
floatRootView?.findViewById<ImageView>(R.id.iv_close)?.setOnClickListener {
|
||||||
|
windowManager.removeView(floatRootView)
|
||||||
|
timeUpdateHandler.removeCallbacks(timeUpdateRunnable)
|
||||||
|
}
|
||||||
floatRootView?.setOnTouchListener(ItemViewTouchListener(layoutParam, windowManager))
|
floatRootView?.setOnTouchListener(ItemViewTouchListener(layoutParam, windowManager))
|
||||||
windowManager.addView(floatRootView, layoutParam)
|
windowManager.addView(floatRootView, layoutParam)
|
||||||
|
timeUpdateHandler.post(timeUpdateRunnable)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -84,9 +109,10 @@ class WorkAccessibilityService : AccessibilityService(), LifecycleOwner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getLifecycle(): Lifecycle = mLifecycleRegistry
|
override fun getLifecycle(): Lifecycle = mLifecycleRegistry
|
||||||
override fun onStart(intent: Intent?, startId: Int) {
|
|
||||||
super.onStart(intent, startId)
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
|
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
|
||||||
|
return super.onStartCommand(intent, flags, startId)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onUnbind(intent: Intent?): Boolean {
|
override fun onUnbind(intent: Intent?): Boolean {
|
||||||
|
|||||||
@ -47,8 +47,7 @@ object Utils {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
val myManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
val myManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
||||||
val runningService =
|
val runningService = myManager.getRunningServices(1000) as ArrayList<ActivityManager.RunningServiceInfo>
|
||||||
myManager.getRunningServices(1000) as ArrayList<ActivityManager.RunningServiceInfo>
|
|
||||||
for (i in runningService.indices) {
|
for (i in runningService.indices) {
|
||||||
if (runningService[i].service.className == ServiceName) {
|
if (runningService[i].service.className == ServiceName) {
|
||||||
return true
|
return true
|
||||||
@ -62,15 +61,13 @@ object Utils {
|
|||||||
*/
|
*/
|
||||||
private fun commonROMPermissionCheck(context: Context?): Boolean {
|
private fun commonROMPermissionCheck(context: Context?): Boolean {
|
||||||
var result = true
|
var result = true
|
||||||
if (Build.VERSION.SDK_INT >= 23) {
|
try {
|
||||||
try {
|
val clazz: Class<*> = Settings::class.java
|
||||||
val clazz: Class<*> = Settings::class.java
|
val canDrawOverlays =
|
||||||
val canDrawOverlays =
|
clazz.getDeclaredMethod("canDrawOverlays", Context::class.java)
|
||||||
clazz.getDeclaredMethod("canDrawOverlays", Context::class.java)
|
result = canDrawOverlays.invoke(null, context) as Boolean
|
||||||
result = canDrawOverlays.invoke(null, context) as Boolean
|
} catch (e: Exception) {
|
||||||
} catch (e: Exception) {
|
Log.e("ServiceUtils", Log.getStackTraceString(e))
|
||||||
Log.e("ServiceUtils", Log.getStackTraceString(e))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,23 +9,6 @@
|
|||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:paddingTop="50dp"
|
android:paddingTop="50dp"
|
||||||
tools:context=".MainActivity">
|
tools:context=".MainActivity">
|
||||||
<Button
|
|
||||||
android:id="@+id/bt_01"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="应用内悬浮窗口(当前页面)" />
|
|
||||||
|
|
||||||
<Button
|
|
||||||
android:id="@+id/bt_02"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="系统悬浮窗口(全局显示)[锁屏无法显示]" />
|
|
||||||
|
|
||||||
<Button
|
|
||||||
android:id="@+id/bt_04"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="系统悬浮窗口(前台显示)" />
|
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/bt_03"
|
android:id="@+id/bt_03"
|
||||||
@ -33,12 +16,6 @@
|
|||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="系统悬浮窗口(全局显示)[无限制]" />
|
android:text="系统悬浮窗口(全局显示)[无限制]" />
|
||||||
|
|
||||||
<Button
|
|
||||||
android:id="@+id/bt_05"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="恢复初始状态" />
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/textView"
|
android:id="@+id/textView"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user