Created
November 15, 2012 20:53
-
-
Save markuswustenberg/4081196 to your computer and use it in GitHub Desktop.
Examples for the presentation "A Practical Introduction to Sensing with Android" in the Context-Aware Computing course, Department of Computer Science, Aarhus University, 2013
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dk.au.cs.ubi.cac.one; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.hardware.Sensor; | |
import android.hardware.SensorEvent; | |
import android.hardware.SensorEventListener; | |
import android.hardware.SensorManager; | |
import android.os.Bundle; | |
import android.util.Log; | |
public class AccelerometerActivity extends Activity { | |
private static final String TAG = AccelerometerActivity.class.getSimpleName(); | |
private SensorManager sensorManager; | |
private final SensorEventListener accelerometerSensorEventListener = new AccelerometerSensorEventListener(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_accelerometer); | |
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); | |
} | |
@Override | |
protected void onResume() { | |
Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | |
if (!sensorManager.registerListener(accelerometerSensorEventListener, accelerometer, SensorManager.SENSOR_DELAY_FASTEST)) { | |
Log.w(TAG, "Couldn't register accelerometer."); | |
} | |
} | |
@Override | |
protected void onPause() { | |
sensorManager.unregisterListener(accelerometerSensorEventListener); | |
} | |
private static final class AccelerometerSensorEventListener implements SensorEventListener { | |
@Override | |
public void onAccuracyChanged(Sensor sensor, int accuracy) { | |
// Do something when the accuracy has changed | |
} | |
@Override | |
public void onSensorChanged(SensorEvent event) { | |
// Do something with your event | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dk.au.cs.ubi.cac.one; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
public class GpsActivity extends Activity { | |
private LocationManager locationManager; | |
private final LocationListener gpsLocationListener = new GpsLocationListener(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_gps); | |
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); | |
} | |
@Override | |
protected void onResume() { | |
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener); | |
} | |
@Override | |
protected void onPause() { | |
locationManager.removeUpdates(gpsLocationListener); | |
} | |
private static final class GpsLocationListener implements LocationListener { | |
@Override | |
public void onLocationChanged(Location location) { | |
// Do something with the received location object | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
// Handle that the provider got disabled | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
// Handle that the provider got enabled | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
// Handle the status change | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dk.au.cs.ubi.cac.one; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import android.app.Activity; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.net.wifi.ScanResult; | |
import android.net.wifi.WifiManager; | |
import android.net.wifi.WifiManager.WifiLock; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.util.Log; | |
public class WifiActivity extends Activity { | |
private static final String TAG = WifiActivity.class.getSimpleName(); | |
private static final long WIFI_SCAN_DELAY_MILLIS = TimeUnit.SECONDS.toMillis(1); | |
private WifiManager wifiManager; | |
private WifiScanBroadcastReceiver wifiScanBroadcastReceiver = new WifiScanBroadcastReceiver(); | |
private WifiLock wifiLock; | |
private volatile boolean running; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_wifi); | |
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); | |
wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, WifiActivity.class.getName()); | |
} | |
@Override | |
protected void onResume() { | |
running = true; | |
wifiLock.acquire(); | |
registerReceiver(wifiScanBroadcastReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); | |
final Handler wifiScanHandler = new Handler(); | |
Runnable wifiScanRunnable = new Runnable() { | |
@Override | |
public void run() { | |
if (!running) { | |
return; | |
} | |
if (!wifiManager.startScan()) { | |
Log.w(TAG, "Couldn't start Wi-fi scan!"); | |
} | |
wifiScanHandler.postDelayed(this, WIFI_SCAN_DELAY_MILLIS); | |
} | |
}; | |
wifiScanHandler.post(wifiScanRunnable); | |
} | |
@Override | |
protected void onPause() { | |
running = false; | |
unregisterReceiver(wifiScanBroadcastReceiver); | |
wifiLock.release(); | |
} | |
private final class WifiScanBroadcastReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
if (!running || !WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())) { | |
return; | |
} | |
List<ScanResult> scanResults = wifiManager.getScanResults(); | |
// Do something with your scanResults | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment