一、效果图
二、源代码
静态广播弹出的弹窗是由悬浮弹窗所实现的
package com.study.forcelogoutdemo
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
class MainActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun sendDynamic(view: View) {
with(Intent()) {
action = FORCE_LOGOUT_ACTION
sendBroadcast(this)
}
}
fun sendStatic(view: View) {
with(Intent()) {
action = STATIC_FORCE_LOGOUT_ACTION
setPackage(packageName)
sendBroadcast(this)
}
}
}
package com.study.forcelogoutdemo
import android.app.AlertDialog
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.util.Log
import android.view.WindowManager
import android.widget.Toast
class StaticForceReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.e(javaClass.simpleName, context.toString())
if (Build.VERSION.SDK_INT >= 23) {
if (Settings.canDrawOverlays(context)) {
showDialogTip(context)
} else {
Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + context.packageName)
).apply {
addFlags(FLAG_ACTIVITY_NEW_TASK)
context.startActivity(this)
}
Toast.makeText(context, "请去设置中打开应用悬浮窗的权限", Toast.LENGTH_SHORT).show()
}
} else {
showDialogTip(context)
}
}
private fun showDialogTip(context: Context) {
val dialog = AlertDialog.Builder(context).run {
setMessage("静态注册的广播检测到您的登录状态异常,请重新登录")
setCancelable(false)
setPositiveButton("退出登录") { dialog, _ ->
dialog.dismiss()
ActivityCollector.clearAllActivity()
Intent(context, LoginActivity::class.java).run {
addFlags(FLAG_ACTIVITY_NEW_TASK)
context.startActivity(this)
}
}
create()
}
if (Build.VERSION.SDK_INT >= 26) {
dialog.window!!.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY)
} else {
dialog.window!!.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)
}
dialog.show()
}
}
报错示意图:
|