我有一个OnOff开关,当“选中”它时,它应允许用户根据他们选择的特定行接收通知。
但是我似乎无法弄清楚如何对其进行管理,即当用户选择了其路线并接通了开关时,该开关将仅停留在其选择的线路上。
旁注,切换开关还会添加地理围栏,并启动一项服务来轮询gps位置(虽然不重要,但我认为这样可能会有所帮助)
因此,用户交互过程始于MainActivity,它允许用户通过单击“ RED Line”之类的按钮来选择其行
然后,在单击的侦听器上的按钮上附加了一个名为“选择行”的静态变量,它是一个全局字符串,然后在单击按钮后,将打开“时间表活动”,这就是发生所有魔术的地方。
全局变量
public static String SelectedLine = "";
用户点击粉红色线条时的OnClicklistener
mPink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SelectedLine = "PINKLINE";
Intent timeTableIntent = new Intent(MainActivity.this, TimeTable.class);
startActivity(timeTableIntent);
finish();
}
});
它显示了时间表和一个Switch,用户可以在其中切换以获取通知。
交换机的通用代码
private void service_enabled() {
notificationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
saveData();
if(isChecked)
{
if (!checkPermissions()){
mPendingGeofenceTask = PendingGeofenceTask.ADD;
requestPermissions();
return;
}
Notifications();
addGeofences();
Intent i = new Intent(getApplicationContext(),GeoJobService.class);
startService(i);
Toast.makeText(TimeTable.this, "Notification Switch "+isChecked, Toast.LENGTH_SHORT).show();
}
if(!isChecked)
{
Intent i = new Intent(getApplicationContext(),GeoJobService.class);
stopService(i);
if (!checkPermissions()) {
mPendingGeofenceTask = PendingGeofenceTask.REMOVE;
requestPermissions();
return;
}
removeGeofences();
notificationSwitch.setChecked(false);
}
}
});
}
private void saveData() {
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(NotSwitch,notificationSwitch.isChecked());
editor.apply();
// Toast.makeText(this, "Saved Data", Toast.LENGTH_SHORT).show();
}
public void loadData()
{
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);
switchOnOff = sharedPreferences.getBoolean(NotSwitch,false);
// Toast.makeText(this, "Loaded Data", Toast.LENGTH_SHORT).show();
}
public void updateView()
{
notificationSwitch.setChecked(switchOnOff);
}
我已经尝试过了,它似乎可以实现我想要的功能,但是这种行为似乎很奇怪,我认为由于开关的SharedPrefences,当我打开它时它会自动保持打开状态(对于我选择的所有路由)但是,当我在每个Actvity中恢复或调用onCreate方法时,checkNotifications方法被称为切换,相应的开关会关闭,但是那确实感觉到了我的问题的答案,它感觉更像是用字符串和胶带遮盖住的样子
void CheckNotifications()
{
String SelectedLine = MainActivity.SelectedLine;
Toast.makeText(this, SelectedLineNotifications, Toast.LENGTH_SHORT).show();
if(notificationSwitch.isChecked())
{
switch(SelectedLine)
{
case "REDLINE":
SelectedLineNotifications = "REDLINE";
break;
case "BLUELINE":
SelectedLineNotifications = "BLUELINE";
break;
case "YELLOWLINE":
SelectedLineNotifications = "YELLOWLINE";
break;
case "GREENLINE":
SelectedLineNotifications = "GREENLINE";
break;
case "PURPLEINE":
SelectedLineNotifications = "PURPLELINE";
break;
case "ORANGELINE":
SelectedLineNotifications = "ORANGELINE";
break;
case "PINKLINE":
SelectedLineNotifications = "PINKLINE";
break;
case "GREYLINE":
SelectedLineNotifications = "GREYLINE";
break;
}
}
switch (SelectedLineNotifications)
{
case "REDLINE":
case "BLUELINE":
case "YELLOWLINE":
case "GREENLINE":
case "PURPLEINE":
case "ORANGELINE":
case "PINKLINE":
case "GREYLINE":
notificationSwitch.setChecked(true);
saveData();
break;
}
}
所以我想要知道的是如何处理此开关,如果用户在他/她选择的特定行中将开关切换为打开状态,并保持在用户选择的行中,则它将保持打开状态当他切换“打开”时
答案 0 :(得分:0)
我的建议是为每行制作几个不同的切换按钮。
然后将它们每个都保存在“共享偏好设置”中,像这样的不同键。
private void saveData(String key , boolean value) {
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key,value);
editor.apply();
}
public void loadData(String key)
{
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);
switchOnOff = sharedPreferences.getBoolean(key,false);
// Toast.makeText(this, "Loaded Data", Toast.LENGTH_SHORT).show();
}
public void updateView(Switch selectedLineSwitch)
{
try{
selectedLineSwitch.setChecked(switchOnOff);
}catch (Exception e)
{
Log.e("Error", "did not update View");
}
}
这样称呼他们。
if(SelectedLine == "REDLINE")
{
loadData("red");
Log.d(TAG,"loaded Red Data");
redNotificationSwitch= (Switch) findViewById(R.id.red_switch);
redNotificationSwitch.setVisibility(View.VISIBLE);
updateView(redNotificationSwitch);
redNotificationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
saveData("red",isChecked);
SelectedLineNotifications = "REDLINE";
notificationSwitchMethod(redNotificationSwitch);
}
});
}