实现强制下线功能
强制下线应该是一个比较常用的功能,比如QQ在比的地方被登陆了,就会强制比被挤下线. 强制下线的功能还是比较简单的,只需要在界面上弹出一个框,告知用户无法再进行任何操作即可.只能点击确定然后跳转至登录界面. 强制下线功能需要关闭所有的Activity,然后返回到登录界面 先编写一个ActivityCollector类用来管理所有的Activity
object ActivityCollector {
private val activities = ArrayList< Activity> ( )
fun addActivity ( activity: Activity) {
activities. add ( activity)
}
fun removeActivity ( activity: Activity) {
activities. remove ( activity)
}
fun finishAll ( ) {
for ( activity in activities) {
if ( ! activity. isFinishing ( ) ) {
activity. finish ( )
}
}
activities. clear ( )
}
}
然后创建一个BaseActivity作为所有Activity的父类
open class BaseActivity : AppCompatActivity ( ) {
override fun onCreate ( savedInstanceState: Bundle? ) {
super . onCreate ( savedInstanceState)
ActivityCollector. addActivity ( this )
}
override fun onDestroy ( ) {
super . onDestroy ( )
ActivityCollector. removeActivity ( this )
}
}
编写一个用于登录的Activity以及对应的xml布局文件
class LoginActivity : BaseActivity ( ) {
override fun onCreate ( savedInstanceState: Bundle? ) {
super . onCreate ( savedInstanceState)
setContentView ( R. layout. activity_login)
login. setOnClickListener {
val account = accountEdit. text. toString ( )
val password = passwordEdit. text. toString ( )
if ( account == "admin" && password == "123456" ) {
val intent = Intent ( this , MainActivity:: class . java)
startActivity ( intent)
finish ( )
} else {
Toast. makeText ( this , "account or password is invalid" , Toast. LENGTH_SHORT) . show ( )
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns: android= " http://schemas.android.com/apk/res/android"
android: layout_width= " match_parent"
android: layout_height= " match_parent"
android: orientation= " vertical" >
< LinearLayout
android: layout_width= " match_parent"
android: layout_height= " 60dp"
android: orientation= " vertical" >
< TextView
android: layout_width= " 90dp"
android: layout_height= " wrap_content"
android: layout_gravity= " center_vertical"
android: text= " Account:"
android: textSize= " 18sp" />
< EditText
android: id= " @+id/accountEdit"
android: layout_width= " 0dp"
android: layout_height= " wrap_content"
android: layout_gravity= " center_vertical"
android: layout_weight= " 1" />
</ LinearLayout>
< LinearLayout
android: layout_width= " match_parent"
android: layout_height= " 60dp"
android: orientation= " vertical" >
< TextView
android: layout_width= " 90dp"
android: layout_height= " wrap_content"
android: layout_gravity= " center_vertical"
android: text= " Password:"
android: textSize= " 18sp" />
< EditText
android: id= " @+id/passwordEdit"
android: layout_width= " 0dp"
android: layout_height= " wrap_content"
android: layout_gravity= " center_vertical"
android: layout_weight= " 1" />
</ LinearLayout>
< Button
android: id= " @+id/login"
android: layout_width= " 200dp"
android: layout_height= " 60dp"
android: layout_gravity= " center_horizontal"
android: text= " Login" />
</ LinearLayout>
加入强制下线功能,修改activity_main.xml和MainActivity当中的代码
class MainActivity : AppCompatActivity ( ) {
override fun onCreate ( savedInstanceState: Bundle? ) {
super . onCreate ( savedInstanceState)
setContentView ( R. layout. activity_main)
forceOffline. setOnClickListener {
val intent = Intent ( "com.zb.broadcastbestpractice.FORCE_OFFLINE" )
sendBroadcast ( intent)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns: android= " http://schemas.android.com/apk/res/android"
android: orientation= " vertical"
android: layout_width= " match_parent"
android: layout_height= " match_parent" >
< Button
android: layout_width= " match_parent"
android: layout_height= " wrap_content"
android: id= " @+id/forceOffline"
android: text= " Send force offline broadcast" />
</ LinearLayout>
我们通过按钮的点击事件发送了一条广播,广播的值为com.zb.broadcastbestpractice.FORCE_OFFLINE,这条广播就意在通知程序强制用户下线 强制用户下线的逻辑并不是写在MainActivity当中的,而是应该写在接收这条广播的BroadcastReceiver当中 这样不管在任何的地方,只要发送一条广播,就可以完成强制下线得操作了 创建一个BroadcastReceiver来接收这条强制下线的广播 在BaseActivity当中动态注册一个BroadcastReceiver,因为所有的Activity都继承自BaseActivity,那么每个Activity都可以接收这条广播了. 编写一个ForceOfflineReceiver继承BroadcastReceiver,在onReceive当中编写具体的广播逻辑
在AndroidManifest.xml当中修改一下代码,将登录界面设置成为主界面