Android Implement Compass and Accelerometer ( Step counter ) together in one activity class
<p>I developed a compass and a step counter application separately. I need to add both of them together in one application one activity class. Even I add the coding of them the accelerometer step counter doesn't work accurately. I think that the problem is with the onResum or onPause methods. Becouse I added the both codings in same onResume method. Anyone have a suggestion or guidance how to make this accurate?</p>

<p>Below is my coding that I have tried.</p>

<pre><code>    public class MapView extends Activity implements SensorEventListener  {

    private SensorManager mSensorManager;

    //For compass-------------------------

    private Sensor mCompass;
    private TextView DegreeTextView;
    private TextView DirectionTextView;
    static String direction;

    //For step counter--------------------

    private float mLastX, mLastY, mLastZ;
    private boolean mInitialized;
    private Sensor mAccelerometer;
    private final float NOISE = (float) 1.0;
    private int scount=0;
    //-------------------------------------

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);      

      //For compass--------------------
     mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mCompass = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        DegreeTextView = (TextView) findViewById(R.id.Degree);
        DirectionTextView = (TextView) findViewById(R.id.Direction);
      //--------------------------------------

        //For step counter--------------------
       mInitialized = false;
       mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensorManager.registerListener(this, mAccelerometer , SensorManager.SENSOR_DELAY_NORMAL);
        //--------------------------------------

    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onSensorChanged(SensorEvent event) {
// For compass----------------------------
         int degreepoint = Math.round(event.values[0]);
            char tmp = 0x00B0;



            if(degreepoint&lt;360 &amp;&amp; degreepoint&gt;=337 ){   direction="N";    }
            if(degreepoint&lt;22 &amp;&amp; degreepoint&gt;=0 ){  direction="N";    }    
            if(degreepoint&lt;67 &amp;&amp; degreepoint&gt;=22 ){     direction="NE";    }    
            if(degreepoint&lt;102 &amp;&amp; degreepoint&gt;=67 ){    direction="E";    }
            if(degreepoint&lt;157 &amp;&amp; degreepoint&gt;=102 ){   direction="SE";    }    
            if(degreepoint&lt;202 &amp;&amp; degreepoint&gt;=157 ){   direction="S";    }    
            if(degreepoint&lt;247 &amp;&amp; degreepoint&gt;=202 ){   direction="SW";    }    
            if(degreepoint&lt;292 &amp;&amp; degreepoint&gt;=247 ){   direction="W";    }    
            if(degreepoint&lt;337 &amp;&amp; degreepoint&gt;=292 ){   direction="NW";    }
            // The other values provided are: 
            //  float pitch = event.values[1];
            //  float roll = event.values[2];
            DegreeTextView.setText(""+degreepoint+tmp+"");
            DirectionTextView.setText(""+direction+"");

            //-----------------------------------------------------

            //For step counter-------------------------------------

            TextView tvX= (TextView)findViewById(R.id.textViewX);
            TextView tvY= (TextView)findViewById(R.id.textViewY);
            TextView tvZ= (TextView)findViewById(R.id.textViewZ);
            TextView stepS= (TextView)findViewById(R.id.textViewS);
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];
            if (!mInitialized) {
                mLastX = x;
                mLastY = y;
                mLastZ = z;
                tvX.setText("0.0");
                tvY.setText("0.0");
                tvZ.setText("0.0");
                mInitialized = true;
            } else {
                float deltaX = Math.abs(mLastX - x);
                float deltaY = Math.abs(mLastY - y);
                float deltaZ = Math.abs(mLastZ - z);
                if (deltaX &lt; NOISE) deltaX = (float)0.0;
                if (deltaY &lt; NOISE) deltaY = (float)0.0;
                if (deltaZ &lt; NOISE) deltaZ = (float)0.0;
                mLastX = x;
                mLastY = y;
                mLastZ = z;
                tvX.setText(Float.toString(deltaX));
                tvY.setText(Float.toString(deltaY));
                tvZ.setText(Float.toString(deltaZ));

                float g = (mLastX * mLastX + mLastY * mLastY + mLastZ * mLastZ) / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);

                if(g&gt;=2.0){
                    scount=scount+1;
                    }

                stepS.setText(Integer.toString(scount));
            }


            //-----------------------------------------------------

    }
    @Override
    protected void onPause() {
        // Unregister the listener on the onPause() event to preserve battery life;
        super.onPause();
        mSensorManager.unregisterListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mCompass, SensorManager.SENSOR_DELAY_NORMAL);

        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }
}
</code></pre>
