簡體   English   中英

在哈希圖中使用傳感器值

[英]Using Sensor value in hashmap

我是Android新手。 我正在使用磁傳感器數據創建一個應用程序。 我正在跟蹤另一個基於Wifi的程序的源代碼。 我的基本程序是

 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) { } } 

我可以以此訪問傳感器值,但我想在其他類中使用傳感器值。 在我遵循的源代碼中,該代碼用作。

 //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()); 

因此,如何從我的主要活動的“ onSensorChanged”事件中獲取我的磁傳感器數據,以像上面的廣播Receiver中那樣使用它來代替“ onReceiveWifiscanresult”?

任何幫助都會很棒。 謝謝。

哦,我找到了答案。 現在,我可以將數據存儲在SQLite數據庫中,然后使用游標訪問數據。 然后將數據放入哈希圖中。

我用了這段代碼

  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(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM