Android Resources

  • Official Android beginners tutorial - link
  • How to use background services that continue running after your app closes - link
  • Android Activity Lifecycle explanation - link
  • Android C++ NDK explanation - link
  • Android Sensors Overview - link
  • Java cheatsheet - link

Examples

To write CSV files for later use, such as using Python to analyze data collected on the tablet, use the following as a template:

// NOTE: Requires the following permission in the Android manifest
// 

private final String csvFilenameBase = "sensor_data";
private FileWriter writer;  

private boolean logCsvSetup() {
    try {
        SimpleDateFormat s = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);
        String timestamp = s.format(new Date());
        String csvFilename = csvFilenameBase + "_" + timestamp + ".csv";
        File pathBase = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        File path = new File(pathBase.toString() + "/SensorData");
        if(!path.exists())
        {
            path.mkdir();
        }

        File file = new File(path, csvFilename);
        file.createNewFile();
        writer = new FileWriter(file);

        Toast toast = Toast.makeText(mContext, "CSV file created successfully!", Toast.LENGTH_SHORT);
        toast.show();
    } catch (Exception e) {
        Log.e("ERROR", e.toString());
        Toast toast = Toast.makeText(mContext, "CSV file could not be created", Toast.LENGTH_SHORT);
        toast.show();
        return false;
    }

    return true;
}

private boolean writeCsv(String string) {
    String newLine = String.valueOf(System.currentTimeMillis()) + ", ";
    newLine += string;
    newLine += "\n";

    try {
        writer.write(newLine);
        writer.flush();
    } catch (Exception e) {
        Toast toast = Toast.makeText(mContext, "Could not write to CSV file", Toast.LENGTH_SHORT);
        toast.show();
    }

}

To read files, this StackOverflow answer should be sufficient. It is very similar to the code in logCsvSetup().

The following code is taken directly from the lecture on March 27, 2017, and shows how to interact with buttons and textboxes.

public class MainActivity extends AppCompatActivity {

    private int counter;
    private Button counterButton;
    private TextView counterTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        counterButton = (Button) findViewById(R.id.buttonCounter);
        counterTextView = (TextView) findViewById(R.id.counterTextView);
        counter = 0;

        counterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                counter++;

                String newText = Integer.toString(counter);
                counterTextView.setText(newText);

                Log.d("ButtonTag", newText);
            }
        });
    }
}

The following is the corresponding XML layout for the code above.



    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="com.ece420.ece420_march_talk.MainActivity">

    
        android:id="@+id/counterTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello friends!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    
        android:id="@+id/buttonCounter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:text="Increment Counter"
        app:layout_constraintBottom_toTopOf="@+id/counterTextView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>