大家好,又见面了,我是你们的朋友全栈君。
LocationManager
在Android中可以根据LocationManager来获取设备所在的地理信息
根据需求可以将定位的代码移动到所需的地方或者可以稍加改动获取城市的信息
MainActivity 中:
package com.example.myapplicationpp;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.io.IOException;
import java.util.List;
public class MainActivity extends Activity {
private LocationManager locationManager;
private String locationProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得LocationManager引用
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//获取手机中开启的位置提供器
List<String> providers = locationManager.getProviders(true);
//选择获取方式
if (providers.contains(LocationManager.GPS_PROVIDER)) {
locationProvider = LocationManager.GPS_PROVIDER;
} else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
locationProvider = LocationManager.NETWORK_PROVIDER;
} else {
Toast.makeText(this,"定位失败",Toast.LENGTH_SHORT).show();
}
//执行运行时权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
}
//周期性的位置更新,每隔3000ms更新位置,第三个参数监听位置的变化距离,单位米
locationManager.requestLocationUpdates(locationProvider, 3000, 1, locationListener);
}
//位置监听器
LocationListener locationListener = new LocationListener() {
//状态发生变化时是使用
@Override
public void onStatusChanged(String provider, int status, Bundle arg2) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
//信息更新时使用
@Override
public void onLocationChanged(Location location) {
//获取精度
double latitude = location.getLatitude();
//获取纬度
double longitude = location.getLongitude();
String addString = null;
List<Address> addList = null;
// Geocoder经纬度解码者可用于将经纬度转为具体位置信息
Geocoder ge = new Geocoder(MainActivity.this);
try {
//通过经纬度获取地址值,由于地址可能有几个,这块限制为一个
addList = ge.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addList != null && addList.size() > 0) {
for (int i = 0; i < addList.size(); i++) {
Address ad = addList.get(i);
addString = ad.getLocality();//拿到城市
}
}
if(addString != null) {
Toast.makeText(MainActivity.this,addString,Toast.LENGTH_SHORT).show();
}
String locationStr = "维度:" + location.getLatitude()
+ "经度:" + location.getLongitude();
Log.i("andly", locationStr + "----" + addString);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if (locationManager != null) {
//关闭时程序时,移除监听器
locationManager.removeUpdates(locationListener);
}
}
}
AndroidManifest:
申请权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
当在真机上运行时,弹出定位错误信息,则可能是因为没有打开定位权限,需要自己手动打开,运行成功弹出你所在的城市的位置。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/125469.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...