Chris Pollett >
Students > [Bio] [Blog] [Deliverable 2: Image Feature Selection] [Deliverable 3: Accelerometer and Video Recording Apps] |
Get Familiar with Android Studio and Test Water with Real-time GPS AppDescription: Since this thesis project collects and uses the raw data with users' cellphones, it calls for an App that is able to record the actual or analogous information on steering wheel movements and real time traffic condition, just to name a few. The first step to take toward this goal is to code and test some App's to realize some required functions separately before integrating them into a finalized App. The App platform chosen here is Android Studio due to the fact that dominating amount of worldwide cellphones are powered by Android OS. The first test App to get familiar with Android Studio is GPS App. The source code was written with Android API 28, the latest version as of 2019. App uses LocationManager class and LocationListerner interface to obtain GNSS coordinates. LocationListerner requires the implementation of onLocationChanged(), onStatusChanged(), onProviderDisabled(), and onProviderEnabled() methods. onLocationChanged() is where the latitudes and longitude are obtained. Finally, permission to access fine and course locations is provided in manifest. How to run the code: Two ways: emulator or android smartphone tethering. I mainly used emulator and double checked with tethering. Both work well albeit slower implementation on emulator. For emulator, choose any virtual cellphone that supports Android API 28 and click RUN on Android Studio. Emulator provides the GNSS coordinates: click on ... button and GPS information is listed on the top of left-handed bar. For cellphone tethering, a Huawei MateSE is used for testing. To debug and run the code on the cellphone, two steps are required: 1)activating developer mode on the cellphone. Follow Setting > About phone > Build Number > click seven times, and you are in developer mode. To allow for debugging and testing, follow Setting > System > Developer Mode > Debugging > select USB debugging; 2) choose the tethering mode in Android Studio by following RUN > Edit Configurations... > Delopyment Target Options > USB Device. Source code: MainActivity.java package com.example.gpss; import android.content.Intent; import android.location.LocationListener; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.location.Location; import android.location.LocationManager; import android.util.Log; import android.location.LocationProvider; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements LocationListener { private LocationManager mlocationManager; private TextView mLatitude, mLongitude; private Location location; public void onCreate(Bundle savedInstanceState) { try{ super.onCreate(savedInstanceState); mlocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); setContentView(R.layout.activity_main); mLatitude = (TextView) findViewById(R.id.mLatitude); mLongitude = (TextView) findViewById(R.id.mLongitude); location = mlocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } catch(SecurityException se){ se.getStackTrace(); } } protected void onResume() { /* * Add LocationListener and request updates every 1000ms or 10m * Notice in this case we are saying this Activity implements LocationListener * requestLocationUpdates asks explicit permission or throw Security Exception */ try { mlocationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 1000, 10f, this); super.onResume(); } catch(SecurityException se){ se.getStackTrace(); } } protected void onPause() { mlocationManager.removeUpdates(this); super.onPause(); } public void onProviderDisabled(String provider) { Log.v("GPS", "Disabled"); Intent intent = new Intent( android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } public void onProviderEnabled(String provider) { Log.v("GPS", "Enabled"); } public void onStatusChanged(String provider, int status, Bundle extras) { switch (status) { case LocationProvider.OUT_OF_SERVICE: Log.v("GPS", "Status Changed: Out of Service"); break; case LocationProvider.TEMPORARILY_UNAVAILABLE: Log.v("GPS", "Status Changed: Temporarily Unavailable"); break; case LocationProvider.AVAILABLE: Log.v("GPS", "Status Changed: Available"); break; } } public void onLocationChanged(Location location){ Log.v("Current Latitude:" , ""+location.getLatitude()); Log.v("Current Longitude:" , ""+location.getLongitude()); mLatitude.setText(Double.toString(location.getLatitude())); mLongitude.setText(Double.toString(location.getLongitude())); } } Emulator Screenshot: Reference: |