溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

kotlin實(shí)現(xiàn)強(qiáng)制下線功能

發(fā)布時(shí)間:2020-09-06 19:19:27 來(lái)源:腳本之家 閱讀:193 作者:代碼沒(méi)寫(xiě)完 欄目:移動(dòng)開(kāi)發(fā)

強(qiáng)制下線是需要關(guān)閉所有的活動(dòng),先創(chuàng)建一個(gè)類(lèi)來(lái)管理所有的活動(dòng)。

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

然后建立一個(gè)所有活動(dòng)的父類(lèi)

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

建立一個(gè)登錄的布局文件

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

然后編寫(xiě)一個(gè)登錄的活動(dòng),這里寫(xiě)了一個(gè)簡(jiǎn)單的登錄,密碼正確的話就進(jìn)入主頁(yè)面,否則給出提示。

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,"賬號(hào)或密碼錯(cuò)誤",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)
    })
  }
}

最后對(duì)AndroidManifest.xml修改,把主活動(dòng)設(shè)置更換一下。

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI