- 前提准备(view binding)
-
新建一个 BroadcastBestPractice 项目
-
创建一个 ActivityCollector 类用于管理所有的 Activity
object ActivityCollector {//用于管理所有的Activity private val activities = ArrayList<Activity>() fun addActivity(activity: Activity) { activities.add(activity) } fun removeActivity(activity: Activity) { activities.remove(activity) } fun finishAll() { for (activity in activities) { if (!activity.isFinishing) { activity.finish() } } activities.clear() } }
- 创建BaseActivity类
open class BaseActivity : AppCompatActivity() { lateinit var receiver: ForceOfflineReceiver override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) ActivityCollector.addActivity(this) } override fun onResume() { super.onResume() val intentFilter = IntentFilter() intentFilter.addAction("com.example.broadcastbestpractice.FORCE_OFFLINE") receiver = ForceOfflineReceiver() registerReceiver(receiver, intentFilter) } override fun onPause() { super.onPause() unregisterReceiver(receiver) } override fun onDestroy() { super.onDestroy() ActivityCollector.removeActivity(this) } inner class ForceOfflineReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { AlertDialog.Builder(context).apply { setTitle("Warning") setMessage("You are forced to be offline. Please try to login again.") setCancelable(false) setPositiveButton("OK") { _, _ -> ActivityCollector.finishAll() // 销毁所有Activity val i = Intent(context, LoginActivity::class.java) context.startActivity(i) // 重新启动LoginActivity } show() } } } }
- 创建一个LoginActivity来作为登录界面编辑布局文件activity_login.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<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/accountEdit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<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/passwordEdit"
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="200dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:text="Login" />
</LinearLayout>
- 修改LoginActivity中的代码
class LoginActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_login) val binding =ActivityLoginBinding.inflate(layoutInflater) setContentView(binding.root) binding.login.setOnClickListener { val account = binding.accountEdit.text.toString() val password = binding.passwordEdit.text.toString() // 如果账号是admin且密码是123456,就认为登录成功 if (account == "admin" && password == "123456") { val intent = Intent(this, MainActivity::class.java) startActivity(intent) finish() } else { Toast.makeText(this, "account or password is invalid", Toast.LENGTH_SHORT).show() } } } }
- 修改activity_main.xml中的代 码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/forceOffline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send force offline broadcast" />
</LinearLayout>
- 修改MainActivity中的代码
class MainActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val binding = ActivityMainBinding.inflate(layoutInflater); setContentView(binding.root); binding.forceOffline.setOnClickListener { val intent = Intent("com.example.broadcastbestpractice.FORCE_OFFLINE") sendBroadcast(intent) } } }
- 对AndroidManifest.xml文件进行修改将主Activity设置为LoginActivity
- 书中使用的kotlin-android-extensions插件已被废弃需要使用ViewBinding
android { ... buildFeatures { viewBinding true } }