質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

833閲覧

androidでボタンを押した後,等間隔で加速度を保存する

km16

総合スコア12

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2022/07/25 12:38

編集2022/07/29 04:04

前提

android studioで加速度センサーの値を端末に保存するアプリを作ろうとしています.
Saveボタンを押す→5秒間加速度を測定&保存→測定終了→Saveボタンを押す→・・・(繰り返し)
という流れを行おうと考えてます.

実現したいこと

Timerクラスのscheduleメソッドを用いて測定&保存の部分を等間隔で行いたい.

発生している問題・エラーメッセージ

Saveボタンを押しても何も起こらない

該当のソースコード

java

1package com.example.accelerationtent_hozon_test; 2 3import androidx.appcompat.app.AppCompatActivity; 4 5import android.app.Activity; 6import android.hardware.Sensor; 7import android.hardware.SensorEvent; 8import android.hardware.SensorEventListener; 9import android.hardware.SensorManager; 10import android.os.Bundle; 11import android.os.Handler; 12import android.view.View; 13import android.widget.Button; 14import android.widget.TextView; 15import android.widget.Toast; 16 17import java.io.IOException; 18import java.io.OutputStream; 19import java.io.OutputStreamWriter; 20import java.io.PrintWriter; 21import java.net.FileNameMap; 22import java.text.SimpleDateFormat; 23import java.util.ArrayList; 24import java.util.Calendar; 25import java.util.Timer; 26import java.util.TimerTask; 27 28public class MainActivity extends AppCompatActivity { 29 30 float x,y,z; //加速度の値を格納する変数 31 32 MyTimerTask timerTask = null; //onClickメソッドでインスタンス生成 33 Timer mTimer = null; //onClickメソッドでインスタンス生成 34 Handler mHandler = new Handler(); //UI Threadへのpost用ハンドラ 35 36 SensorManager sensorManager; //センサーマネージャー 37 Sensor accelSensor; //加速度センサー 38 MySensorEventListener mySensorEventListener; //センサーのイベントリスナー 39 40 TextView textViewX; //Z軸方向の加速度表示 41 TextView textViewY; //Z軸方向の加速度表示 42 TextView textViewZ; //Z軸方向の加速度表示 43 TextView textViewHYOUKI; //保存状態の表記 44 45 Button buttonSave; //測定開始ボタン 46 47 String FileName; //保存時のファイル名(保存開始日時) 48 49 @Override 50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 setContentView(R.layout.activity_main); 53 54 //テキスト,ボタンの登録 55 textViewX = findViewById(R.id.textViewX); 56 textViewY = findViewById(R.id.textViewY); 57 textViewZ = findViewById(R.id.textViewZ); 58 textViewHYOUKI = findViewById(R.id.textViewHYOUKI); 59 60 buttonSave = findViewById(R.id.buttonSave); 61 62 // SensorManagerを取得する 63 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 64 65 // 加速度センサーを取得する 66 accelSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 67 68 if( accelSensor==null ){ //加速度センサ-を取得できなかった場合 69 Toast.makeText(this, "加速度センサーが使えません", Toast.LENGTH_SHORT).show(); 70 } 71 } 72 73 @Override 74 protected void onResume(){ 75 super.onResume(); 76 if( accelSensor!=null ){//加速度センサーが取得できた場合 77 mySensorEventListener = new MySensorEventListener(); 78 sensorManager.registerListener(mySensorEventListener, //センサーの登録 79 accelSensor, 10000); 80 } 81 } 82 //ボタンが押されたときの挙動 83 public class timerActivity extends Activity implements View.OnClickListener { 84 public void onClick(View v) { 85 86 Button btn = (Button) v; 87 88 switch (btn.getId()) { 89 90 //Saveボタンが押されたとき 91 case R.id.buttonSave: 92 93 if (mTimer == null) { 94 //保存時のファイル名の取得(ボタンを押した日時) 95 Calendar cl = Calendar.getInstance(); 96 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); 97 FileName =sdf.format(cl.getTime()); 98 99 //タイマーの初期化処理 100 timerTask = new MainActivity.MyTimerTask(); 101 mTimer = new Timer(true); 102 mTimer.schedule(timerTask, 100, 100); 103 } 104 break; 105 106 default: //それ以外の時 107 break; 108 109 } 110 } 111 } 112 113 //mTimer.scheduleの挙動 114 class MyTimerTask extends TimerTask{ 115 116 @Override 117 public void run() { 118 // mHandlerを通じてUI Threadへ処理をキューイング 119 mHandler.post( new Runnable() { 120 public void run() { 121 textViewX.setText("X: " + (x)); 122 textViewY.setText("Y: " + (y)); 123 textViewZ.setText("Z: " + (z)); 124 125 OutputStream out; 126 127 try { 128 out = openFileOutput(FileName+".csv", MODE_APPEND); 129 PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "UTF-8")); 130 writer.append((x) + (", ") + (y) + (", ") + (z) + ("\n")); //各座標を書き込む 131 writer.close(); 132 } catch (IOException e){ 133 e.printStackTrace(); 134 } 135 } 136 }); 137 } 138 } 139 140 class MySensorEventListener implements SensorEventListener{ 141 142 @Override 143 public void onSensorChanged(SensorEvent sensorEvent) { 144 //センサーの値を取得する 145 x = sensorEvent.values[0]; 146 y = sensorEvent.values[1]; 147 z = sensorEvent.values[2]; 148 } 149 public void onAccuracyChanged(Sensor sensor, int i){ 150 } 151 } 152} 153
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textViewX" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="100dp" android:layout_marginTop="90dp" android:text="X" android:textSize="20sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:ignore="MissingConstraints" /> <TextView android:id="@+id/textViewY" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:text="Y" android:textSize="20sp" app:layout_constraintStart_toStartOf="@+id/textViewX" app:layout_constraintTop_toBottomOf="@+id/textViewX" tools:ignore="MissingConstraints" /> <TextView android:id="@+id/textViewZ" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:text="Z" android:textSize="20sp" app:layout_constraintStart_toStartOf="@+id/textViewY" app:layout_constraintTop_toBottomOf="@+id/textViewY" tools:ignore="MissingConstraints" /> <Button android:id="@+id/buttonSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="160dp" android:layout_marginTop="74dp" android:text="Save" android:textSize="20sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textViewHYOUKI" tools:ignore="MissingConstraints" /> <TextView android:id="@+id/textViewHYOUKI" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="40dp" android:layout_marginTop="216dp" android:textSize="30sp" app:layout_constraintStart_toStartOf="@+id/textViewY" app:layout_constraintTop_toBottomOf="@+id/textViewY" tools:ignore="MissingConstraints" /> </androidx.constraintlayout.widget.ConstraintLayout>

現在5秒間測定の部分は考えずに単純にSaveボタンを押したら保存開始というようなプログラムをかこうとしています.

試したこと

timerActivityクラスやMyTimerTaskクラスの位置が悪いのかと思ったのですがどのように配置すれば良いか分かりませんでした.

補足情報(FW/ツールのバージョンなど)

androidエミュレータのAPI31で実行しました.

参考にしたサイト
加速度の保存の仕方を参考:https://teratail.com/questions/228703
timerクラスの使い方を参考:https://techbooster.org/android/application/934/

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2022/07/25 14:28

質問に付けられている button タグは、 html の <button> のことです。関係無いと思われます。
guest

回答1

0

ベストアンサー

Saveボタンを押しても何も起こらない

Save ボタンにクリックリスナーが登録されていません。

投稿2022/07/25 14:52

jimbe

総合スコア12632

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問