From now on, I will make an Android application for studying. Since there is a function to utilize the life log, I tried to find out how to easily acquire location information. Since the actual application acquires location information for a long time, it is necessary to set up and execute Service. We will investigate Service next time.
There are two types of permissions, which can be divided into GPS acquisition and Network acquisition.
AndroidManifest.xml
<!--Obtained from GPS-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!--Obtained from Network-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
GPS: Highly accurate location information can be obtained, but power consumption is high. Cannot be used indoors. Network: Location information is less accurate, but consumes less power. Can be obtained indoors
The general features are as described above. It is necessary to use it properly according to the purpose of the application.
MainActivity.java
package yong.testproject;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity implements LocationListener {
private LocationManager mLocationManager;
private String bestProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initLocationManager();
}
@Override
protected void onStart() {
super.onStart();
locationStart();
}
@Override
protected void onStop() {
super.onStop();
locationStop();
}
private void initLocationManager() {
//Instance generation
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//Advanced Setting
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
criteria.setSpeedRequired(false);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
bestProvider = mLocationManager.getBestProvider(criteria, true);
}
private void checkPermission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//Get permission permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1000);
}
}
private void locationStart() {
checkPermission();
mLocationManager.requestLocationUpdates(bestProvider, 60000, 3, this);
}
private void locationStop() {
mLocationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
Log.d("DEBUG", "called onLocationChanged");
Log.d("DEBUG", "lat : " + location.getLatitude());
Log.d("DEBUG", "lon : " + location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("DEBUG", "called onStatusChanged");
switch (status) {
case LocationProvider.AVAILABLE:
Log.d("DEBUG", "AVAILABLE");
break;
case LocationProvider.OUT_OF_SERVICE:
Log.d("DEBUG", "OUT_OF_SERVICE");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.d("DEBUG", "TEMPORARILY_UNAVAILABLE");
break;
default:
Log.d("DEBUG", "DEFAULT");
break;
}
}
@Override
public void onProviderDisabled(String provider) {
Log.d("DEBUG", "called onProviderDisabled");
}
@Override
public void onProviderEnabled(String provider) {
Log.d("DEBUG", "called onProviderEnabled");
}
}
LocationManager Create an instance of the LocationManager class, set the provider, and get the location information. Acquisition start is LocationManager.requestLocationUpdates, To stop the acquisition, use LocationManager.removeUpdates.
Criteria Make detailed settings for the provider. ・ Criteria.setSpeedRequired (false) → Location Manager speed calculation function. do not use. ・ Criteria.setAltitudeRequired (false) → Advanced information. do not use. ・ Criteria.setBearingRequired (false) → Direction information pointed to by the terminal. do not use. ・ Criteria.setCostAllowed (true) → Improve accuracy by exchanging data with the base station. It is called Cost Allowed because data communication increases due to exchange. If you want to use it, you may want to implement it by asking the user for consent.
LocationListener Set the listener to receive location information. If the location information is changed, the callback method onLocationChanged is called. LocationManager.request You can set the minimum interval to call with the argument set when starting acquisition with LocationUpdates. This time, minTime is set to 60000 (ms) and minDistance is set to 3 (m).
Regarding the detailed settings of Criteria, I will investigate more while actually making the application in the future. I would like to be able to make flexible settings according to the purpose of the application, such as power consumption and accuracy of location information.
I referred to the following site. GPS (location information acquisition) on Android [Android] Create an app that acquires location information with GPS [Technology for Accurately Tracking Location Information in Android](https://medium.com/location-tracking-tech/%E4%BD%8D%E7%BD%AE%E6%83%85%E5%A0% B1% E3% 82% 92% E6% AD% A3% E7% A2% BA% E3% 81% AB% E3% 83% 88% E3% 83% A9% E3% 83% 83% E3% 82% AD% E3% 83% B3% E3% 82% B0% E3% 81% 99% E3% 82% 8B% E6% 8A% 80% E8% A1% 93-in-android-% E7% AC% AC1% E5% 9B % 9E-% E3% 83% 90% E3% 83% 83% E3% 82% AF% E3% 82% B0% E3% 83% A9% E3% 83% B3% E3% 83% 89% E3% 81% A7% E3% 81% AE% E4% BD% 8D% E7% BD% AE% E6% 83% 85% E5% A0% B1% E3% 83% 88% E3% 83% A9% E3% 83% 83% E3% 82% AD% E3% 83% B3% E3% 82% B0% E3% 82% 92% E5% 8F% AF% E8% 83% BD% E3% 81% AB% E3% 81% 99% E3% 82% 8B% E3% 82% A2% E3% 83% BC% E3% 82% AD% E3% 83% 86% E3% 82% AF% E3% 83% 81% E3% 83% A3-6bb36559a977)
Thank you very much.
Recommended Posts