简体   繁体   中英

Using Sensor value in hashmap

I am new to Android. I am creating an application using magnetic sensor data. I am following source code of another program which is based on Wifi. My basic program is

 public class MainActivity extends AppCompatActivity implements SensorEventListener{ Sensor magnetometer; SensorManager sm; public float a,b,c; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sm = (SensorManager) getSystemService(SENSOR_SERVICE); sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL); } @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { getMag(event); } } public static void getMag(SensorEvent event) { float[] values = event.values; float x = values[0]; float y = values[1]; float z = values[2]; float value = (float)Math.sqrt(Math.pow(x,2) +Math.pow(y,2) + Math.pow(z,2)); } @Override protected void onStop() { super.onStop(); finish(); } @Override protected void onPause() { super.onPause(); sm.unregisterListener(this); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } } 

I can access the sensor value with this but what i want is to use sensor value in other class. In source code, which i am following, the code is used as.

 //this is map activity, here for wifi, it just uses "getScanResult() to scan bssid and value, which can be used in onReciveWifiScanResult public void onStart() { super.onStart(); mReceiver = new BroadcastReceiver () { @Override public void onReceive(Context c, Intent intent) { onReceiveWifiScanResults(mWifi.getScanResults()); } }; } public void onReceiveWifiScanResults(List<ScanResult> results) { } //And the programmer used it in another class to store to the hashmap public void onReceiveMagScanResults(List<ScanResult> results) { HashMap<String, Integer> measurements = new HashMap<String, Integer>(); for (ScanResult result : results) { measurements.put(result.BSSID, result.level); } TreeSet<String> keys = new TreeSet<String>(); keys.addAll(measurements.keySet()); keys.addAll(mMeasurements.keySet()); 

So, How can i get my magnetic sensor data from "onSensorChanged" event of my Main activity to use like in above broadcast Receiver in place of "onReceiveWifiscanresult"?

Any help would be great. Thank you.

Oh, I found the answer. Now, i can store data in SQLite database and then use cursor to access the data. The data is then put in hashmap.

I used this code

  TextView diff= (TextView) findViewById(R.id.calcDiff); SQLiteDatabase db; db= openOrCreateDatabase( "Mag_Positioning.db" , SQLiteDatabase.CREATE_IF_NECESSARY , null ); db.setVersion(1); db.setLocale(Locale.getDefault()); db.setLockingEnabled(true); Cursor cur = DBHelper.getInstance().getAllData(); cur.moveToFirst(); HashMap<PointF, Float> difference = new HashMap<>(); if (cur.isLast() == false) { do{ cur1=cur.getInt(2); cur2=cur.getInt(3); PointF location = new PointF(cur1, cur2); z= Float.valueOf(cur.getString(7)); total = Float.valueOf(String.valueOf(Math.sqrt(Math.pow((dz),2)))); difference.put(location, total); diff.append("\\n" + difference +"\\n"); }while(cur.moveToNext()); } cur.close(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM