From 4230457ee9edca6af738ec3f832ed0f49d0d99c3 Mon Sep 17 00:00:00 2001 From: zuoxiao <470321431@qq.com> Date: 星期三, 04 十二月 2024 18:10:30 +0800 Subject: [PATCH] 1.下载地图瓦片判断是否有网。 2.没有上传成功的巡检坐标有网时重新上传。 3.添加网络判断的工具类 --- app/src/main/java/com/dayu/pipirrapp/service/MyLocationService.java | 73 ++++++++++++++++++++++++++++++++---- 1 files changed, 64 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/dayu/pipirrapp/service/MyLocationService.java b/app/src/main/java/com/dayu/pipirrapp/service/MyLocationService.java index 0b665fd..3b17e9c 100644 --- a/app/src/main/java/com/dayu/pipirrapp/service/MyLocationService.java +++ b/app/src/main/java/com/dayu/pipirrapp/service/MyLocationService.java @@ -1,19 +1,26 @@ 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; @@ -22,7 +29,9 @@ 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; import com.tencent.bugly.crashreport.CrashReport; @@ -78,32 +87,62 @@ 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("宸℃瀹氫綅鏈嶅姟姝e湪鍚庡彴杩愯") + .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, "璇峰紑鍚疓PS瀹氫綅", Toast.LENGTH_SHORT).show(); + } listener = new MyLocationListener(); - Criteria criteria = new Criteria(); // 鏌ヨ绮惧害锛氶珮锛孋riteria.ACCURACY_COARSE姣旇緝绮楃暐锛孋riteria.ACCURACY_FINE鍒欐瘮杈冪簿纭� - criteria.setAccuracy(Criteria.ACCURACY_FINE); + criteria.setAccuracy(Criteria.ACCURACY_COARSE); //涓嶈姹傛捣鎷� criteria.setAltitudeRequired(false); //涓嶈姹傛柟浣� @@ -116,7 +155,11 @@ MyLog.i("瀹氫綅鐨刾rovider:" + 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); } @@ -166,6 +209,9 @@ } + /** + * 鍘熺敓瀹氫綅 + */ class MyLocationListener implements LocationListener { // 浣嶇疆鏀瑰彉鏃惰幏鍙栫粡绾害 @Override @@ -173,30 +219,34 @@ 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); } // 鐘舵�佹敼鍙樻椂 @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鍏抽棴浜�"); } } @@ -237,7 +287,7 @@ LatLonBean latLonBean = new LatLonBean(); latLonBean.setLatitude(latitude); latLonBean.setLongitude(longitude); - LiveEventBus.get("location").post(latLonBean); + LiveEventBus.get(CommonKeyName.locationData).post(latLonBean); } @@ -246,6 +296,11 @@ MyLog.i("MyLocationService--onDestroy"); // 鍋滄鍓嶅彴鏈嶅姟--鍙傛暟锛氳〃绀烘槸鍚︾Щ闄や箣鍓嶇殑閫氱煡 stopForeground(true); + // 鑾峰彇NotificationManager骞跺彇娑堥�氱煡锛岀‘淇濋�氱煡琚Щ闄� + NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + if (notificationManager != null) { + notificationManager.cancel(0); // 鍙傛暟1瀵瑰簲鐨勬槸startForeground()涓�氱煡鐨処D + } super.onDestroy(); // 鍋滄鎵�鏈夌殑瀹氫綅鏈嶅姟 try { -- Gitblit v1.8.0