kotlin realizes forced offline function

  • 2021-09-11 21:21:37
  • OfStack

Forced offline requires closing all activities and creating a class to manage all activities.


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()
    }
  }
}

Then create a parent class for all activities


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()
    }
  }
}

Create a login layout file


<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>

Then write a login activity, here wrote a simple login, password is correct to enter the main page, otherwise give a hint.


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," Wrong account number or password ",Toast.LENGTH_SHORT).show()
      }
    })
  }
}

Then modify the code of mainactivity under 1


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)
    })
  }
}

Finally, modify AndroidManifest. xml, and change the main activity setting by 1.


<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>

Related articles: