In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to realize the suspension window of Android system by Kotlin". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Talking about the pop-up window of Android
We know that in the Android pop-up window, there is a type of pop-up window that will also be displayed outside the application, this is because it is declared as the system pop-up window, and there are two other types of pop-up window: bullet window and application pop-up window.
Application pop-up window: we routinely use Dialog pop-up window, which depends on the Activity; pop-up window of the application: depends on the parent window, such as PopupWindow; system pop-up window, such as status bar, Toast, etc. The system suspension window mentioned in this article is the system pop-up window.
Design of permission application code for system suspension window
The following screenshot of the package structure simply shows the code structure of the system suspension window, on which more complex business needs can be expanded.
FloatWindowService: the system suspension window pops up in this Service
FloatWindowManager: system suspension window management class
FloatLayout: system suspension window layout
HomeKeyObserverReceiver:
Listen for Home key
FloatWindowUtils: system suspension window tool class.
Implement the FloatWindowService class class FloatWindowService: Service () {privateval TAG = FloatWindowService::class.java.simpleName private var mFloatWindowManager: FloatWindowManager? = null private var mHomeKeyObserverReceiver: HomeKeyObserverReceiver? = null override fun onCreate () {TLogUtils.i (TAG, "onCreate:") mFloatWindowManager = FloatWindowManager (applicationContext) mHomeKeyObserverReceiver = HomeKeyObserverReceiver () registerReceiver (mHomeKeyObserverReceiver IntentFilter (Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) mFloatWindowManagermanagers. CreateWindow ()} override fun onBind (intent: Intent?): IBinder? {return null} override fun onStartCommand (intent: Intent?, flags: Int, startId: Int): Int {return START_NOT_STICKY} override fun onDestroy () {TLogUtils.i (TAG) "onDestroy:") mFloatWindowManager?.removeWindow () if (mHomeKeyObserverReceiver! = null) {unregisterReceiver (mHomeKeyObserverReceiver)} FloatWindowManager class
Including the creation, display, destruction (and update) of the system suspension window.
Public void addView (View view, ViewGroup.LayoutParams params); / / add View to Windowpublic void updateViewLayout (View view, ViewGroup.LayoutParams params); / / update the location of View in Window public void removeView (View view) / delete ViewFloatWindowManager class code class FloatWindowManager constructor (context: Context) {var isShowing = false privateval TAG = FloatWindowManager::class.java.simpleName private var mContext: Context = context private var mFloatLayout = FloatLayout (mContext) private var mLayoutParams: WindowManager.LayoutParams? = null private var mWindowManager: WindowManager = context.getSystemService (Context.WINDOW_SERVICE) as WindowManager fun createWindow () {TLogUtils.i (TAG, "createWindow: start...") / / object configuration operation uses apply Additional processing uses also mLayoutParams = WindowManager.LayoutParams (). Apply {type = if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.O) {/ / Android requires TYPE_APPLICATION_OVERLAY after 8.0. the following window types are not allowed to display reminder windows on top of other applications and windows: TYPE_PHONE, TYPE_PRIORITY_PHONE, TYPE_SYSTEM_ALERT, TYPE_SYSTEM_OVERLAY, TYPE_SYSTEM_ERROR. WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY} else {/ / before Android 8.0, hovering windows can be set to TYPE_PHONE, which is a type of non-application window used to provide user interaction. / / when API Level = 23, you need to declare the permission SYSTEM_ALERT_WINDOW in the Android Manifest.xml file before you can draw the control WindowManager.LayoutParams.TYPE_PHONE} / / set the picture format on other applications. The effect is that the background is transparent format = PixelFormat.RGBA_8888 / / sets the floating window not to be focused (to operate other visible windows except the floating window) flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE / / adjust the docking position of the floating window to the right top gravity = Gravity.TOP or Gravity.END Width = 800height = 200x = 20y = 40} mWindowManager.addView (mFloatLayout MLayoutParams) TLogUtils.i (TAG, "createWindow: end...") IsShowing = true} fun showWindow () {TLogUtils.i (TAG, "showWindow: isShowing = $isShowing") if (! isShowing) {if (mLayoutParams = = null) {createWindow ()} else {mWindowManager.addView (mFloatLayout) MLayoutParams) isShowing = true}} fun removeWindow () {TLogUtils.i (TAG, "removeWindow: isShowing = $isShowing") mWindowManager.removeView (mFloatLayout) isShowing = false}} FloatLayout class and its Layout
System suspension window Custom View:FloatLayout
Class FloatLayout @ JvmOverloads constructor (context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0): ConstraintLayout (context, attrs, defStyleAttr, defStyleRes) {private var mTime: TCLTextView private var mDistance: TCLTextView private var mSpeed: TCLTextView private var mCalories: TCLTextView init {val view = LayoutInflater.from (context) .expiate (R.layout.do_exercise_view_float_layout, this) True) mTime = view.findViewById (R.id.float_layout_tv_time) mDistance = view.findViewById (R.id.float_layout_tv_distance) mSpeed = view.findViewById (R.id.float_layout_tv_speed) mCalories = view.findViewById (R.id.float_layout_tv_calories)}}
Layout files: float_layout_tv_time
HomeKeyObserverReceiver class class HomeKeyObserverReceiver: BroadcastReceiver () {override fun onReceive (context: Context?, intent: Intent?) {try {val action = intentroomroom.action val reason = intent.getStringExtra ("reason") TLogUtils.d (TAG, "HomeKeyObserverReceiver: action = $action Reason = $reason ") if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS = = action & &" homekey "= = reason) {val keyCode = intent.getIntExtra (" keycode ", KeyEvent.KEYCODE_UNKNOWN) TLogUtils.d (TAG," keyCode = $keyCode ") context?.stopService (Intent (context) FloatWindowService::class.java)}} catch (ex: Exception) {ex.printStackTrace ()}} companion object {privateval TAG = HomeKeyObserverReceiver::class.java.simpleName}} FloatWindowUtils class object FloatWindowUtils {const val REQUEST_FLOAT_CODE = 1000 privateval TAG = FloatWindowUtils::class.java.simpleName / * determine whether Service is enabled * / fun isServiceRunning (context: Context ServiceName: String): Boolean {if (TextUtils.isEmpty (ServiceName)) {return false} val myManager = context.getSystemService (Context.ACTIVITY_SERVICE) as ActivityManager val runningService = myManager.getRunningServices (1000) as ArrayList runningService.forEach {if (it.service.className = = ServiceName) {return true} return False} / * check whether the suspension window permission is enabled * / @ SuppressLint ("NewApi") fun checkSuspendedWindowPermission (context: Activity) Block: ()-> Unit) {if (commonROMPermissionCheck (context)) {block ()} else {Toast.makeText (context, "Please open the suspension window permission" Toast.LENGTH_SHORT) .show () context.startActivityForResult (Intent (Settings.ACTION_MANAGE_OVERLAY_PERMISSION). Apply {data = Uri.parse ("package:$ {context.packageName}")} REQUEST_FLOAT_CODE)}} / * determine suspension window permissions * / fun commonROMPermissionCheck (context: Context?): Boolean {var result = true if (Build.VERSION.SDK_INT > = 23) {try {val clazz: Class = Settings::class.java val canDrawOverlays = clazz.getDeclaredMethod ("canDrawOverlays") Context::class.java) result = canDrawOverlays.invoke (null, context) as Boolean} catch (e: Exception) {TLogUtils.e (TAG, e)}} return result}} "how Kotlin implements the suspension window of Android system" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.