官方安卓8.0改動介紹
https://developer.android.google.cn/about/versions/oreo/background-location-limits
截取部分圖片
image.png
根據以上圖片介紹,我們決定用Service顯示一個前臺通知,將獲取的定位信息傳給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;
}
}
-
顯示后臺定位通知欄
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("正在后臺定位") .setContentText("定位進行中") .setWhen(System.currentTimeMillis()); Notification build = builder.build(); //調用這個方法把服務設置成前臺服務 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;
//不加這段話會導致下面爆紅,(這個俗稱版本壓制,哈哈哈哈哈哈)
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);
}
//網絡獲取定位為空時,每隔1秒請求一次
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, locationListener);
}
return location;
}
4.定位監聽
public interface LocationCallback {
/**
* 當前位置
*/
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: " + "當前坐標:" + 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準備工作已經完成,記得在AndroidManifest中注冊
1.Activity中記得請求運行時權限,否則無法獲取定位
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("經度" + location.getLatitude() + "\n緯度:" + location.getLongitude());
} else {
txt.setText("無法獲取地理位置1111");
}
locationForegroundService.setLocationCallback(new LocationForegroundService.LocationCallback() {
@Override
public void onLocation(Location location) {
if (location != null) {
txt.setText("經度" + location.getLatitude() + "\n緯度:" + location.getLongitude());
} else {
txt.setText("無法獲取地理位置2222");
}
}
});
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
2.安卓8.0獲取定位需要打開定位服務,反正無法獲取定位信息
private boolean GPSOpen() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 通過GPS衛星定位,定位級別可以精確到街(通過24顆衛星定位,在室外和空曠的地方定位準確、速度快)
boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// 通過WLAN或移動網絡(3G/2G)確定的位置(也稱作AGPS,輔助GPS定位。主要用于在室內或遮蓋物(建筑群或茂密的深林等)密集的地方定位)
boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
return gps || network;
}
public void getLocation(View view) {
if (android.os.Build.VERSION.SDK_INT >= 26) {
//判斷定位服務開啟沒有,沒有跳轉到設置頁面
if (!GPSOpen()) {
Intent i = new Intent();
i.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
}
//綁定服務
Intent intent = new Intent(this, LocationForegroundService.class);
isBound= bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
3.同樣的Activity銷毀后也要解綁Service,為了避免ServiceConnection空指針需要定義一個變量 isBound
@Override
protected void onDestroy() {
if (isBound) {
unbindService(connection)
isBound = false
}
super.onDestroy();
}
到此適配已經完成了??床幻靼椎耐瑢W可以下載demo查看。demo中無 isBound 變量自行按照文章中的方式添加