Hello everyone,
Today I would like to show you how to use the LocationManager classes to collect the exact location of your mobile during the execution of your application. As part of the content, I would like to talk a little bit about sensors as well. Please note: there is one video published on youtube that is related of this article that I wrote, find below more details below:
Summary:
The Use LocationManager object using getSystemService(Context.LOCATION_SERVICE), then instantiate LocationListenet , and this listener needs to override some methods, such as OnLocationEnableb, OnStatusChanged or OnProviderEnable and Disable.
How to get Location Updates:
1-Call requestLocationUpdates(provider, minTime,minDistance, Listener)
2-Call removeUpdates(Listener) to stop getting Location.
It works the same way that you call some other methods, as for example the Hardware Sensors. But a very important information here is:
Any service or activity that will ask for a listener on sensor data should implement the SensorEventListener interface.
The java class SensorEventListener has two required methods:
onAccuracyChanged() -> when accuracy of the sensor changes
onSensorChanged() -> when sensor reports new values.
So, when the sensor values change on mobile system, the values array(in the event that goes using onSensorChanged method) will change.
The Event Values:
The value here is an array of values returned from the sensor.
Each type of sensor has a different array of values, such as X,y,z axis(math).TYPE_proximity has only one element in its values array(eg: values[0]).
Warning: Preserve the battery! When you are dealing with location, the hardware that works for sensor is highly consumed, per user action, delays parameters
and others.
Example of delays parameters:
SENSOR_DELAY_NORMAL: sampling rate for screen orientation changes.
SENSOR_DELAY_UI: For monitoring changes that will be applied to the user(interface).
SENSOR_DELAY_FASTEST: a zero microsecond delay.
SENSOR_DELAY_GAME: for monitoring changes for a use in a typical game.
Saying that, let's go to the main coding of this article:
1) Creating your android project you will see the 3 main files we are going to work with:
LocationThiagoLeoncioActivity.java, activity_location_thiago_leoncio.xml and AndroidManifest.xml
1.1) On activity_location_thiago_leoncio.xml I will drag and drop all TextViews that I am going to use as a display.
1.2)On LocationThiagoLeoncioActivity.java we will write our methods that are going to be displayed(called) by the main onCreate method.
In this case is calling a method setupLocation() that is provided below, and the embedded method showLocationofThiago():
1.3) Last but not least the AndroidManifest.xml that allows the mobile emulator user to get the location:
2) The way that I developed when you run your emulator and launch your Android Device Monitor sending the location it will show you something similar to this below:
To help you better understand this, please watch this video that I've published: https://youtu.be/9Q0w71dUsys
I hope it helps you,
Thiago Leoncio.
Thiago - Tech Director, Principal Architect and software developer for many different languages. I am passionate about the development of any kind. I am a developer since 1996, coding for many different styles such as Cold Fusion, Java, PHP, PL-SQL, Objective-C, Nodejs and many others..anything needed we can translate to code.
Saturday, May 16, 2015
Sunday, April 26, 2015
Android Coding: How to start coding for Android
Hello everyone,
Today I am going to show on simple steps how to start coding using Android app.
1) First of all you need to download the Android studio that is basically the Eclipse( modified for it) at https://developer.android.com/sdk/installing/index.html?pkg=studio
2) Run the file android-studio-bundle-141.1980579-windows.exe
3) Install the SDK from Oracle to allow you use java.sh or .exe
I hope it helps you.I am going to provide more articles related of mobile development next month.
Happy coding. Please send me an e-mail if you want this sample code.
Cheers,
Thiago Leoncio.
Today I am going to show on simple steps how to start coding using Android app.
1) First of all you need to download the Android studio that is basically the Eclipse( modified for it) at https://developer.android.com/sdk/installing/index.html?pkg=studio
2) Run the file android-studio-bundle-141.1980579-windows.exe
3) Install the SDK from Oracle to allow you use java.sh or .exe
4) Choose components that you are going to use.
5) Define where you are going to install them
6) This is a very important step: You are going to define how many GB are you allowing for Android Emulator. Please note that this can affect your machine performance depending on the size of your project.
Installation completes successfully
7) Running Android Studio
8) Next step is create your own Android Java Project
Now that we have our project created, and you can start modifying it ,creating some interactions for your user.
9) On this example we are going to create one regular button that calls a textView to be displayed.
So, here it is our project screen:
10)First of all, we are going to create our button called button, and out text to be displayed called textView1. Once they are created into Android display, you should be able to see them on our main activity.xml(called activity_fullscreen_thiago_leoncio_blog.xml) as below:
11) This step now we are going to create the action on the java class called FullscreenThiagoLeoncioBlogActivity.java
//Please note that you should import the TextView from android.widget.TextView.
12) Now we should return to our main xml activity file called activity_fullscreen_thiago_leoncio_blog.xml, and make sure the button we added has the onClick action related properly added.
Now you can save your code and launch your Android emulator clicking on the green arrow (run button).
Choosing the type of device you want to emulate.
then click
...and if you click on this button.....
On TOP the message that we wrote on onBtnTouch method
I hope it helps you.I am going to provide more articles related of mobile development next month.
Happy coding. Please send me an e-mail if you want this sample code.
Cheers,
Thiago Leoncio.
Subscribe to:
Posts (Atom)