Android實現(xiàn)懸浮窗的簡單方法實例
1. 前言
現(xiàn)在很多應(yīng)用都有小懸浮窗的功能,比如看直播的時候,通過Home鍵返回桌面,直播的小窗口仍可以在屏幕上顯示。下面將介紹下懸浮窗的的一種簡單實現(xiàn)方式。
2.原理
Window我們應(yīng)該很熟悉,它是一個接口類,具體的實現(xiàn)類為PhoneWindow,它可以對View進(jìn)行管理。WindowManager是一個接口類,繼承自ViewManager,從名稱就知道它是用來管理Window的,它的實現(xiàn)類是WindowManagerImpl。如果我們想要對Window(View)進(jìn)行添加、更新和刪除操作就可以使用WindowManager,WindowManager會將具體的工作交由WindowManagerService處理。這里我們只需要知道WindowManager能用來管理Window就好。
WindowManager是一個接口類,繼承自ViewManager,ViewManager中定義了3個方法,分布用來添加、更新和刪除View,如下所示:
public interface ViewManager {
public void addView(View view, ViewGroup.LayoutParams params);
public void updateViewLayout(View view, ViewGroup.LayoutParams params);
public void removeView(View view);
}
WindowManager也繼承了這些方法,而這些方法傳入的參數(shù)都是View類型,說明了Window是以View的形式存在的。
3.具體實現(xiàn)
3.1浮窗布局
懸浮窗的簡易布局如下的可參考下面的layout_floating_window.xml文件。頂層深色部分的FrameLayout布局是用來實現(xiàn)懸浮窗的拖拽功能的,點擊右上角ImageView可以實現(xiàn)關(guān)閉懸浮窗,剩下區(qū)域顯示內(nèi)容,這里只是簡單地顯示文本內(nèi)容,不做復(fù)雜的東西,故只設(shè)置TextView。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/layout_drag"
android:layout_width="match_parent"
android:layout_height="15dp"
android:background="#dddddd">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_close"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="end"
android:src="@drawable/img_delete"/>
</FrameLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#eeeeee"
android:scrollbars="vertical"/>
</LinearLayout>
3.2 懸浮窗的實現(xiàn)
1. 使用服務(wù)Service
Service 是一種可在后臺執(zhí)行長時間運(yùn)行操作而不提供界面的應(yīng)用組件,可由其他應(yīng)用組件啟動,而且即使用戶切換到其他應(yīng)用,仍將在后臺繼續(xù)運(yùn)行。要保證應(yīng)用在后臺時,懸浮窗仍然可以正常顯示,所以這里可以使用Service。
2. 獲取WindowManager并設(shè)置LayoutParams
private lateinit var windowManager: WindowManager
private lateinit var layoutParams: WindowManager.LayoutParams
override fun onCreate() {
// 獲取WindowManager
windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
layoutParams = WindowManager.LayoutParams().apply {
// 實現(xiàn)在其他應(yīng)用和窗口上方顯示浮窗
type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_PHONE
}
format = PixelFormat.RGBA_8888
// 設(shè)置浮窗的大小和位置
gravity = Gravity.START or Gravity.TOP
flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
width = 600
height = 600
x = 300
y = 300
}
}
3. 創(chuàng)建View并添加到WindowManager
private lateinit var floatingView: View
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (Settings.canDrawOverlays(this)) {
floatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_window.xml, null)
windowManager.addView(floatingView, layoutParams)
}
return super.onStartCommand(intent, flags, startId)
}
4. 實現(xiàn)懸浮窗的拖拽和關(guān)閉功能
// 浮窗的坐標(biāo)
private var x = 0
private var y = 0
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (Settings.canDrawOverlays(this)) {
floatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_window.xml, null)
windowManager.addView(floatingView, layoutParams)
// 點擊浮窗的右上角關(guān)閉按鈕可以關(guān)閉浮窗
floatingView.findViewById<AppCompatImageView>(R.id.iv_close).setOnClickListener {
windowManager.removeView(floatingView)
}
// 實現(xiàn)浮窗的拖動功能, 通過改變layoutParams來實現(xiàn)
floatingView.findViewById<AppCompatImageView>(R.id.layout_drag).setOnTouchListener { v, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
x = event.rawX.toInt()
y = event.rawY.toInt()
}
MotionEvent.ACTION_MOVE -> {
val currentX = event.rawX.toInt()
val currentY = event.rawY.toInt()
val offsetX = currentX - x
val offsetY = currentY - y
x = currentX
y = currentY
layoutParams.x = layoutParams.x + offsetX
layoutParams.y = layoutParams.y + offsetY
// 更新floatingView
windowManager.updateViewLayout(floatingView, layoutParams)
}
}
true
}
return super.onStartCommand(intent, flags, startId)
}
5. 利用廣播進(jìn)行通信
private var receiver: MyReceiver? = null
override fun onCreate() {
// 注冊廣播
receiver = MyReceiver()
val filter = IntentFilter()
filter.addAction("android.intent.action.MyReceiver")
registerReceiver(receiver, filter)
}
inner class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val content = intent.getStringExtra("content") ?: ""
// 通過Handler更新UI
val message = Message.obtain()
message.what = 0
message.obj = content
handler.sendMessage(message)
}
}
val handler = Handler(this.mainLooper) { msg ->
tvContent.text = msg.obj as String
false
}
可以在Activity中通過廣播給Service發(fā)送信息
fun sendMessage(view: View?) {
Intent("android.intent.action.MyReceiver").apply {
putExtra("content", "Hello, World!")
sendBroadcast(this)
}
}
6. 設(shè)置權(quán)限
懸浮窗的顯示需要權(quán)限,在AndroidManefest.xml中添加:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
此外,還要通過Settings.ACTION_MANAGE_OVERLAY_PERMISSION來讓動態(tài)設(shè)置權(quán)限,在Activity中設(shè)置。
// MainActivity.kt
fun startWindow(view: View?) {
if (!Settings.canDrawOverlays(this)) {
startActivityForResult(Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:$packageName")), 0)
} else {
startService(Intent(this@MainActivity, FloatingWindowService::class.java))
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 0) {
if (Settings.canDrawOverlays(this)) {
Toast.makeText(this, "懸浮窗權(quán)限授權(quán)成功", Toast.LENGTH_SHORT).show()
startService(Intent(this@MainActivity, FloatingWindowService::class.java))
}
}
}
3.3 完整代碼
class FloatingWindowService : Service() {
private lateinit var windowManager: WindowManager
private lateinit var layoutParams: WindowManager.LayoutParams
private lateinit var tvContent: AppCompatTextView
private lateinit var handler: Handler
private var receiver: MyReceiver? = null
private var floatingView: View? = null
private val stringBuilder = StringBuilder()
private var x = 0
private var y = 0
// 用來判斷floatingView是否attached 到 window manager,防止二次removeView導(dǎo)致崩潰
private var attached = false
override fun onCreate() {
super.onCreate()
// 注冊廣播
receiver = MyReceiver()
val filter = IntentFilter()
filter.addAction("android.intent.action.MyReceiver")
registerReceiver(receiver, filter);
// 獲取windowManager并設(shè)置layoutParams
windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
layoutParams = WindowManager.LayoutParams().apply {
type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_PHONE
}
format = PixelFormat.RGBA_8888
// format = PixelFormat.TRANSPARENT
gravity = Gravity.START or Gravity.TOP
flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
width = 600
height = 600
x = 300
y = 300
}
handler = Handler(this.mainLooper) { msg ->
tvContent.text = msg.obj as String
// 當(dāng)文本超出屏幕自動滾動,保證文本處于最底部
val offset = tvContent.lineCount * tvContent.lineHeight
floatingView?.apply {
if (offset > height) {
tvContent.scrollTo(0, offset - height)
}
}
false
}
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
@SuppressLint("ClickableViewAccessibility")
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (Settings.canDrawOverlays(this)) {
floatingView = LayoutInflater.from(this).inflate(R.layout.layout_show_log, null)
tvContent = floatingView!!.findViewById(R.id.tv_log)
floatingView!!.findViewById<AppCompatImageView>(R.id.iv_close).setOnClickListener {
stringBuilder.clear()
windowManager.removeView(floatingView)
attached = false
}
// 設(shè)置TextView滾動
tvContent.movementMethod = ScrollingMovementMethod.getInstance()
floatingView!!.findViewById<FrameLayout>(R.id.layout_drag).setOnTouchListener { v, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
x = event.rawX.toInt()
y = event.rawY.toInt()
}
MotionEvent.ACTION_MOVE -> {
val currentX = event.rawX.toInt()
val currentY = event.rawY.toInt()
val offsetX = currentX - x
val offsetY = currentY - y
x = currentX
y = currentY
layoutParams.x = layoutParams.x + offsetX
layoutParams.y = layoutParams.y + offsetY
windowManager.updateViewLayout(floatingView, layoutParams)
}
}
true
}
windowManager.addView(floatingView, layoutParams)
attached = true
}
return super.onStartCommand(intent, flags, startId)
}
override fun onDestroy() {
// 注銷廣播并刪除浮窗
unregisterReceiver(receiver)
receiver = null
if (attached) {
windowManager.removeView(floatingView)
}
}
inner class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val content = intent.getStringExtra("content") ?: ""
stringBuilder.append(content).append("\n")
val message = Message.obtain()
message.what = 0
message.obj = stringBuilder.toString()
handler.sendMessage(message)
}
}
}
4. 總結(jié)
以上就是Android懸浮窗的一個簡單實現(xiàn)方式。如果需要實現(xiàn)其他復(fù)雜一點的功能,比如播放視頻,也可以在此基礎(chǔ)上完成。
到此這篇關(guān)于Android實現(xiàn)懸浮窗的簡單方法的文章就介紹到這了,更多相關(guān)Android實現(xiàn)懸浮窗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android基于PhotoView實現(xiàn)的頭像/圓形裁剪控件
這篇文章主要給大家介紹了關(guān)于Android基于PhotoView實現(xiàn)的頭像/圓形裁剪控件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
android2.3.5 CDMA/EVDO撥號APN解決方案
google提供的android2.3里面,只能在GSM/WCDMA情況下才能從“設(shè)置”->“無線和網(wǎng)絡(luò)”->“移動網(wǎng)絡(luò)”->“接入點名稱”中選擇不同的apn帳號進(jìn)行撥號連接,而CDMA/EVDO則沒有這個功能,接下來本文介紹一些方法實現(xiàn)這個功能,感興趣的朋友可以了解下2013-01-01
Android編輯框EditText與焦點變更監(jiān)視器及文本變化監(jiān)視器實現(xiàn)流程詳解
這篇文章主要介紹了Android編輯框EditText與焦點變更監(jiān)視器及文本變化監(jiān)視器實現(xiàn)流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09
android 版本檢測 Android程序的版本檢測與更新實現(xiàn)介紹
做個網(wǎng)站的安卓客戶端,用戶安裝到自己手機(jī)上,如果我出了新版本怎么辦呢?要有版本更新功能,感興趣的朋友可以了解下2013-01-01
Android Studio去除界面默認(rèn)標(biāo)題欄的方法
這篇文章主要介紹了Android Studio去除界面默認(rèn)標(biāo)題欄的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2007-09-09
Android修改DatePicker字體顏色及分割線顏色詳細(xì)介紹
這篇文章主要介紹了Android修改DatePicker字體顏色及分割線顏色詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-05-05
神奇的listView實現(xiàn)自動顯示隱藏布局Android代碼
這篇文章主要介紹了神奇的listView實現(xiàn)自動顯示隱藏布局Android代碼實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09

