kotlin实现强制下线功能

时间:2021-10-18 08:19:40

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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()
    }
  }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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()
    }
  }
}

建立一个登录的布局文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<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>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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的代码

?
1
2
3
4
5
6
7
8
9
10
11
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修改,把主活动设置更换一下。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_40119478/article/details/80525686