Easy Tutorial
❮ Verilog Fsm Javascript Lightbox ❯

10.14 An Introduction to Android GPS

Category Android Basic Tutorial

Introduction to This Section:

When it comes to the term GPS, everyone should be familiar with it, right? GPS stands for Global Positioning System technology. In Android, there are generally four ways to locate:

This series of tutorials only covers the basic usage of GPS positioning! GPS acquires the current latitude and longitude of the device by interacting with satellites, which is quite accurate, but it also has some drawbacks. The biggest drawback is that it is almost unusable indoors ... It requires signals from at least four satellites to ensure accurate positioning with GPS! However, if you are outdoors and without a network, GPS can still be used!

In this section, we will explore the basic usage of GPS in Android~


1. Some APIs Related to Location


1) LocationManager

Official API documentation: LocationManager

This is a system service and cannot be directly instantiated with new. It requires:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

Also, don't forget to add permissions for GPS positioning:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Once you have obtained the LocationManager object, you can call the following common methods:


2) LocationProvider (Location Provider)

Official API documentation: LocationProvider

This is an abstract representation of the GPS positioning component. You can call the following methods to obtain information about the positioning component!

Common methods are as follows:


3) Location (Location Information)

Official API documentation: Location

An abstract class for location information, you can call the following methods to obtain relevant location information!

Common methods are as follows:


3. Determine if GPS is turned on and two ways to turn on GPS

The first thing we should do before using GPS positioning is to determine whether the GPS is already turned on or available. If it's not turned on, we need to turn on the GPS to complete the positioning! AGPS is not considered here~


1) Determine if GPS is available

private boolean isGpsAble(LocationManager lm){
    return lm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER) ? true : false;
}

2) If GPS is not turned on, turn on GPS

Method one: Force to turn on GPS, useless after Android 5.0...

// Force to turn on GPS for users before 5.0
private void openGPS(Context context){
    Intent gpsIntent = new Intent();
    gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
    gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
    gpsIntent.setData(Uri.parse("custom:3"));
    try {
        PendingIntent.getBroadcast(LocationActivity.this, 0, gpsIntent, 0).send();
    } catch (PendingIntent.CanceledException e) {
        e.printStackTrace();
    }
}

Method two: Open the GPS location information settings page and let the user turn it on themselves

// Open the location settings page for the user to set it up themselves
private void openGPS2(){
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivityForResult(intent, 0);
}

4. Dynamically obtain location information

This is very simple, just call the requestLocationUpdates method to set a LocationListener to periodically detect the location!

Example code is as follows:

Layout: activity_location.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp"
        android:textSize="20sp"
        android:textStyle="bold" />

</LinearLayout>

LocationActivity.java:

/**
 * Created by Jay on 2015/11/20 0020.
 */
public class LocationActivity extends AppCompatActivity {

    private LocationManager lm;
    private TextView tv_show;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        tv_show = (TextView) findViewById(R.id.tv_show);
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (
updateShow(lm.getLastKnownLocation(provider));
            }

            @Override
            public void onProviderDisabled(String provider) {
                updateShow(null);
            }
        });
    }


    //Define a method to update the display
    private void updateShow(Location location) {
        if (location != null) {
            StringBuilder sb = new StringBuilder();
            sb.append("Current location information:\n");
            sb.append("Longitude: " + location.getLongitude() + "\n");
            sb.append("Latitude: " + location.getLatitude() + "\n");
            sb.append("Altitude: " + location.getAltitude() + "\n");
            sb.append("Speed: " + location.getSpeed() + "\n");
            sb.append("Bearing: " + location.getBearing() + "\n");
            sb.append("Accuracy: " + location.getAccuracy() + "\n");
            tv_show.setText(sb.toString());
        } else tv_show.setText("");
    }


    private boolean isGpsAble(LocationManager lm) {
        return lm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER) ? true : false;
    }


    //Open the settings page for the user to set up themselves
    private void openGPS2() {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivityForResult(intent, 0);
    }

}

Well, it's very simple. Since GPS needs to be used outdoors, I took the opportunity to run out to the convenience store and buy a cup of milk tea, and took a screenshot on the way~

requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)

When the time exceeds minTime (unit: milliseconds), or the position moves more than minDistance (unit: meters), the method in the listener will be called to update the GPS information. It is recommended that this minTime is not less than 60000, that is, 1 minute, which will be more efficient and power-saving. If you need to update the GPS as close to real-time as possible, you can set minTime and minDistance to 0.

By the way, don't forget, you also need a permission:

&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

5. Proximity Alert (Geo-fencing)

Well, it's about fixing a point, and when the distance between the phone and that point is less than the specified range, the corresponding processing can be triggered! It's a bit like a geo-fence... We can call the LocationManager's addProximityAlert method to add a proximity alert! The complete method is as follows:

addProximityAlert (double latitude, double longitude, float radius, long expiration, PendingIntent intent)

Property description:

-latitude : The longitude of the specified fixed point

-longitude : The latitude of the specified fixed point

-radius : The specified radius length

-expiration : The specified proximity alert will expire after this many milliseconds, -1 means never expire

-intent : This parameter specifies the component corresponding to the intent that is triggered when approaching the fixed point

Example code is as follows :

ProximityActivity.java :

/**
 * Created by Jay on 2015/11/21 0021.
 */
public class ProximityActivity extends AppCompatActivity {
    private LocationManager lm;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_proximity);
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //Define the fixed point's longitude and latitude
        double longitude = 113.56843;
        double latitude = 22.374937;
        float radius = 10;     //Define the radius in meters
        Intent intent = new Intent(this, ProximityReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(this, -1, intent, 0);
        lm.addProximityAlert(latitude, longitude, radius, -1, pi);
    }
}

You also need to register a broadcast receiver: ProximityReceiver.java :

/**
 * Created by Jay on 2015/11/21 0021.
 */
public class ProximityReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean isEnter = intent.getBooleanExtra( LocationManager.KEY_PROXIMITY_ENTERING, false);
        if(isEnter) Toast.makeText(context, "You have arrived near the South Soft B1 building", Toast.LENGTH_LONG).show();
        else Toast.makeText(context, "You have left the vicinity of the South Soft B1 building", Toast.LENGTH_LONG).show();
    }
}

Don't forget to register:

<receiver android:name=".ProximityReceiver"/>

Running effect picture :

PS: Well, I set

❮ Verilog Fsm Javascript Lightbox ❯