Note that there are some explanatory texts on larger screens.

plurals
  1. POSensor Reading keeps increasing even after closing app, Android
    primarykey
    data
    text
    <p>I have make this app which calculates Linear Acceleration of device of all dimension, combines it, and calculates net acceleration. Top five acceleration values are stored in Shared Preference key-value pairs.</p> <p>The problem is when I close the app and reopen it, I find the values increased to a much higher level. I don't seem to catch the point in the code from where it is happening. </p> <p>Please Help me out.</p> <p>ShakoMeter.java (this is my main activity where all sensor data is collected and stored)</p> <pre><code>package com.example.shake_o_meter; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Collections; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class ShakeOMeter extends Activity implements SensorEventListener{ float ro(double ax2) { DecimalFormat df = new DecimalFormat("#.###"); return Float.valueOf(df.format(ax2)); } SensorManager smngr; Sensor sen; TextView t,t3; double max=-1; double ax = 0,ay = 0,az = 0; SharedPreferences sharedPref; SharedPreferences.Editor editor; ArrayList&lt;Float&gt; list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shake_ometer); t = (TextView)findViewById(R.id.TextView1); // Showing initial message TextView textview = new TextView(getApplicationContext()); textview.setText("Get ready to Shake It!!"); textview.setBackgroundColor(Color.BLACK); textview.setTextColor(Color.WHITE); textview.setTextSize(30); textview.setPadding(10,10,10,10); textview.setGravity(Gravity.CENTER_VERTICAL); Toast toast = new Toast(getApplicationContext()); toast.setView(textview); toast.setDuration(Toast.LENGTH_SHORT); toast.setGravity(Gravity.FILL, 0, 0); toast.show(); //Getting Sensor Control smngr = (SensorManager) getSystemService(Context.SENSOR_SERVICE); sen = (Sensor) smngr.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION); //Obtaining SharedPreferance for storing key-value pairs of high scores. sharedPref = PreferenceManager.getDefaultSharedPreferences(this); editor = sharedPref.edit(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.shake_ometer, menu); return true; } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public void onSensorChanged(SensorEvent event) { list = new ArrayList&lt;Float&gt;(); ax = event.values[0]; ay = event.values[1]; az = event.values[2]; double temp=Math.sqrt(ro(ax)*ro(ax)+ro(ay)*ro(ay)+ro(az)*ro(az)); if(temp&gt;=max){ max=temp; t.setText(ro(max)+""); list.add(sharedPref.getFloat("1", 0)); list.add(sharedPref.getFloat("2", 0)); list.add(sharedPref.getFloat("3", 0)); list.add(sharedPref.getFloat("4", 0)); list.add(sharedPref.getFloat("5", 0)); if(max&gt;list.get(0)){ list.remove(0); list.add((float) max); Collections.sort(list); editor.putFloat("1", list.get(0)); editor.putFloat("2", list.get(1)); editor.putFloat("3", list.get(2)); editor.putFloat("4", list.get(3)); editor.putFloat("5", list.get(4)); editor.commit(); System.out.println(sharedPref.getFloat("5", 0)+" " +sharedPref.getFloat("4", 0)+" " +sharedPref.getFloat("3", 0)+" " +sharedPref.getFloat("2", 0)+" " +sharedPref.getFloat("1", 0)); } } } public void onClickReset(View view){ Intent intent = getIntent(); finish(); startActivity(intent); } public void onClickTopFive(View view){ Intent intent = new Intent(getApplicationContext(),TopFive.class); startActivity(intent); } @Override protected void onResume(){ super.onResume(); final ShakeOMeter sh = this; final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { // Do something after 5s = 5000ms smngr.registerListener(sh, sen, SensorManager.SENSOR_DELAY_NORMAL); } },3000); } @Override protected void onPause() { super.onPause(); smngr.unregisterListener(this); } protected void onStop(){ super.onStop(); smngr.unregisterListener(this); } public void onDestroy(){ super.onDestroy(); smngr.unregisterListener(this); finish(); } } </code></pre> <p>TopFive.java (This is a sub Activity where top five activities are displayed)</p> <pre><code>package com.example.shake_o_meter; import java.text.DecimalFormat; import android.annotation.TargetApi; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.NavUtils; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; public class TopFive extends Activity { float ro(double ax2) { DecimalFormat df = new DecimalFormat("#.###"); return Float.valueOf(df.format(ax2)); } SharedPreferences sharedPref; SharedPreferences.Editor edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_top_five); // Show the Up button in the action bar. setupActionBar(); TextView t1 = (TextView) findViewById(R.id.T1); TextView t2 = (TextView) findViewById(R.id.T2); TextView t3 = (TextView) findViewById(R.id.T3); TextView t4 = (TextView) findViewById(R.id.T4); TextView t5 = (TextView) findViewById(R.id.T5); sharedPref = PreferenceManager.getDefaultSharedPreferences(this); edit = sharedPref.edit(); t1.setText(ro(sharedPref.getFloat("1", 0))+""); t2.setText(ro(sharedPref.getFloat("2", 0))+""); t3.setText(ro(sharedPref.getFloat("3", 0))+""); t4.setText(ro(sharedPref.getFloat("4", 0))+""); t5.setText(ro(sharedPref.getFloat("5", 0))+""); } public void onClickReset(View view){ edit.putFloat("1", 0);edit.commit(); edit.putFloat("2", 0);edit.commit(); edit.putFloat("3", 0);edit.commit(); edit.putFloat("4", 0);edit.commit(); edit.putFloat("5", 0);edit.commit(); Intent intent = getIntent(); finish(); startActivity(intent); } /** * Set up the {@link android.app.ActionBar}, if the API is available. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void setupActionBar() { if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB) { getActionBar().setDisplayHomeAsUpEnabled(true); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.top_five, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // This ID represents the Home or Up button. In the case of this // activity, the Up button is shown. Use NavUtils to allow users // to navigate up one level in the application structure. For // more details, see the Navigation pattern on Android Design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload