/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { Log.i(TAG, "[ACTIVITY] onCreate"); super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.run); mStepValue = 0; mPaceValue = 0; mUtils = Utils.getInstance(); handler.postDelayed(runnable, 1000); }
// 重写回复函数 @Override protected void onResume() { Log.i(TAG, "[ACTIVITY] onResume"); super.onResume(); mSettings = PreferenceManager.getDefaultSharedPreferences(this); mPedometerSettings = new PedometerSettings(mSettings); mUtils.setSpeak(mSettings.getBoolean("speak", false)); // Read from preferences if the service was running on the last onPause mIsRunning = mPedometerSettings.isServiceRunning(); // Start the service if this is considered to be an application start (last onPause was long // ago) if (!mIsRunning && mPedometerSettings.isNewStart()) { startStepService(); bindStepService(); } else if (mIsRunning) { bindStepService(); } mPedometerSettings.clearServiceRunning(); mStepValueView = (TextView) findViewById(R.id.step_value); mPaceValueView = (TextView) findViewById(R.id.pace_value); mDistanceValueView = (TextView) findViewById(R.id.distance_value); mSpeedValueView = (TextView) findViewById(R.id.speed_value); mCaloriesValueView = (TextView) findViewById(R.id.calories_value); resetValues(true); pause_btn = (Button) findViewById(R.id.pause_btn); stop_btn = (Button) findViewById(R.id.stop_btn); runtime = (TextView) findViewById(R.id.runtime); pause_btn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View arg0) { if (!isPause) { isPause = true; handler.postDelayed(runnable, 1000); if (!isUnbindService) { unbindStepService(); isUnbindService = true; } pause_btn.setText("继续"); stopStepService(); } else { isPause = false; handler.postDelayed(runnable, 1000); pause_btn.setText("暂停"); startStepService(); bindStepService(); isUnbindService = false; } } }); stop_btn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View arg0) { // resetValues(false); AlertDialog.Builder builder1 = new AlertDialog.Builder(mContext); builder1 .setMessage("确定结束吗?") .setCancelable(false) .setPositiveButton( "确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { if (!isUnbindService) { isUnbindService = true; unbindStepService(); } stopStepService(); mQuitting = true; handler.removeCallbacks(runnable); send_run_record(); // 将本次运动记录发送至后台 try { RunActivity.setMessage(LoginActivity.user_id); } catch (Exception e) { } } }) .setNegativeButton( "取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // 确定结束跑步取消的话,让其相当于点了一次暂停 if (!isPause) { isPause = true; handler.postDelayed(runnable, 1000); if (!isUnbindService) { unbindStepService(); isUnbindService = true; } pause_btn.setText("继续"); stopStepService(); } dialog.cancel(); } }); builder1.setTitle("提示"); builder1.create().show(); } }); }