在实际应用开发中,有时候需要简单获取自身的位置,例如创建用户账号时,应用程序可以通过当前位置判断,用户身在哪个国家,由此来判断需要显示给用户什么语言界面,用户需要什么物品等。
package com.example.study.testthread;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import java.util.List;
import static android.content.ContentValues.TAG;
class LocationUtils {
private volatile static LocationUtils uniqueInstance;
private LocationManager locationManager;
private String locationProvider;
private Location location;
private Context mContext;
private LocationUtils(Context context) {
mContext = context;
getLocation();
}
//DLC Single Model
public static LocationUtils getInstance(Context context) {
if (uniqueInstance == null) {
synchronized (LocationUtils.class) {
if (uniqueInstance == null) {
uniqueInstance = new LocationUtils(context);
}
}
}
return uniqueInstance;
}
@SuppressLint("MissingPermission")
private void getLocation() {
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
//Get a location provider, GPS, or NetWork
List<String> providers = locationManager.getProviders(true);
if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
// NetWork Location
Log.d(TAG, "NetWork Location");
locationProvider = LocationManager.NETWORK_PROVIDER;
} else if (providers.contains(LocationManager.GPS_PROVIDER)) {
//GPS Location
Log.d(TAG, "GPS Location");
locationProvider = LocationManager.GPS_PROVIDER;
} else {
Log.d(TAG, "No locator is available");
return;
}
if (Build.VERSION.SDK_INT >= 23
&& ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(mContext,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
@SuppressLint("MissingPermission") Location location = locationManager.getLastKnownLocation(locationProvider);
if (location != null) {
setLocation(location);
}
//Monitor geographic location changes
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
}
private void setLocation(Location location) {
this.location = location;
String address = "precision:" + location.getLatitude() + "longitude:" + location.getLongitude();
Log.d(TAG, address);
}
//Get latitude and longitude
public Location showLocation() {
return location;
}
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) {
location.getAccuracy();//precision
setLocation(location);
}
};
}
开启定位需要权限申请,在android 6.0以下,只需要在AndroidManifest.xml文件下声明以下代码就可以了:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
而在android 6.0以上的系统,不仅仅要在AndroidManifest.xml声明,更需要去申请动态权限,也就是需要增加提示框告诉用户,你需要定位权限,并得到用户允许才可以使用
动态权限检查,当没有这个权限时,提示用户需要获取:
//check permissions
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_COARSE_LOCATION!= PackageManager.PERMISSION_GRANTED) {
}else {
//Return successfu permissions status and Status code
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CODE);
}
并获得用户的回答时候,进行回调判断。系统自动判断,需要返回权限状态码等
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
}
Manifest.permission后接需要申请的权限名称
Android权限大全
android 需要动态申请的权限:
所属权限组 | 权限 |
---|---|
日历 | READ_CALENDAR |
日历 | WRITE_CALENDAR |
相机 | CAMERA |
联系人 | READ_CONTACTS |
联系人 | WRITE_CONTACTS |
联系人 | GET_ACCOUNTS |
位置 | ACCESS_FINE_LOCATION |
位置 | ACCESS_COARSE_LOCATION |
麦克风 | RECORD_AUDIO |
电话 | READ_PHONE_STATE |
电话 | CALL_PHONE |
电话 | READ_CALL_LOG |
电话 | WRITE_CALL_LOG |
电话 | ADD_VOICEMAIL |
电话 | USE_SIP |
电话 | PROCESS_OUTGOING_CALLS |
传感器 | BODY_SENSORS |
短信 | SEND_SMS |
短信 | RECEIVE_SMS |
短信 | READ_SMS |
短信 | RECEIVE_WAP_PUSH |
短信 | RECEIVE_MMS |
存储 | READ_EXTERNAL_STORAGE |
存储 | WRITE_EXTERNAL_STORAGE |
网友评论