2016-02-06 66 views
0

我使用Sensor.TYPE_STEP_COUNTER我知道它只在重新啓動時重置。按下按鈕時是否有其他方法將步驟重置爲0?我的步驟計數器不會重置步驟,即使在卸載後

請看看我的代碼,你會發現Runactivity.class

也許我能做到這一點,其中復位步驟的另一種方式。 無需我每次重新啓動。

public class RunActivity extends AppCompatActivity implements SensorEventListener{ 

    private SensorManager sensorManager; 
    private TextView count; 
    boolean activityRunning; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_run); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     Bundle bundle = getIntent().getExtras(); 
     final String naam = bundle.getString("naam"); 
     TextView NaamView = null; 

     Button stopRun = (Button) findViewById(R.id.stopRun); 

     count = (TextView) findViewById(R.id.countView); 
     sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 

     NaamView = (TextView) findViewById(R.id.naamRunText); 

     NaamView.setText(naam); 

     stopRun.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       String countValue = count.getText().toString(); 



       Log.d("countVAL", String.valueOf(countValue)); 


       Intent myIntent = new Intent(RunActivity.this, HomeScreenActivity.class); 

       Bundle bundle = new Bundle(); 


       bundle.putString("naam", naam); 
       sensorManager.flush(RunActivity.this); 
       sensorManager.unregisterListener(RunActivity.this); 
       count.setText("0"); 
       onStop(); 

       myIntent.putExtras(bundle); 

       startActivity(myIntent); 


      } 
     }); 

    } 



    @Override 
    protected void onResume() { 
     super.onResume(); 
     activityRunning = true; 
     Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER); 
     if(countSensor != null){ 
      sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI); 

     }else{ 
      Toast.makeText(this, "Jouw apparaat heeft geen sensor!", Toast.LENGTH_LONG) .show(); 
     } 
    } 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     if(activityRunning){ 
      count.setText(String.valueOf(event.values[0])); 
     }else{ 
      event.values[0] = 0; 
     } 



    } 



    @Override 
    protected void onPause() { 
     super.onPause(); 
     sensorManager.unregisterListener(this); 
     activityRunning = false; 
    } 



    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 

    } 

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

    } 
} 
+1

和你的代碼,請.... – Gavriel

+0

@Gavriel我沒有編輯我的問題。 – LifeOS

回答

2

當您單擊該應用程序的復位按鈕保存當前的步數來SharedPreferences復位。而且您需要一種方法來確定最後一次重新啓動的時間,因爲每次重新啓動保存的計數時都會失效。

private int stepsInSensor = 0; 
private int stepsAtReset; 

void onCreate() { 
    SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
    stepsAtReset = prefs.getInt("stepsAtReset", 0); 
} 

    public void onClick(View v) { 
     stepsAtReset = stepsInSensor; 

     SharedPreferences.Editor editor = 
      getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); 
     editor.putInt("stepsAtReset", stepsAtReset); 
     editor.commit(); 

     // you can now display 0: 
     count.setText(String.valueOf(0)); 
    } 

@Override 
public void onSensorChanged(SensorEvent event) { 
    if(activityRunning){ 
     stepsInSensor = Integer.valueOf(event.values[0]); 
     int stepsSinceReset = stepsInSensor - stepsAtReset; 
     count.setText(String.valueOf(stepsSinceReset)); 
    }else{ 
     event.values[0] = 0; 
    } 
} 
+0

你能告訴我一個例子嗎?在我停下來之後,我希望該值爲零。 – LifeOS

+0

我完全像你說的那樣,在我點擊停止按鈕後,sep計數器仍然是一樣的。 – LifeOS

+0

我添加了一行以在onClick中顯示「0」。但是,您是否在點擊重置後嘗試執行一些操作? – Gavriel

0

不,基於這種類型的Sensor API

的傳感器返回的同時激活自上次重新啓動由用戶 所採取的步驟的數量。該值返回爲浮點數 (小數部分設置爲零),在系統重新啓動時僅重置爲零,即 。

它只能是當系統重新啓動