| | |
| | | import com.dayu.pipirrapp.R; |
| | | import com.dayu.pipirrapp.activity.MainActivity; |
| | | import com.dayu.pipirrapp.bean.db.LatLonBean; |
| | | import com.dayu.pipirrapp.utils.CommonKeyName; |
| | | import com.dayu.pipirrapp.utils.MyLog; |
| | | import com.dayu.pipirrapp.utils.ToastUtil; |
| | | import com.jeremyliao.liveeventbus.LiveEventBus; |
| | |
| | | * 备注: |
| | | */ |
| | | public class MyLocationService extends Service { |
| | | |
| | | |
| | | private LocationManager locationManager; |
| | | private MyLocationListener listener; |
| | |
| | | @Override |
| | | public int onStartCommand(Intent intent, int flags, int startId) { |
| | | MyLog.i("onStartCommand()"); |
| | | isSingle = intent.getBooleanExtra("isSingle", false); |
| | | if (intent != null) { |
| | | isSingle = intent.getBooleanExtra("isSingle", false); |
| | | } |
| | | if (!isSingle) { |
| | | // 在API11之后构建Notification的方式 |
| | | Notification.Builder builder = new Notification.Builder |
| | |
| | | PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE); |
| | | |
| | | Notification notification = new NotificationCompat.Builder(this, "location_channel") |
| | | .setContentTitle("定位服务") |
| | | .setContentText("定位服务正在后台运行") |
| | | .setContentTitle("巡检定位") |
| | | .setContentText("巡检定位服务正在后台运行") |
| | | .setSmallIcon(R.mipmap.ic_launcher) |
| | | .setContentIntent(pendingIntent) |
| | | .build(); |
| | |
| | | MyLog.i("原生定位没有权限>>>>"); |
| | | return; |
| | | } |
| | | locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER , 1000, 0, listener); |
| | | locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, listener); |
| | | } catch (Exception e) { |
| | | CrashReport.postCatchedException(e); |
| | | } |
| | |
| | | |
| | | String longitude = "Longitude:" + location.getLongitude(); |
| | | String latitude = "Latitude:" + location.getLatitude(); |
| | | MyLog.i("原生定位onLocationChanged: Latitude:" + latitude + " Longitude:" + longitude); |
| | | MyLog.d("原生定位onLocationChanged: Latitude:" + latitude + " Longitude:" + longitude); |
| | | postPosition(location.getLatitude(), location.getLongitude()); |
| | | // 是否一次性定位 |
| | | if (isSingle) { |
| | | stopSelf(); // 获取到经纬度以后,停止该service |
| | | } |
| | | ToastUtil.showToast(MyLocationService.this, "原生定位onLocationChanged: Latitude:" + latitude + " Longitude:" + longitude); |
| | | // ToastUtil.showToast(MyLocationService.this, "原生定位onLocationChanged: Latitude:" + latitude + " Longitude:" + longitude); |
| | | } |
| | | |
| | | // 状态改变时 |
| | |
| | | LatLonBean latLonBean = new LatLonBean(); |
| | | latLonBean.setLatitude(latitude); |
| | | latLonBean.setLongitude(longitude); |
| | | LiveEventBus.get("location").post(latLonBean); |
| | | LiveEventBus.get(CommonKeyName.locationData).post(latLonBean); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void onDestroy() { |
| | | MyLog.i("MyLocationService--onDestroy"); |
| | | |
| | | // 停止前台服务--参数:表示是否移除之前的通知 |
| | | stopForeground(true); |
| | | // 获取NotificationManager并取消通知,确保通知被移除 |