我正在尝试创建一个Android应用程序,其范围为用户给出的区域中的信标。 问题是,当我添加距离约束并返回 mainactivity 时,应用程序也会显示上一个测距实例。
例如:
当我将距离改为5米时,距离的默认值为10米,它仍然显示相距10米的信标。如果我将距离的值更改为2m,我将会显示5m范围的实例和10m范围的实例。
MAINACTIVITY
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.settings) {
try {
beaconManager.stopRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
beaconManager.unbind(this);
Log.w(TAG,"Stopped ranging");
} catch (RemoteException e) {
e.printStackTrace();
}
startActivity(new Intent(MainActivity.this, Settings.class));
}
return true;
}
在oncreate中变量 距离
SharedPreferences sharedPref = getApplication().getSharedPreferences("Settings",Context.MODE_PRIVATE);
distance = sharedPref.getFloat("distance",10.0f);
测距功能
@Override
public void onBeaconServiceConnect() {
//beacon ranging
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
for (Beacon beacon : beacons) {
if(beacon.getDistance()<= distance) {
Log.i(TAG,beacon.getBluetoothName());
Log.i(TAG,String.valueOf(distance));
}
}
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
设置
Button button;
EditText distance;
Float distanceValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
setTitle("Settings");
SharedPreferences sharedPref = getApplication().getSharedPreferences("Settings",Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPref.edit();
button = (Button) findViewById(R.id.button);
distance = (EditText) findViewById(R.id.distanceValue);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(distance.getText().toString().isEmpty()){
distance.requestFocus();
distance.setError("Required");
}
else{
distanceValue = Float.valueOf(distance.getText().toString());
editor.putFloat("distance",distanceValue);
editor.commit();
startActivity(new Intent(Settings.this,MainActivity.class));
}
}
});
LOG
之前的
06-28 14:54:21.724 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00037
06-28 14:54:21.724 13866-18103/com.example.beaconscanner I/MonitoringActivity: 10.0
06-28 14:54:21.732 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00035
06-28 14:54:21.732 13866-18103/com.example.beaconscanner I/MonitoringActivity: 10.0
后的
06-28 14:56:37.778 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00035
06-28 14:56:37.778 13866-18103/com.example.beaconscanner I/MonitoringActivity: 5.0
06-28 14:56:37.788 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00035
06-28 14:56:37.788 13866-18103/com.example.beaconscanner I/MonitoringActivity: 5.0
06-28 14:56:37.801 13866-18103/com.example.beaconscanner I/MonitoringActivity: proxiteeMini_00037
06-28 14:56:37.801 13866-18103/com.example.beaconscanner I/MonitoringActivity: 10.0
proxiteeMini_00037在10米范围内
proxiteeMini_00035在5米范围内
答案 0 :(得分:0)
我怀疑你有多个MainActivity实例同时运行。 MainActivity的旧实例具有旧的距离值,而新的实例具有新值。创建从SettingsActivity返回MainActivity的意图将导致此问题。您可以通过使用this.finish();