Android开发

kotlin实现强制下线功能

本文主要是介绍kotlin实现强制下线功能,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

class ActivityCollector {
  //var activities :MutableList<Activity>=MutableList<Activity>()
  companion object{
    val activities = ArrayList<Activity>()
    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()
    }
  }
}

建立一个登录的布局文件

<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="horizontal">
  <TextView
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:textSize="18sp"
    android:text="account:"
    />
  <EditText
    android:id="@+id/account"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_gravity="center_vertical"
    />
</LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:orientation="horizontal">
    <TextView
      android:layout_width="90dp"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      android:textSize="18sp"
      android:text="password:"
      />
    <EditText
      android:id="@+id/password"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:layout_gravity="center_vertical"
      android:inputType="textPassword"
      />
  </LinearLayout>
  <Button
    android:id="@+id/login"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:text="Login"
    />
</LinearLayout>

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

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修改,把主活动设置更换一下。

<application
  android:allowBackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:roundIcon="@mipmap/ic_launcher_round"
  android:supportsRtl="true"
  android:theme="@style/AppTheme">
  <activity android:name=".MainActivity">
  </activity>
  <activity android:name=".LoginActivity" android:launchMode="singleTask">
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
</application>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。

这篇关于kotlin实现强制下线功能的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!