第三部分:
3. SettingActivity
public class SettingActivity extends AppCompatActivity {
// setting 参数设置map key
public static final String POWER = "power";
public static final String URL = "url";
public static final String VEHICLE_ID = "vehicle_id";
public static final String VEHICLE_TYPE = "vehicle_type";
public static final String ACTION = "action";
public static final String FIRST = "first";
// EPC map key
public static final String EPC = "epc";
// action 模式
public static final String ACTION_LOAD = "load";
public static final String ACTION_UNLOAD = "unload";
// Vehicle type
public static final String TYPE_CAR = "car";
public static final String TYPE_TRAN = "tran";
public static final String TYPE_PLANE = "plane";
// power range
public static final int MAXPOWER = 26;
public static final int MINPOWER = 16;
// default value
public static final String DEFAULT_URL = "http://idart311.ticp.net/traffic.php";
public static final String DEFAULT_VEHICLE_ID = "1";
// power 显示字符串
public static final String POWER_TEXT = "POWER(db) : ";
// 实例域
private TextView mPowerText; // power文字描述显示控件
private SeekBar mSeekBar; // 调节power控件
private EditText mURL; // url编辑区
private Button mOkButton; // 确定按钮
private Button mCancelButton; // 取消按钮
private EditText mVehicleID; // vehicle_id编辑区
private RadioGroup mActionRadioGroup; // action选择区
private RadioGroup mVehicleRadioGroup; // vehicle_type选择区
private String mType = ""; // 存储vehicel_type的更改
private String mAction = ""; // 存储Action的更改
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
//ActionBar设置
Toolbar toolbar = (Toolbar) findViewById(R.id.setting_toolbar);
toolbar.setNavigationIcon(android.R.drawable.ic_dialog_dialer);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
setSupportActionBar(toolbar);
//power setting
mPowerText = (TextView)findViewById(R.id.powerset_text);
int v = sp.getInt(POWER, MAXPOWER);
mPowerText.setText(POWER_TEXT + v);
mSeekBar = (SeekBar)findViewById(R.id.powerset_seekbar);
mSeekBar.setProgress(v - MINPOWER);
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int n = seekBar.getProgress() + 16;
mPowerText.setText("POWER(dBm) : " + n);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//URL
mURL = (EditText)findViewById(R.id.urlset_edit);
String s = sp.getString(URL, "");
mURL.setText(s);
//Vehicle_id
mVehicleID = (EditText)findViewById(R.id.vehicle_edit);
String ves = sp.getString(VEHICLE_ID, "");
mVehicleID.setText(ves);
//Vehicle_type
mVehicleRadioGroup = (RadioGroup)findViewById(R.id.vehicle_type_radiogroup);
String vts = sp.getString(VEHICLE_TYPE, "");
switch (vts) {
case TYPE_CAR :
mVehicleRadioGroup.check(R.id.type_car);
break;
case TYPE_TRAN:
mVehicleRadioGroup.check(R.id.type_tran);
break;
case TYPE_PLANE :
mVehicleRadioGroup.check(R.id.type_plane);
break;
default:
break;
}
mVehicleRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
case R.id.type_car :
mType = TYPE_CAR;
break;
case R.id.type_tran :
mType = TYPE_TRAN;
break;
case R.id.type_plane :
mType = TYPE_PLANE;
break;
default:
break;
}
}
});
//action
mActionRadioGroup = (RadioGroup)findViewById(R.id.action_radiogroup);
String as = sp.getString(ACTION, "");
switch (as) {
case ACTION_LOAD:
mActionRadioGroup.check(R.id.action_load);
break;
case ACTION_UNLOAD:
mActionRadioGroup.check(R.id.action_unload);
break;
default:
break;
}
mActionRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.action_load) {
mAction = ACTION_LOAD;
} else if(checkedId == R.id.action_unload) {
mAction = ACTION_UNLOAD;
}
}
});
//Button
mOkButton = (Button)findViewById(R.id.setting_ok_button);
mOkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedpre = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int power_edit = mSeekBar.getProgress() + MINPOWER;
String url_edit = mURL.getText().toString().trim();
String vehicle_id_edit = mVehicleID.getText().toString().trim();
String vehicle_type_edit = mType;
String action_edit = mAction;
sharedpre.edit()
.putInt(POWER, power_edit)
.putString(URL, url_edit)
.putString(VEHICLE_ID, vehicle_id_edit)
.putString(FIRST, FIRST)
.commit();
// 若mType不为空。则已改变
if (!vehicle_type_edit.equals("")) {
sharedpre.edit()
.putString(VEHICLE_TYPE, vehicle_type_edit)
.commit();
}
// 若mAction不为空。则已改变
if (!action_edit.equals("")) {
sharedpre.edit()
.putString(ACTION, action_edit)
.commit();
}
// 启动connectactivity,重新连接
Intent i = new Intent(SettingActivity.this, ConnectActivity.class);
startActivity(i);
// 销毁之前的mainActivity和此settingactivity
ActivityArray.getActivityArray(getApplicationContext()).getArray().get(0).finish();
ActivityArray.getActivityArray(getApplicationContext()).getArray().remove(0);
finish();
}
});
mCancelButton = (Button)findViewById(R.id.setting_cancel_button);
mCancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// 向上按钮的监听
case android.R.id.home :
finish();
break;
default:
break;
}
return true;
}
}