Android プログラムの例
前準備
前準備として,下記のインストールが済んでいること.
- JDK (Java Development Kit)のバージョン1.6.0_2以上と、Android SDK
→ Ubuntu: Ubuntu での Android Studio についての Web ページ
- (オプション) Eclipse, Eclipse の プラグイン Android Development Tools のインストール が終わっていること
- Android 2.2 を、次のような手順でインストールしておくこと
Android SDK マネージャ を起動し、Android 2.2 をインストールする。
◆ 起動手順の例
sudo /usr/local/android-studio/sdk/tools/android
ファイル出力
ディレクトリ情報の取得
package com.example.hoge3.hoge3;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Environment;
import android.util.Log;
import java.io.File;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File file = null;
file = Environment.getExternalStorageDirectory();
Log.v("storage", file.getPath());
file = Environment.getDataDirectory();
Log.v("data", file.getPath());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ファイルの作成
package com.example.hoge3.hoge3;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
FileOutputStream fileOutputStream = openFileOutput("myfile.txt", MODE_WORLD_READABLE);
String writeString = "test";
fileOutputStream.write(writeString.getBytes());
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ファイルの確認
adb shell
run-as com.example.hoge3.hoge3
cd files
adb push myfile.txt .
センサーデータの取得
センサーデータを取得し、Logcat で出力しながら、ファイルに保存するプログラム
【関連する外部ページ】 https://developer.android.com/guide/topics/sensors/index.html
【関連する外部ページ】 https://developer.android.com/reference/android/hardware/SensorEvent.html
【関連する外部ページ】 http://www.adakoda.com/android/000182.html
package com.example.hoge3.hoge3;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import java.util.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends Activity
implements SensorEventListener {
private boolean mRegisteredSensor;
private SensorManager mSensorManager;
private FileOutputStream fs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRegisteredSensor = false;
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
long utc = System.currentTimeMillis();
try {
fs = openFileOutput("myfile" + String.valueOf(utc) + ".txt", MODE_WORLD_READABLE);
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
@Override
protected void onResume() {
super.onResume();
List<Sensor> sensors_accelerometer = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
if (sensors_accelerometer.size() > 0) {
Sensor sensor = sensors_accelerometer.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List<Sensor> sensors_magnetic_field = mSensorManager.getSensorList(Sensor.TYPE_MAGNETIC_FIELD);
if (sensors_magnetic_field.size() > 0) {
Sensor sensor = sensors_magnetic_field.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List<Sensor> sensors_gyroscope = mSensorManager.getSensorList(Sensor.TYPE_GYROSCOPE);
if (sensors_gyroscope.size() > 0) {
Sensor sensor = sensors_gyroscope.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List<Sensor> sensors_light = mSensorManager.getSensorList(Sensor.TYPE_LIGHT);
if (sensors_light.size() > 0) {
Sensor sensor = sensors_light.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List<Sensor> sensors_pressure = mSensorManager.getSensorList(Sensor.TYPE_PRESSURE);
if (sensors_pressure.size() > 0) {
Sensor sensor = sensors_pressure.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List<Sensor> sensors_proximity = mSensorManager.getSensorList(Sensor.TYPE_PROXIMITY);
if (sensors_proximity.size() > 0) {
Sensor sensor = sensors_proximity.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List<Sensor> sensors_gravity = mSensorManager.getSensorList(Sensor.TYPE_GRAVITY);
if (sensors_gravity.size() > 0) {
Sensor sensor = sensors_gravity.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List<Sensor> sensors_linear_acceleration = mSensorManager.getSensorList(Sensor.TYPE_LINEAR_ACCELERATION);
if (sensors_linear_acceleration.size() > 0) {
Sensor sensor = sensors_linear_acceleration.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List<Sensor> sensors_rotation_vector = mSensorManager.getSensorList(Sensor.TYPE_ROTATION_VECTOR);
if (sensors_rotation_vector.size() > 0) {
Sensor sensor = sensors_rotation_vector.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List<Sensor> sensors_orientation = mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
if (sensors_orientation.size() > 0) {
Sensor sensor = sensors_orientation.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List<Sensor> sensors_ambient_temperature = mSensorManager.getSensorList(Sensor.TYPE_AMBIENT_TEMPERATURE);
if (sensors_ambient_temperature.size() > 0) {
Sensor sensor = sensors_ambient_temperature.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List<Sensor> sensors_relative_humidity = mSensorManager.getSensorList(Sensor.TYPE_RELATIVE_HUMIDITY);
if (sensors_relative_humidity.size() > 0) {
Sensor sensor = sensors_relative_humidity.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
}
@Override
protected void onPause() {
if (mRegisteredSensor) {
mSensorManager.unregisterListener(this);
mRegisteredSensor = false;
}
super.onPause();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
/*
metadata:
accelerometer, 1, ax, acceleration minus Gx on the physical x-axis, m/s^2
accelerometer, 2, ay, acceleration minus Gy on the physical y-axis, m/s^2
accelerometer, 3, az, acceleration minus Gz on the physical z-axis, m/s^2
magnetic_field, 1, mx, ambient magnetic field in the physical x-axis, micro-Tesla
magnetic_field, 2, my, ambient magnetic field in the physical y-axis, micro-Tesla
magnetic_field, 3, mz, ambient magnetic field in the physical z-axis, micro-Tesla
gyroscope, 1, rx, angular speed around the physical x-axis, radian/second
gyroscope, 2, ry, angular speed around the physical y-axis, radian/second
gyroscope, 3, rz, angular speed around the physical z-axis, radian/second
light, 1, al, ambient light level, SI lux
pressure, 1, p, atmospheric pressure, hPa (millibar)
proximity, 1, d, proximity of an object relative to the view screen, centimeters
gravity, 1, gx, direction and magnitude of gravity on the physical x-axis, m/s^2
gravity, 2, gy, direction and magnitude of gravity on the physical y-axis, m/s^2
gravity, 3, gz, direction and magnitude of gravity on the physical z-axis, m/s^2
linear_acceleration, 1, lsx, acceleration along the device X-axis, not including gravity, m/s^2
linear_acceleration, 2, lsx, acceleration along the device Y-axis, not including gravity, m/s^2
linear_acceleration, 3, lsx, acceleration along the device Z-axis, not including gravity, m/s^2
rotation_vector, 1, rvx, the orientation of the device as a combination of an angle and the X-axis: x*sin(theta/2), vector element
rotation_vector, 2, rvy, the orientation of the device as a combination of an angle and the Y-axis: y*sin(theta/2). vector element
rotation_vector, 3, rvz, the orientation of the device as a combination of an angle and the Z-axis: z*sin(theta/2), vector element
orientation, 1, ox, degrees of rotation that a device makes around the physical x-axis, degree
orientation, 2, oy, degrees of rotation that a device makes around the physical y-axis, degree
orientation, 3, oz, degrees of rotation that a device makes around the physical z-axis, degree
ambient_temperature, 1, at, ambient (room) temperature, degree Celsius
relation_humidity, 1, rh, the relative ambient humidity, percent
*/
@Override
public void onSensorChanged(SensorEvent event) {
// https://developer.android.com/reference/android/hardware/SensorEvent.html
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
String a = String.valueOf(event.timestamp) + ", " + String.valueOf(event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]);
Log.v("accelerometer", a);
try {
fs.write( ("accelerometer, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
String a = String.valueOf(event.timestamp) + ", " + String.valueOf(event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]);
Log.v("magnetic_field", a);
try {
fs.write( ("magnetic_field, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
String a = String.valueOf(event.timestamp) + ", " + String.valueOf(event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]);
Log.v("gyroscope", a);
try {
fs.write( ("gyroscope, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
String a = String.valueOf(event.timestamp) + ", " + String.valueOf(event.values[0]);
Log.v("light", a);
try {
fs.write( ("light, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
String a = String.valueOf(event.timestamp) + ", " + String.valueOf(event.values[0]);
Log.v("pressure", a);
try {
fs.write( ("pressure, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
String a = String.valueOf(event.timestamp) + ", " + String.valueOf(event.values[0]);
Log.v("proximity", a);
try {
fs.write( ("proximity, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_GRAVITY) {
String a = String.valueOf(event.timestamp) + ", " + String.valueOf(event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]);
Log.v("gravity", a);
try {
fs.write( ("gravity, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
String a = String.valueOf(event.timestamp) + ", " + String.valueOf(event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]);
Log.v("linear_acceleration", a);
try {
fs.write( ("linear_acceleration, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
String a = String.valueOf(event.timestamp) + ", " + String.valueOf(event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]);
Log.v("rotation_vector", a);
try {
fs.write( ("rotation_vector, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
// values[0]:
// Azimuth, angle between the magnetic north direction and the Y axis,
// around the Z axis (0 to 359). 0=North, 90=East, 180=South, 270=West
// values[1]:
// Pitch, rotation around X axis (-180 to 180),
// with positive values when the z-axis moves toward the y-axis.
// values[2]:
// Roll, rotation around Y axis (-90 to 90),
// with positive values when the x-axis moves away from the z-axis.
String a = String.valueOf(event.timestamp) + ", " + String.valueOf(event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]);
Log.v("orientation", a);
try {
fs.write( ("orientation, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
String a = String.valueOf(event.timestamp) + ", " + String.valueOf(event.values[0]);
Log.v("ambient_temperature", a);
try {
fs.write( ("ambient_temperature, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_RELATIVE_HUMIDITY) {
String a = String.valueOf(event.timestamp) + ", " + String.valueOf(event.values[0]);
Log.v("relative_humidity", a);
try {
fs.write( ("relative_humidity, " + a + "\n").getBytes());
} catch(IOException e) {}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
GPS
GPS データを取得し、Logcat で出力
【関連する外部ページ】 https://d.hatena.ne.jp/orangesignal/20101223/1293079002
public class MainActivity extends ActionBarActivity implements LocationListener,GpsStatus.Listener { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new GraphicView(this)); //GPS Initialize this.locationmanager=(LocationManager)getSystemService(LOCATION_SERVICE); this.locationmanager.addGpsStatusListener(this); String provider=locationmanager.getBestProvider(null, true); this.locationmanager.requestLocationUpdates(provider, 0, 0, MainActivity.this); } public void onDestroy(){ super.onDestroy(); if(locationmanager!=null){ locationmanager.removeUpdates(MainActivity.this); locationmanager.removeGpsStatusListener(MainActivity.this); } }
see http://www.adakoda.com/android/000125.html
package com.example.hoge3.hoge3;
package com.example.hoge3.hoge3;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.location.LocationManager;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends Activity implements LocationListener {
private LocationManager mLocationManager;
private FileOutputStream fs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// GPS
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// data file
long utc = System.currentTimeMillis();
try {
fs = openFileOutput("myfile" + String.valueOf(utc) + ".txt", MODE_WORLD_READABLE);
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
@Override
protected void onResume() {
if (mLocationManager != null) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
// LocationManager.NETWORK_PROVIDER,
0,
0,
this);
}
super.onResume();
}
@Override
protected void onPause() {
if (mLocationManager != null) {
mLocationManager.removeUpdates(this);
}
super.onPause();
}
@Override
public void onLocationChanged(Location location) {
Log.v("----------", "----------");
Log.v("Latitude", String.valueOf(location.getLatitude()));
Log.v("Longitude", String.valueOf(location.getLongitude()));
Log.v("Accuracy", String.valueOf(location.getAccuracy()));
Log.v("Altitude", String.valueOf(location.getAltitude()));
Log.v("Time", String.valueOf(location.getTime()));
Log.v("Speed", String.valueOf(location.getSpeed()));
Log.v("Bearing", String.valueOf(location.getBearing()));
String a =
String.valueOf(location.getLatitude()) + ", " +
String.valueOf(location.getLongitude()) + ", " +
String.valueOf(location.getAccuracy()) + ", " +
String.valueOf(location.getAltitude()) + ", " +
String.valueOf(location.getBearing()) + ", " +
String.valueOf(location.getExtras()) + ", " +
String.valueOf(location.getProvider()) + ", " +
String.valueOf(location.getSpeed()) + ", " +
String.valueOf(location.getTime());
try {
fs.write( ("gps, " + a + "\n").getBytes());
} catch(IOException e) {}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
Log.v("Status", "AVAILABLE");
break;
case LocationProvider.OUT_OF_SERVICE:
Log.v("Status", "OUT_OF_SERVICE");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.v("Status", "TEMPORARILY_UNAVAILABLE");
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
AndroidManifest.xml に次の1行を追加
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
センサーデータとGPS データを取得し、Logcat で出力しながら、ファイルに出力
package com.example.hoge3.hoge3;
package com.example.hoge3.hoge3;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import java.util.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.location.LocationManager;
import android.util.Log;
public class MainActivity extends Activity
implements SensorEventListener, LocationListener {
private LocationManager mLocationManager;
private boolean mRegisteredSensor;
private SensorManager mSensorManager;
private FileOutputStream fs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// GPS
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// sensors
mRegisteredSensor = false;
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
// data file
long utc = System.currentTimeMillis();
try {
fs = openFileOutput("myfile" + String.valueOf(utc) + ".txt", MODE_WORLD_READABLE);
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
@Override
protected void onResume() {
// GPS
if (mLocationManager != null) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
// LocationManager.NETWORK_PROVIDER,
0,
0,
this);
}
super.onResume();
// Sensors
List sensors_accelerometer = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
if (sensors_accelerometer.size() > 0) {
Sensor sensor = sensors_accelerometer.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List sensors_magnetic_field = mSensorManager.getSensorList(Sensor.TYPE_MAGNETIC_FIELD);
if (sensors_magnetic_field.size() > 0) {
Sensor sensor = sensors_magnetic_field.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List sensors_gyroscope = mSensorManager.getSensorList(Sensor.TYPE_GYROSCOPE);
if (sensors_gyroscope.size() > 0) {
Sensor sensor = sensors_gyroscope.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List sensors_light = mSensorManager.getSensorList(Sensor.TYPE_LIGHT);
if (sensors_light.size() > 0) {
Sensor sensor = sensors_light.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List sensors_pressure = mSensorManager.getSensorList(Sensor.TYPE_PRESSURE);
if (sensors_pressure.size() > 0) {
Sensor sensor = sensors_pressure.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List sensors_proximity = mSensorManager.getSensorList(Sensor.TYPE_PROXIMITY);
if (sensors_proximity.size() > 0) {
Sensor sensor = sensors_proximity.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List sensors_gravity = mSensorManager.getSensorList(Sensor.TYPE_GRAVITY);
if (sensors_gravity.size() > 0) {
Sensor sensor = sensors_gravity.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List sensors_linear_acceleration = mSensorManager.getSensorList(Sensor.TYPE_LINEAR_ACCELERATION);
if (sensors_linear_acceleration.size() > 0) {
Sensor sensor = sensors_linear_acceleration.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List sensors_rotation_vector = mSensorManager.getSensorList(Sensor.TYPE_ROTATION_VECTOR);
if (sensors_rotation_vector.size() > 0) {
Sensor sensor = sensors_rotation_vector.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List sensors_orientation = mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
if (sensors_orientation.size() > 0) {
Sensor sensor = sensors_orientation.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List sensors_ambient_temperature = mSensorManager.getSensorList(Sensor.TYPE_AMBIENT_TEMPERATURE);
if (sensors_ambient_temperature.size() > 0) {
Sensor sensor = sensors_ambient_temperature.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
List sensors_relative_humidity = mSensorManager.getSensorList(Sensor.TYPE_RELATIVE_HUMIDITY);
if (sensors_relative_humidity.size() > 0) {
Sensor sensor = sensors_relative_humidity.get(0);
mRegisteredSensor = mSensorManager.registerListener(this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST);
mRegisteredSensor = true;
}
}
@Override
protected void onPause() {
if (mLocationManager != null) {
mLocationManager.removeUpdates(this);
}
if (mRegisteredSensor) {
mSensorManager.unregisterListener(this);
mRegisteredSensor = false;
}
super.onPause();
}
@Override
public void onLocationChanged(Location location) {
Log.v("----------", "----------");
Log.v("Latitude", String.valueOf(location.getLatitude()));
Log.v("Longitude", String.valueOf(location.getLongitude()));
Log.v("Accuracy", String.valueOf(location.getAccuracy()));
Log.v("Altitude", String.valueOf(location.getAltitude()));
Log.v("Time", String.valueOf(location.getTime()));
Log.v("Speed", String.valueOf(location.getSpeed()));
Log.v("Bearing", String.valueOf(location.getBearing()));
String a =
String.valueOf(location.getLatitude()) + ", " +
String.valueOf(location.getLongitude()) + ", " +
String.valueOf(location.getAccuracy()) + ", " +
String.valueOf(location.getAltitude()) + ", " +
String.valueOf(location.getBearing()) + ", " +
String.valueOf(location.getExtras()) + ", " +
String.valueOf(location.getProvider()) + ", " +
String.valueOf(location.getSpeed()) + ", " +
String.valueOf(location.getTime());
try {
fs.write( ("gps, " + a + "\n").getBytes());
} catch(IOException e) {}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
Log.v("Status", "AVAILABLE");
break;
case LocationProvider.OUT_OF_SERVICE:
Log.v("Status", "OUT_OF_SERVICE");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.v("Status", "TEMPORARILY_UNAVAILABLE");
break;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
/*
metadata:
accelerometer, 1, ax, acceleration minus Gx on the physical x-axis, m/s^2
accelerometer, 2, ay, acceleration minus Gy on the physical y-axis, m/s^2
accelerometer, 3, az, acceleration minus Gz on the physical z-axis, m/s^2
magnetic_field, 1, mx, ambient magnetic field in the physical x-axis, micro-Tesla
magnetic_field, 2, my, ambient magnetic field in the physical y-axis, micro-Tesla
magnetic_field, 3, mz, ambient magnetic field in the physical z-axis, micro-Tesla
gyroscope, 1, rx, angular speed around the physical x-axis, radian/second
gyroscope, 2, ry, angular speed around the physical y-axis, radian/second
gyroscope, 3, rz, angular speed around the physical z-axis, radian/second
light, 1, al, ambient light level, SI lux
pressure, 1, p, atmospheric pressure, hPa (millibar)
proximity, 1, d, proximity of an object relative to the view screen, centimeters
gravity, 1, gx, direction and magnitude of gravity on the physical x-axis, m/s^2
gravity, 2, gy, direction and magnitude of gravity on the physical y-axis, m/s^2
gravity, 3, gz, direction and magnitude of gravity on the physical z-axis, m/s^2
linear_acceleration, 1, lsx, acceleration along the device X-axis, not including gravity, m/s^2
linear_acceleration, 2, lsx, acceleration along the device Y-axis, not including gravity, m/s^2
linear_acceleration, 3, lsx, acceleration along the device Z-axis, not including gravity, m/s^2
rotation_vector, 1, rvx, the orientation of the device as a combination of an angle and the X-axis: x*sin(theta/2), vector element
rotation_vector, 2, rvy, the orientation of the device as a combination of an angle and the Y-axis: y*sin(theta/2). vector element
rotation_vector, 3, rvz, the orientation of the device as a combination of an angle and the Z-axis: z*sin(theta/2), vector element
orientation, 1, ox, degrees of rotation that a device makes around the physical x-axis, degree
orientation, 2, oy, degrees of rotation that a device makes around the physical y-axis, degree
orientation, 3, oz, degrees of rotation that a device makes around the physical z-axis, degree
ambient_temperature, 1, at, ambient (room) temperature, degree Celsius
relation_humidity, 1, rh, the relative ambient humidity, percent
*/
@Override
public void onSensorChanged(SensorEvent event) {
// https://developer.android.com/reference/android/hardware/SensorEvent.html
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
String a = String.valueOf(event.timestamp + ", " + event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]);
Log.v("accelerometer", a);
try {
fs.write( ("accelerometer, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
String a = String.valueOf(event.timestamp + ", " + event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]);
Log.v("magnetic_field", a);
try {
fs.write( ("magnetic_field, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
String a = String.valueOf(event.timestamp + ", " + event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]);
Log.v("gyroscope", a);
try {
fs.write( ("gyroscope, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
String a = String.valueOf(event.timestamp + ", " + event.values[0]);
Log.v("light", a);
try {
fs.write( ("light, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
String a = String.valueOf(event.timestamp + ", " + event.values[0]);
Log.v("pressure", a);
try {
fs.write( ("pressure, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
String a = String.valueOf(event.timestamp + ", " + event.values[0]);
Log.v("proximity", a);
try {
fs.write( ("proximity, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_GRAVITY) {
String a = String.valueOf(event.timestamp + ", " + event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]);
Log.v("gravity", a);
try {
fs.write( ("gravity, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
String a = String.valueOf(event.timestamp + ", " + event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]);
Log.v("linear_acceleration", a);
try {
fs.write( ("linear_acceleration, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
String a = String.valueOf(event.timestamp + ", " + event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]);
Log.v("rotation_vector", a);
try {
fs.write( ("rotation_vector, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
// values[0]:
// Azimuth, angle between the magnetic north direction and the Y axis,
// around the Z axis (0 to 359). 0=North, 90=East, 180=South, 270=West
// values[1]:
// Pitch, rotation around X axis (-180 to 180),
// with positive values when the z-axis moves toward the y-axis.
// values[2]:
// Roll, rotation around Y axis (-90 to 90),
// with positive values when the x-axis moves away from the z-axis.
String a = String.valueOf(event.timestamp + ", " + event.values[0]) + ", " +
String.valueOf(event.values[1]) + ", " +
String.valueOf(event.values[2]);
Log.v("orientation", a);
try {
fs.write( ("orientation, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
String a = String.valueOf(event.timestamp + ", " + event.values[0]);
Log.v("ambient_temperature", a);
try {
fs.write( ("ambient_temperature, " + a + "\n").getBytes());
} catch(IOException e) {}
}
if (event.sensor.getType() == Sensor.TYPE_RELATIVE_HUMIDITY) {
String a = String.valueOf(event.timestamp + ", " + event.values[0]);
Log.v("relative_humidity", a);
try {
fs.write( ("relative_humidity, " + a + "\n").getBytes());
} catch(IOException e) {}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
グラフィックス
GraphicView.java
表示部分
package com.example.hoge3.hoge3;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.View;
public class GraphicView extends View {
public MainActivity m;
public GraphicView(MainActivity context) {
super(context);
this.m = context;
}
@Override
public void onDraw(Canvas canvas) {
// 座標系がわかるような罫線を引く
Paint paint = new Paint();
paint.setColor(Color.argb(75, 255, 255, 255));
// 円を書く
paint.setAntiAlias(false);
paint.setColor(Color.BLUE);
canvas.drawCircle(m.x, m.y, 40, paint);
// 多角形を書く
paint.setColor(Color.RED);
Path path = new Path();
path.moveTo(100, 300);
path.lineTo(10, 350);
path.lineTo(80, 330);
canvas.drawPath(path, paint);
// 文字を書く
paint.setAntiAlias(false);
paint.setColor(Color.rgb(255, 255, 0));
paint.setTextSize(36);
paint.setAntiAlias(true);
canvas.drawText(m.text, 10, 450, paint);
}
}
MainActivity.java
package com.example.hoge3.app;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
public int x = 120;
public int y = 100;
public String text = "hello";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GraphicView(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}