- 0. Introduction
- 1. Project Schema
- 2. Install and set up Google Play Services
- 3. Add Google Play Services for your Android Application Project
- 4. Configure the Strings and Layout files
- 5. Carriots Project and Device Registration
- 6. Constructing the Android Code
- 7. Checking Data is collected in Carriots
This easy tutorial describes how to send a stream to Carriots which includes latitude and longitude coordinates from an Android phone. This tutorial assumes that you have already successfully set up an Android development environment in Eclipse. If you have not, please go to How to send a stream with your Android phone before continuing. In this project you will learn how to:
- Set up your development environment to work with Google Maps
- Set up a project in the Google Developers Console
- Send streams with GPS coordinates to Carriots with your Android application
- Your favorite IDE, in our case Eclipse
- A Google account to use with the Google Developers Console
- An Android phone or emulator
Music to listen: Jay Z | Alicia Keys - Empire State of Mind
This project is a quick way to create an Android application that sends GPS streams to Carriots using Android Code.
Here is the overall diagram of the project:
Using Eclipse, go to
- Window -> Android SDK Manager
- In the folder Extras select Google Play services and click on the Install button
- When the installation finishes, the Google Play services folder will be located in your ADT Bundle SDK folder inside sdk -> extras -> google -> google_play_services -> libproject -> google_play-services_lib
- SDK Manager
Now you need to import the file into your Eclipse workspace, to do this go to
- File -> Import
- Then choose Android -> Existing Android Code
- Click Browse and natigate to the google_play-services_lib location as indicated above. Check the Copy projects into workspace checkbox and click Finish
- Now you should see a google_play-services_lib folder in the Package Eplorer in the left-hand margin. Right click over the folder and click on Properties. Click on Android and check the Is Library checkbox as shown below
- Add Google Play-Services Library
In the Package Explorer find the folder of the project you made in the previous How to send a stream with your Android phone tutorial or create a new project. Once created, right click over the project and go to
- Properties -> Android
- Check the Google APIs checkbox under Target Name
- Click the Add button to the right and select the google_play-services_lib folder as seen in the picture below
- Add Google Play-Services Library
Enable Google Maps on your application
Now you need to enable Google Maps on your application. To do this you must go to the Google Developers Console, if you don't already have a google account you will need to create one.
- In Project Name choose the name you want for your project
- Under the Projects tab you should click the Create Project button
Click on the project and using the menu on the left-hand side select
- APIs & auth -> APIs
- Scroll down to Google Maps Android API v2 and by clicking on the OFF button change the status from "OFF" to "ON"
- Google APIs
Use the left-hand menu again and click on
- Credentials -> Create new Key -> Android key
- Create New Key
Now you need to find your SHA1 fingerprint. To do so open Eclipse and go to
- Window -> Preferences -> Android -> Build
- Put this number in the Google Developers Console box followed by a semicolon and the package name of your application (see picture below)
- Click Create and then copy the API key to use in the Android Manifest in the next step
- SHA1 Credentials
Configure the file AndroidManifest.xml
- Type the following permissions above the uses-feature tag
- Between the close of the activityand aplication tags add the following. Note that you must use the API key from the "Enable Google Maps" step
- Within the activity tag add the following. This will prevent the application from stopping if there is a screen rotation while finding the location or sending streams
- Your AndroidManifest.xml code should look something like this
Add the xml Strings
- Click on the project -> res -> values -> strings.xml
- strings.xml Location
- Add the the following code into your strings.xml file after the other strings and before the closing resource tag
Skip the next step if you are working from the How to send a stream with your Android phone tutorial
- Click on the project -> res -> layout -> your xml file
- XML Layout Location
- Add the the following code into your xml layout file
If youre registered in Carriots, you have a default device already created for you.
Check your control panel and see what it looks like.
Basically you need the device id_developer that might be something like defaultDevice@myusername.
But if you want, you can create a new device and use it in this example.
Now, go to your control panel “My account menu” and check your Apikey.
Its a big alphanumeric token like:
You need this number to complete the example.
It's time to add the code to our application that will find the device location and send the coordinates to Carriots
- Go to src -> com.example.yourPackage -> yourMain.java and make the following changes:
- Add these imports under the package name
- Add these variable definitions after public class MyActivity extends Activity
After the onCreate method, add these classes (if you are working from the previous tutorial you should delete the classes)
- The sendStream method will act as an on click listener for the send button and it will call the connect method. The connect method establishes the connection with Google Play Services. If Play Services are not available it will produce a warning message. Otherwise, it will establish the update rules and instantiate a location listener. It also instantiates the FindLocation Async Task.
- The MyLocationListener class gets the current latitude and longitude every time the location is updated (every second) or when the location changes. It also tests the accuracy of the location. If the accuracy is below 100 meters is changes the "accurateLocation" variable to true.
- The FindLocation Async Task opens a thread that shows a progress dialog while the program is getting an accurate location. It waits 15 seconds or until it has found a location, then it checks the accuracy of that location, allowing time to look for a more accurate one. It then instatiates the sendStreamTask class.
- The sendStreamTask class is a helper that will allow you to perform the http request in the background, and after, use the information on the User Interface thread.
- Your Android code should look something like this
- Run on a device (if you use an emulator it must be one compatible with Google Maps v2), and push the button to send a stream to Carriots
- For help go here to Using Hardware Devices
- Android App
If you get here, your device must be sending streams when you push the button.
It's time to test our control panel and check if we have new streams.
Go to “Data management” → “Data streams” and you will see your data.
- Data Stream List