您需要为每个交换机添加唯一的首选项。
添加PREF\u SWITCH\u PUSH和PREF\u SWITCH\u EMAIL首选项。
private fun onSwitch() {
val sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE)
val editor = sharedPreferences.edit()
push_switch1.isChecked = sharedPreferences.getBoolean(PREF_SWITCH_PUSH, false)
push_switch1.setOnCheckedChangeListener { _, isChecked ->
if (isChecked){
editor.putBoolean(PREF_SWITCH_PUSH, true)
Toast.makeText(this@MainActivity, "Push Notification ON", Toast.LENGTH_SHORT).show()
} else {
editor.putBoolean(PREF_SWITCH_PUSH, false)
Toast.makeText(this@MainActivity, "Push Notification Off", Toast.LENGTH_SHORT).show()
}
editor.apply()
}
email_switch1.isChecked = sharedPreferences.getBoolean(PREF_SWITCH_EMAIL, false)
email_switch1.setOnCheckedChangeListener { _, isChecked ->
if (isChecked){
editor.putBoolean(PREF_SWITCH_EMAIL, true)
Toast.makeText(this@MainActivity, "Email Notification ON", Toast.LENGTH_SHORT).show()
}else{
editor.putBoolean(PREF_SWITCH_EMAIL, false)
Toast.makeText(this@MainActivity, "Email Notification OFF", Toast.LENGTH_SHORT).show()
}
editor.apply()
}
}