


How Android uses GPS to obtain the user's geographical location and monitor location changes
The example in this article describes how Android uses GPS to obtain the user's geographical location and monitor location changes. Share it with everyone for your reference, the details are as follows:
LocationActivity.java
/* LocationActivity.java * @author octobershiner * 2011 7 22 * SE.HIT * 一个演示定位用户的位置并且监听位置变化的代码 * */ package uni.location; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.Vibrator; import android.util.Log; import android.widget.TextView; public class LocationActivity extends Activity { /** Called when the activity is first created. */ //创建lcoationManager对象 private LocationManager manager; private static final String TAG = "LOCATION DEMO"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取系统的服务, manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //第一次获得设备的位置 updateLocation(location); //重要函数,监听数据测试 manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10, locationListener); } /*此处更新一下,当activity不处于活动状态时取消GPS的监听*/ public void onPause(){ super.onPause(); locationManager.removeListener(locationListener); } //创建一个事件监听器 private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateLocation(location); } public void onProviderDisabled(String provider){ updateLocation(null); Log.i(TAG, "Provider now is disabled.."); } public void onProviderEnabled(String provider){ Log.i(TAG, "Provider now is enabled.."); } public void onStatusChanged(String provider, int status,Bundle extras){ } }; //获取用户位置的函数,利用Log显示 private void updateLocation(Location location) { String latLng; if (location != null) { double lat = location.getLatitude(); double lng = location.getLongitude(); latLng = "Latitude:" + lat + " Longitude:" + lng; } else { latLng = "Can't access your location"; } Log.i(TAG, "The location has changed.."); Log.i(TAG, "Your Location:" +latLng); } }
It is not enough to just modify the activity file. Because of the security of the android system, an authorization mechanism is used for services, so it is necessary Modify the manifest.xml file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="uni.location" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".LocationActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> </manifest>
Many friends may have questions, that is, the debugging problem of GPS positioning on the android virtual machine. In fact, it can be simulated. You start the virtual machine, and then open the DDMS interface. Left The side device column will dynamically display the startup status of various services on the virtual machine. After the virtual machine is now unlocked, go to the emulator line under the stand-alone device column. At this time, you will find that there is a location control under the emulator control below. Open it. manual tag, haha I believe you have seen the latitude and longitude, you can change it. Run your program, and then click the send button with the latitude and longitude you just set to simulate receiving the new geographical location.
In this demo, I used Log to display the status. It is recommended to use this method. It is very useful. Friends who want to know more can refer to my other article. To learn how to use Log skillfully, I recommend you to search for sundyzlh’s instructional videos.
Regarding the use of LOG, please refer to the previous article "Detailed explanation of Android programming based on Log to demonstrate an activity life cycle instance"
The final effect is as shown in the figure below:
I hope this article will be helpful to everyone in Android programming.
For more related articles on how Android uses GPS to obtain the user's geographical location and monitor location changes, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.
