前言:如今主流的定位方式有两种:GPS定位和网络定位。GPS即通过卫星定位,网络定位则需要通过手机附近的基站,WIFI等确定位置信息。二者的优缺点各有不同,Gps较为精准但是耗电量大,网络定位功耗小但是不精确。这篇文章会以Location Manager为例,讲解如何在Androidstudio中使用这两种定位服务。
一、权限声明
在Manifest.xml中声明定位权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<!--精确定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!--模糊定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!-- 后台定位-->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
.......
</manifest>
并注册LocationService服务
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
......
<service android:name=".LocationService"
android:enabled="true"
android:exported="false"/>
......
</manifest>
二、新建LocationService.java文件
导入以下类
import android.Manifest;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.content.pm.PackageManager;
创建Locationservice
public class LocationService extends Service {
private static final String TAG = "LocationService";
private LocationManager locationManager;
private LocationListener locationListener;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "----------Service created----------");
//实现定位功能
initLocation();
}
private void initLocation() {
//创建locationManager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//创建locationlistener,在获取位置后调用,写入处理信息的逻辑
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();//维度
double longitude = location.getLongitude();//经度
//这里可以写处理数据的逻辑
//打印数据
Log.d(TAG, "Latitude: " + latitude + ", Longitude: " + longitude);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
//判断权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
//开始请求位置信息
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Service destroyed");
//停止请求
if (locationManager != null && locationListener != null) {
locationManager.removeUpdates(locationListener);
}
}
}
在请求位置信息时,传入了参数LocationManager.GPS_PROVIDER。这样在定位时会使用Gps定位。如果想使用网络定位,只需要把参数改为LocationManager.NETWORK_PROVIDER。
三、权限请求和启动Service
在MainActivity中向用户请求定位权限,得到许可后启动LocationService服务
public class MainActivity extends AppCompatActivity {
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestLocationPermissions();
private void requestLocationPermissions() {
//判断权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {
//请求权限
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
} else {
//启动位置监听服务
Intent intent = new Intent(this,LocationService.class);
startService(intent);
}
}
在获取到坐标后可以利用Geocoder类获取具体的地理位置,也可通过广播发送到MainActivity进行加工,这里不过多赘述。
标签:定位,void,AndroidStudio,LOCATION,import,android,public,GPS From: https://blog.csdn.net/m0_72225765/article/details/145241085