|  |  |  | 
|---|
|  |  |  | package com.dayu.pipirrapp.service; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import android.annotation.SuppressLint; | 
|---|
|  |  |  | import android.Manifest; | 
|---|
|  |  |  | import android.app.Notification; | 
|---|
|  |  |  | import android.app.NotificationChannel; | 
|---|
|  |  |  | import android.app.NotificationManager; | 
|---|
|  |  |  | import android.app.PendingIntent; | 
|---|
|  |  |  | import android.app.Service; | 
|---|
|  |  |  | import android.content.Intent; | 
|---|
|  |  |  | import android.content.pm.PackageManager; | 
|---|
|  |  |  | import android.graphics.BitmapFactory; | 
|---|
|  |  |  | import android.location.Criteria; | 
|---|
|  |  |  | import android.location.Location; | 
|---|
|  |  |  | import android.location.LocationListener; | 
|---|
|  |  |  | import android.location.LocationManager; | 
|---|
|  |  |  | import android.os.Build; | 
|---|
|  |  |  | import android.os.Bundle; | 
|---|
|  |  |  | import android.os.IBinder; | 
|---|
|  |  |  | import android.widget.Toast; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import androidx.annotation.Nullable; | 
|---|
|  |  |  | import androidx.core.app.ActivityCompat; | 
|---|
|  |  |  | import androidx.core.app.NotificationCompat; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baidu.location.BDAbstractLocationListener; | 
|---|
|  |  |  | import com.baidu.location.BDLocation; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.dayu.pipirrapp.activity.MainActivity; | 
|---|
|  |  |  | import com.dayu.pipirrapp.bean.db.LatLonBean; | 
|---|
|  |  |  | import com.dayu.pipirrapp.utils.MyLog; | 
|---|
|  |  |  | import com.dayu.pipirrapp.utils.ToastUtil; | 
|---|
|  |  |  | import com.jeremyliao.liveeventbus.LiveEventBus; | 
|---|
|  |  |  | import com.tencent.bugly.crashreport.CrashReport; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | Notification notification = builder.build(); // 获取构建好的Notification | 
|---|
|  |  |  | notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音 | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return super.onStartCommand(intent, flags, startId); | 
|---|
|  |  |  | return START_STICKY;// 如果系统终止该服务,之后会重启 | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void onCreate() { | 
|---|
|  |  |  | super.onCreate(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 启动前台服务 | 
|---|
|  |  |  | startForegroundService(); | 
|---|
|  |  |  | createNativeLocation(); | 
|---|
|  |  |  | //        createBDLocation(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void startForegroundService() { | 
|---|
|  |  |  | // 针对 Android 8.0 及更高版本创建通知渠道 | 
|---|
|  |  |  | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | 
|---|
|  |  |  | NotificationChannel channel = new NotificationChannel( | 
|---|
|  |  |  | "location_channel", | 
|---|
|  |  |  | "Location Service Channel", | 
|---|
|  |  |  | NotificationManager.IMPORTANCE_DEFAULT | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | NotificationManager manager = getSystemService(NotificationManager.class); | 
|---|
|  |  |  | if (manager != null) { | 
|---|
|  |  |  | manager.createNotificationChannel(channel); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 创建通知 | 
|---|
|  |  |  | Intent notificationIntent = new Intent(this, MainActivity.class); | 
|---|
|  |  |  | PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Notification notification = new NotificationCompat.Builder(this, "location_channel") | 
|---|
|  |  |  | .setContentTitle("定位服务") | 
|---|
|  |  |  | .setContentText("定位服务正在后台运行") | 
|---|
|  |  |  | .setSmallIcon(R.mipmap.ic_launcher) | 
|---|
|  |  |  | .setContentIntent(pendingIntent) | 
|---|
|  |  |  | .build(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 将服务设置为前台服务 | 
|---|
|  |  |  | startForeground(1, notification); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 原生的定位服务 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @SuppressLint("MissingPermission") | 
|---|
|  |  |  | private void createNativeLocation() { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); | 
|---|
|  |  |  | if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { | 
|---|
|  |  |  | // 提示用户开启定位 | 
|---|
|  |  |  | Toast.makeText(this, "请开启GPS定位", Toast.LENGTH_SHORT).show(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | listener = new MyLocationListener(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Criteria criteria = new Criteria(); | 
|---|
|  |  |  | // 查询精度:高,Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精确 | 
|---|
|  |  |  | criteria.setAccuracy(Criteria.ACCURACY_FINE); | 
|---|
|  |  |  | criteria.setAccuracy(Criteria.ACCURACY_COARSE); | 
|---|
|  |  |  | //不要求海拔 | 
|---|
|  |  |  | criteria.setAltitudeRequired(false); | 
|---|
|  |  |  | //不要求方位 | 
|---|
|  |  |  | 
|---|
|  |  |  | MyLog.i("定位的provider:" + provider); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //第二个参数是间隔时间 第三个参数是间隔多少距离,这里我试过了不同的各种组合,能获取到位置就是能,不能获取就是不能 | 
|---|
|  |  |  | locationManager.requestLocationUpdates(provider, 1000, 0, listener); | 
|---|
|  |  |  | if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | 
|---|
|  |  |  | MyLog.i("原生定位没有权限>>>>"); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER , 1000, 0, listener); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | CrashReport.postCatchedException(e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 原生定位 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | class MyLocationListener implements LocationListener { | 
|---|
|  |  |  | // 位置改变时获取经纬度 | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | if (isSingle) { | 
|---|
|  |  |  | stopSelf();  // 获取到经纬度以后,停止该service | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ToastUtil.showToast(MyLocationService.this, "原生定位onLocationChanged:  Latitude:" + latitude + "  Longitude:" + longitude); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 状态改变时 | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void onStatusChanged(String provider, int status, Bundle extras) { | 
|---|
|  |  |  | MyLog.i("onStatusChanged - provider:" + provider + " status:" + status); | 
|---|
|  |  |  | ToastUtil.showToast(MyLocationService.this, "onStatusChanged - provider:" + provider + " status:" + status); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 提供者可以使用时 | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void onProviderEnabled(String provider) { | 
|---|
|  |  |  | MyLog.i("GPS开启了"); | 
|---|
|  |  |  | ToastUtil.showToast(MyLocationService.this, "GPS开启了"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 提供者不可以使用时 | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void onProviderDisabled(String provider) { | 
|---|
|  |  |  | MyLog.i("GPS关闭了"); | 
|---|
|  |  |  | ToastUtil.showToast(MyLocationService.this, "GPS关闭了"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | MyLog.i("MyLocationService--onDestroy"); | 
|---|
|  |  |  | // 停止前台服务--参数:表示是否移除之前的通知 | 
|---|
|  |  |  | stopForeground(true); | 
|---|
|  |  |  | // 获取NotificationManager并取消通知,确保通知被移除 | 
|---|
|  |  |  | NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | 
|---|
|  |  |  | if (notificationManager != null) { | 
|---|
|  |  |  | notificationManager.cancel(0);  // 参数1对应的是startForeground()中通知的ID | 
|---|
|  |  |  | } | 
|---|
|  |  |  | super.onDestroy(); | 
|---|
|  |  |  | // 停止所有的定位服务 | 
|---|
|  |  |  | try { | 
|---|