Created
December 28, 2012 08:12
-
-
Save eWizardII/4395787 to your computer and use it in GitHub Desktop.
Android ACC + GPS file
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 com.example.tigerblood; | |
import com.parse.LogInCallback; | |
import com.parse.Parse; | |
import com.parse.ParseAnonymousUtils; | |
import com.parse.ParseException; | |
import com.parse.ParseUser; | |
import android.hardware.Sensor; | |
import android.hardware.SensorEvent; | |
import android.hardware.SensorEventListener; | |
import android.hardware.SensorManager; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class MainActivity extends Activity implements SensorEventListener { | |
Button btnShowLocation; | |
// gpstracker class | |
gpstracker gps; | |
private TextView userid; | |
private SensorManager sensorManager; | |
double ax, ay, az; // these are the acceleration in x,y and z axis | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Calls the EULA Script | |
new SimpleEula(this).show(); | |
btnShowLocation = (Button) findViewById(R.id.btnShowLocation); | |
userid = (TextView) findViewById(R.id.textView2); | |
// Create Information for PARSE | |
Parse.initialize(this, "null", | |
"null"); | |
// Generate ANON user | |
ParseAnonymousUtils.logIn(new LogInCallback() { | |
@Override | |
public void done(ParseUser user, ParseException e) { | |
if (e != null) { | |
Log.d("Node", "Anonymous login failed."); | |
user = new ParseUser(); | |
user.setUsername("null user"); | |
user.setPassword("null pass"); | |
} else { | |
Log.d("Node", "Anonymous user logged in."); | |
} | |
} | |
}); | |
// Set up generic sensor managers | |
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); | |
sensorManager.registerListener(this, | |
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), | |
SensorManager.SENSOR_DELAY_NORMAL); | |
// Continuously grab data | |
final class MyLocationListener implements LocationListener { | |
@Override | |
public void onLocationChanged(Location locFromGps) { | |
// called when the listener is notified with a location update | |
// from the GPS | |
gps = new gpstracker(MainActivity.this); | |
// check if GPS enabled | |
if (gps.canGetLocation()) { | |
gps.getLocation(); | |
// Toast.makeText( | |
// getApplicationContext(), | |
// "Your Location is - \nLat: " + gps.latitude | |
// + "\nLong: " + gps.longitude, | |
// Toast.LENGTH_LONG).show(); | |
Toast.makeText( | |
getApplicationContext(), | |
"Sensor Readings - \nLat: " + gps.latitude | |
+ "\nLong: " + gps.longitude + "\nax: " + ax + "\nay: " + ay + "\naz: " + az, | |
Toast.LENGTH_LONG).show(); | |
// data_string containing all the sensor data | |
String data_string = gps.latitude + "," + gps.longitude | |
+ "\n"; | |
// byte[] data = data_string.getBytes(); | |
// ParseFile file = new ParseFile("data.txt", data); | |
} else { | |
// can't get location | |
gps.showSettingsAlert(); | |
} | |
} | |
public void onSensorChanged(SensorEvent event) { | |
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { | |
ax = event.values[0]; | |
ay = event.values[1]; | |
az = event.values[2]; | |
Toast.makeText( | |
getApplicationContext(), | |
"Sensor Readings - \nLat: " + gps.latitude | |
+ "\nLong: " + gps.longitude + "\nax: " + ax + "\nay: " + ay + "\naz: " + az, | |
Toast.LENGTH_LONG).show(); | |
} | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
// called when the GPS provider is turned off (user turning off | |
// the GPS on the phone) | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
// called when the GPS provider is turned on (user turning on | |
// the GPS on the phone) | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, | |
Bundle extras) { | |
// called when the status of the GPS provider changes | |
} | |
} | |
LocationListener locationListener = new MyLocationListener(); | |
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); | |
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, | |
locationListener); | |
// show location button click event | |
btnShowLocation.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View arg0) { | |
// Lock the user choices and display user name | |
// String currentUser = user.getUsername(); | |
// userid.setText(currentUser); | |
btnShowLocation.setEnabled(false); | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.activity_main, menu); | |
return true; | |
} | |
@Override | |
public void onAccuracyChanged(Sensor sensor, int accuracy) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onSensorChanged(SensorEvent event) { | |
// TODO Auto-generated method stub | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment