189 8069 5689

kotlin实现强制下线功能

强制下线是需要关闭所有的活动,先创建一个类来管理所有的活动。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网络空间、营销软件、网站建设、敦煌网站维护、网站推广。

class ActivityCollector {
  //var activities :MutableList=MutableList()
  companion object{
    val activities = ArrayList()
    fun addActivity( activity:Activity){
      activities.add(activity)
    }
    fun removeActivity(activity:Activity){
      activities.remove(activity)
    }
    fun finsishAll(){
      for(activity:Activity in activities){
        if(!activity.isFinishing){
          activity.finish()
        }
      }
      activities.clear()
    }
  }
}

然后建立一个所有活动的父类

open class BaseActivity : AppCompatActivity(){
  var activityCollector=ActivityCollector()
  lateinit var receiver:ForceOfflineReceiver
  override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
    super.onCreate(savedInstanceState, persistentState)
    ActivityCollector.addActivity(this)
    //activityCollector.addActivity(this)
  }
  override fun onResume() {
    super.onResume()
    var inetnefilter:IntentFilter=IntentFilter()
    inetnefilter.addAction("FORCE_FOOLINE")
    receiver=ForceOfflineReceiver()
    registerReceiver(receiver,inetnefilter)
  }
  override fun onPause() {
    super.onPause()
    if(receiver!=null){
      unregisterReceiver(receiver)
      // receiver=null
    }
  }
  override fun onDestroy() {
    super.onDestroy()
    ActivityCollector.removeActivity(this)
  }
  open class ForceOfflineReceiver : BroadcastReceiver(){
    override fun onReceive(context: Context, intent: Intent?) {
      var builder :AlertDialog.Builder=AlertDialog.Builder(context)
      builder.setTitle("Warning")
      builder.setMessage("you are forced to be offline .please try to login again")
      builder.setCancelable(false)
      builder.setPositiveButton("ok",object :DialogInterface.OnClickListener{
        override fun onClick(dialog: DialogInterface?, which: Int) {
          ActivityCollector.finsishAll()
          var intent=Intent(context,LoginActivity::class.java)
          context.startActivity(intent)
        }
      })
      builder.show()
    }
  }
}

建立一个登录的布局文件



  
  

  
    
    
  
  

然后编写一个登录的活动,这里写了一个简单的登录,密码正确的话就进入主页面,否则给出提示。

class LoginActivity:BaseActivity(){
   lateinit var accountEdid:EditText
   lateinit var passwordEdit :EditText
    lateinit var login:Button
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.login)
    accountEdid=findViewById(R.id.account)
    passwordEdit=findViewById(R.id.password)
    login=findViewById(R.id.login)
    login.setOnClickListener(View.OnClickListener {
      var account =accountEdid.text.toString()
      var password=passwordEdit.text.toString()
      if(account.equals("admin")&&password.equals("123")){
        var intent:Intent=Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
      }else{
        Toast.makeText(this,"账号或密码错误",Toast.LENGTH_SHORT).show()
      }
    })
  }
}

然后修改一下mainactivity的代码

class MainActivity : BaseActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    var forceOffline :Button =findViewById(R.id.force_offline)
    forceOffline.setOnClickListener(View.OnClickListener {
      var intent:Intent=Intent("FORCE_FOOLINE")
      sendBroadcast(intent)
    })
  }
}

最后对AndroidManifest.xml修改,把主活动设置更换一下。


  
  
  
    
      
      
    
  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


分享名称:kotlin实现强制下线功能
当前网址:http://jkwzsj.com/article/jhosdi.html

其他资讯