官方安卓8.0改動(dòng)介紹
https://developer.android.google.cn/about/versions/oreo/background-location-limits
截取部分圖片
根據(jù)以上圖片介紹烁设,我們決定用Service顯示一個(gè)前臺(tái)通知躺坟,將獲取的定位信息傳給Activity芯杀。
1.services中bind部分
public class LocalBinder extends Binder {
public LocationForegroundService getLocationForegroundService() {
//Android O上才顯示通知欄
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
showNotify();
}
return LocationForegroundService.this;
}
}
-
顯示后臺(tái)定位通知欄
private void showNotify() { if (channel == null) { channel = createNotificationChannel(); } NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), getPackageName()); Intent intent = new Intent(this, MainActivity.class); builder.setContentIntent(PendingIntent.getActivity(this, 0, intent, 0)) .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher)) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("正在后臺(tái)定位") .setContentText("定位進(jìn)行中") .setWhen(System.currentTimeMillis()); Notification build = builder.build(); //調(diào)用這個(gè)方法把服務(wù)設(shè)置成前臺(tái)服務(wù) startForeground(110, build); } @TargetApi(Build.VERSION_CODES.O) private NotificationChannel createNotificationChannel() { NotificationChannel notificationChannel = new NotificationChannel(getPackageName(), getPackageName(), NotificationManager.IMPORTANCE_HIGH); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(notificationChannel); return notificationChannel; }
3.獲取定位信息
public Location gps() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = null;
//不加這段話會(huì)導(dǎo)致下面爆紅,(這個(gè)俗稱版本壓制,哈哈哈哈哈哈)
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return null;
}
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {//是否支持Network定位
//獲取最后的network定位信息
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
//網(wǎng)絡(luò)獲取定位為空時(shí),每隔1秒請(qǐng)求一次
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, locationListener);
}
return location;
}
4.定位監(jiān)聽(tīng)
public interface LocationCallback {
/**
* 當(dāng)前位置
*/
void onLocation(Location location);
}
private LocationCallback mLocationCallback;
public void setLocationCallback(LocationCallback mLocationCallback) {
this.mLocationCallback = mLocationCallback;
}
private class LocationListener implements android.location.LocationListener {
public LocationListener(String provider) {
Log.e(TAG, "LocationListener " + provider);
}
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + "當(dāng)前坐標(biāo):" + location.getLatitude() + " : " + location.getLongitude());
if (mLocationCallback != null) {
mLocationCallback.onLocation(location);
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
5.記得釋放Service
@Override
public void onDestroy() {
stopForeground(true);
stopSelf();
super.onDestroy();
}
services準(zhǔn)備工作已經(jīng)完成倍权,記得在AndroidManifest中注冊(cè)
1.Activity中記得請(qǐng)求運(yùn)行時(shí)權(quán)限悯仙,否則無(wú)法獲取定位
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e(TAG, "--->onServiceConnected");
LocationForegroundService locationForegroundService = ((LocationForegroundService.LocalBinder) service).getLocationForegroundService();
Location location = locationForegroundService.gps();
if (location != null) {
txt.setText("經(jīng)度" + location.getLatitude() + "\n緯度:" + location.getLongitude());
} else {
txt.setText("無(wú)法獲取地理位置1111");
}
locationForegroundService.setLocationCallback(new LocationForegroundService.LocationCallback() {
@Override
public void onLocation(Location location) {
if (location != null) {
txt.setText("經(jīng)度" + location.getLatitude() + "\n緯度:" + location.getLongitude());
} else {
txt.setText("無(wú)法獲取地理位置2222");
}
}
});
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
2.安卓8.0獲取定位需要打開(kāi)定位服務(wù),反正無(wú)法獲取定位信息
private boolean GPSOpen() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 通過(guò)GPS衛(wèi)星定位,定位級(jí)別可以精確到街(通過(guò)24顆衛(wèi)星定位,在室外和空曠的地方定位準(zhǔn)確想诅、速度快)
boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// 通過(guò)WLAN或移動(dòng)網(wǎng)絡(luò)(3G/2G)確定的位置(也稱作AGPS,輔助GPS定位召庞。主要用于在室內(nèi)或遮蓋物(建筑群或茂密的深林等)密集的地方定位)
boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
return gps || network;
}
public void getLocation(View view) {
if (android.os.Build.VERSION.SDK_INT >= 26) {
//判斷定位服務(wù)開(kāi)啟沒(méi)有,沒(méi)有跳轉(zhuǎn)到設(shè)置頁(yè)面
if (!GPSOpen()) {
Intent i = new Intent();
i.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
}
//綁定服務(wù)
Intent intent = new Intent(this, LocationForegroundService.class);
isBound= bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
3.同樣的Activity銷毀后也要解綁Service,為了避免ServiceConnection空指針需要定義一個(gè)變量 isBound
@Override
protected void onDestroy() {
if (isBound) {
unbindService(connection)
isBound = false
}
super.onDestroy();
}
到此適配已經(jīng)完成了岛心。看不明白的同學(xué)可以下載demo查看篮灼。demo中無(wú) isBound 變量自行按照文章中的方式添加