前提・実現したいこと
加速度を取得しcsvファイルに保存するandroidアプリを製作しています。加速度をファイルに保存したいタイミングでstartボタンを押し、やめたい時にstopボタンを押すというのが基本仕様です。
このボタンを押すという動作を.performClick()で実行したいのですが上手く動作しません。
具体的には、start、stopボタンのどちらかだけを.performClick()で実行するとそのボタンは動作します。またその場合は人力でボタンを押しても動作します。しかし、両方のボタンを.performClick()で実行しようとすると上手く動作しません。
検証し分かったことは、.performClick()自体は問題なく動作しボタンも自動で押されています。原因はonClick()ないに記述されているTimerSet()だと思われますが、原因がつかめません。お力添えをお願い致します。
発生している問題・エラーメッセージ
なし
該当のソースコード
java
public void onClick (View v){ if (v == startButton) { stopRun = false; Calendar calendar = Calendar.getInstance(); fileNameAccelesX = "AccelsX" + calendar.getTime() + ".csv"; fileNameAccelesY = "AccelsY" + calendar.getTime() + ".csv"; fileNameAccelesZ = "AccelsZ" + calendar.getTime() + ".csv"; TimerSet(); } else if(v == stopButton){ stopRun = true; stopTimerTask(); } } public void onSensorChanged(SensorEvent event) { if(event.sensor.getType() == Sensor.TYPE_LIGHT) { light = event.values[0]; if(event.values[0] <= 70){ stopButton.performClick(); }else{ startButton.performClick(); } } } public void TimerSet() { runnable = new Runnable() { public void run() { // 数値を小数点以下2桁までの文字列に変換するためのもの NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); // 加速度センサーから取得した値をTextViewにセットする textViewAccel.setText("端末にかかっている重力加速度を含む加速度"); textViewX.setText("X : " + numberFormat.format(accels[0])); textViewY.setText("Y : " + numberFormat.format(accels[1])); textViewZ.setText("Z : " + numberFormat.format(accels[2])); saveFile(fileNameAccelesX , numberFormat.format(accels[0])); saveFile(fileNameAccelesX, ","); saveFile(fileNameAccelesY, numberFormat.format(accels[1])); saveFile(fileNameAccelesY, ","); saveFile(fileNameAccelesZ, numberFormat.format(accels[2])); saveFile(fileNameAccelesZ, ","); handler.postDelayed(this, period); } }; handler.post(runnable); } public void stopTimerTask(){ handler.removeCallbacks(runnable); } }

回答2件
あなたの回答
tips
プレビュー