Android Open Source - steppy Main Activity






From Project

Back to project page steppy.

License

The source code is released under:

GNU General Public License

If you think the Android project steppy listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.daverix.steppy;
//from  www . ja v  a 2s  . com
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity implements OnStepsCountedListener {
    private SteppyInteractor mInteractor;
    private TextView mStepsTaken;

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

        mStepsTaken = (TextView) findViewById(R.id.textStepsTaken);

        final Intent intent = new Intent(this, SteppyService.class);
        bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        unbindService(mServiceConnection);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if(mInteractor != null) {
            mStepsTaken.setText(String.valueOf(mInteractor.getCountedSteps()));
            mInteractor.registerOnStepsCountedListener(this);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        if(mInteractor != null) {
            mInteractor.unregisterOnStepsCountedListener(this);
        }
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            final SteppyService.SteppyBinder binder = (SteppyService.SteppyBinder) service;
            mInteractor = binder.getSteppyInteractor();
            mInteractor.registerOnStepsCountedListener(MainActivity.this);
            invalidateOptionsMenu();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mInteractor = null;
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem item = menu.findItem(R.id.menuStart);
        if(item == null)
            throw new IllegalStateException("menu item not found");


        if(mInteractor != null) {
            item.setVisible(true);
            if(mInteractor.isListening()) {
                item.setIcon(android.R.drawable.ic_media_play);
                item.setTitle(R.string.menu_start);
            } else {
                item.setIcon(android.R.drawable.ic_lock_power_off);
                item.setTitle(R.string.menu_stop);
            }
        }
        else {
            item.setVisible(false);
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menuStart:
                if(mInteractor != null) {
                    if(mInteractor.isListening()) {
                        mInteractor.startListening();
                    }
                    else {
                        mInteractor.startListening();
                    }
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onStepsCounted(SteppyInteractor listener) {
        mStepsTaken.setText(String.valueOf(listener.getCountedSteps()));
    }
}




Java Source Code List

net.daverix.steppy.MainActivity.java
net.daverix.steppy.OnStepsCountedListener.java
net.daverix.steppy.SteppyInteractor.java
net.daverix.steppy.SteppyService.java