0

I'm trying to make a timeline fragment for my app that shows the number of steps taken each day, I display the step count in a fragment,

  1. I'm having trouble in resetting the value after every 24 hours.
  2. I've declared my step counting variable as a static variable which resets when app is closed but I have declared a service class which is where my step counting logic happens.
  3. Each day's steps will be inserted into a sqlite database and displayed in another fragment inside a listview.

public class HomeFragment extends Fragment {

TextView tv_steps; View myView; private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { int stepCountValue = intent.getExtras().getInt("StepCount"); tv_steps.setText(String.valueOf(stepCountValue)); // Toast.makeText(context, stepCountValue + "", Toast.LENGTH_SHORT).show(); } }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { myView = inflater.inflate(R.layout.home_layout, container, false); RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.home_layout, container, false); tv_steps = (TextView) rl.findViewById(R.id.tv_steps); return rl; } @Override public void onResume() { super.onResume(); getActivity().registerReceiver(receiver, new IntentFilter("stepcountvalue")); } @Override public void onPause() { super.onPause(); //getActivity().unregisterReceiver(receiver); } 

}

My service class for step counting logic:

public class StepCounterService extends Service implements SensorEventListener { SensorManager sensorManager; static int initialStepCount = 0; public static int stepCount = 0; SQLiteDatabase database; private int numberOfStepsToBeInserted; @Override public void onCreate() { super.onCreate(); sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE); database = openOrCreateDatabase("Steps", MODE_PRIVATE, null); try { database.execSQL("CREATE TABLE IF NOT EXISTS steps (date TEXT, steps_count INT(10))"); } catch (Exception e) { e.printStackTrace(); } final Handler handler = new Handler(); Runnable run = new Runnable() { @Override public void run() { //numberOfStepsToBeInserted = stepCount; //stepCount = stepCount - numberOfStepsToBeInserted; //stepCount = 0; String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); database.execSQL("INSERT INTO steps(date, steps_count) VALUES('" + date + "', '" + stepCount + "')"); Cursor c = database.rawQuery("SELECT * FROM steps", null); int dateIndex = c.getColumnIndex("date"); int stepIndex = c.getColumnIndex("steps_count"); c.moveToFirst(); if (c.getCount() >= 1) { while (c.moveToNext()) { Log.d("----------------->", c.getString(dateIndex) + " " + c.getInt(stepIndex)); } } else { Log.i("Check empty", "Cursor empty"); } handler.postDelayed(this, 86400000); } }; handler.post(run); } @Override public void onSensorChanged(SensorEvent event) { if (initialStepCount == 0) { initialStepCount = (int) event.values[0]; } stepCount = (int) event.values[0] - initialStepCount; Intent sendStepCount = new Intent("stepcountvalue"); sendStepCount.putExtra("StepCount", stepCount - numberOfStepsToBeInserted); sendBroadcast(sendStepCount); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER); if (countSensor != null) { sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI); } else { } return START_STICKY; } @Override public void onDestroy() { } } 

1 Answer 1

0

It might be easier for you to accomplish this using the Google Fit API. You'll have to look through it a bit to figure out exactly which part of it you need to use, but it allows much more functionality and will be a lot better for performance than running a background service the entire time. Also, with the addition of Doze and App Standby in Android 6.0+, your service might be stopped when the screen is turned off, making the resulting data inaccurate and unreliable.

If you do not wish to use the Google Fit API, then I would recommend two things to fix your problems:

As for the third problem (didn't really look like a problem, but I'm assuming it is one since it was listed with two other problems before it), the question is a little too generic to answer without going into a lot of detail:

Each day's steps will be inserted into a sqlite database and displayed in another fragment inside a listview.

I would recommend finding a tutorial of some sort to help you with this instead of asking it on Stack Overflow, such as this one, for example.

Sign up to request clarification or add additional context in comments.

1 Comment

Strange, it seems that the links for AlarmManager.set() and PendingIntent.getBroadcast() aren't formatted correctly. They appear fine in the preview while editing, but don't show up properly once the edits are saved... sorry about that :P

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.