1. 获取位置、IMEI、存储信息
1. 初始化
初始化到一个数组中,然后去注册权限
private void initPermission() {
List<String> permissionList = new ArrayList<>();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(Manifest.permission.READ_PHONE_STATE);
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (!permissionList.isEmpty()) {
ActivityCompat.requestPermissions(mActivity, permissionList.toArray(new String[permissionList.size()]), 1);
} else {}
}
2. 接收回调
重写 onRequestPermissionsResult() 方法,监测注册的权限
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
int locationFlags = 0;
if (requestCode == REQUEST_PERMISSION_LOCATION) {
for (int i = 0; i < permissions.length; i++) {
LUtil.i("onRequestPermissionsResult null : " + requestCode + ", str :" + permissions.length + ", con :" + permissions[i]);
if (permissions[i].equals(Manifest.permission.READ_PHONE_STATE)) {
if (grantResults[i] != -1) {
initDeviceID();
}
}
if (permissions[i].equals(Manifest.permission.ACCESS_COARSE_LOCATION)) {
if (grantResults[i] != -1) {
locationFlags += 1;
}
}
if (permissions[i].equals(Manifest.permission.ACCESS_FINE_LOCATION)) {
if (grantResults[i] != -1) {
locationFlags += 1;
}
}
}
if (locationFlags == 2) {
getLocation(true);
}
}
}
3. 具体实现
- 获取位置信息
private void initLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
initLocationPermission();
Toast.makeText(mActivity, "无法获取位置信息", Toast.LENGTH_SHORT);
} else {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null || locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) != null) {
getLocation(false);
}
}
}
获取位置
private void getLocation(boolean iLM) {
if (iLM) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
String provider = "";
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
provider = LocationManager.GPS_PROVIDER;
} else {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
provider = LocationManager.NETWORK_PROVIDER;
}
}
DecimalFormat df = new DecimalFormat("#.000000");
String lati_str = df.format(latitude);
String long_str = df.format(longitude);
LUtil.i("imei : " + mImei + ", lat : " + lati_str + ", lon :" + long_str);
locationManager.requestLocationUpdates(provider, 2000, 0, locationListener);
}
- 获取设备信息
private void initDeviceID() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
initImeiPromession();
Toast.makeText(mActivity, "无法获取设备信息", Toast.LENGTH_SHORT);
}else {
TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(mContext.TELEPHONY_SERVICE);
String imei = telephonyManager.getDeviceId();
mImei = imei;
LUtil.i("imei : " + mImei);
}
}
网友评论