1.下载地图瓦片判断是否有网。
2.没有上传成功的巡检坐标有网时重新上传。
3.添加网络判断的工具类
| | |
| | | // 地图点击后获取经纬度 |
| | | function getLngLat(lnglat) { |
| | | console.log(lnglat.lng.toFixed(6) + "," + lnglat.lat.toFixed(6)); |
| | | addClickOverLay(lnglat); |
| | | // addClickOverLay(lnglat); |
| | | } |
| | | |
| | | // 点击后添加坐标点 |
| | |
| | | window.setCenterAndZoom = setCenterAndZoom; |
| | | window.updateLocation = updateLocation; |
| | | window.aginShowLocation = aginShowLocation; |
| | | window.cleanLocationLay = cleanLocationLay; |
| | | window.updateInspectionLocation=updateInspectionLocation; |
| | | window.cleanLoclLay=cleanLoclLay; |
| | | |
| | | } |
| | | |
| | | // 调用原生安卓方法显示取水口详情 |
| | |
| | | }); |
| | | return "addMarker加载成功" |
| | | } |
| | | //保存定位坐标生成轨迹 |
| | | var path = []; |
| | | var lineLayer = new T.Polyline([], { color: '#ff4500', weight: 3, opacity: 0.8 }); |
| | | let locationMarker; |
| | | //更新定位坐标 |
| | | function updateLocation(log, lat) { |
| | | var lastLat = lat; |
| | | // const intervalId = setInterval(() => { |
| | | // lastLat=lastLat+0.0001; |
| | | var newPoint = new T.LngLat(log, lastLat); |
| | | path.push(newPoint); |
| | | lineLayer.setLngLats(path); |
| | | map.addOverLay(lineLayer); |
| | | let icon = new T.Icon({ |
| | | iconUrl: locationIMGPath, |
| | | iconSize: new T.Point(20, 20), |
| | |
| | | if (locationMarker) { |
| | | map.removeOverLay(locationMarker); |
| | | } |
| | | locationMarker = new T.Marker(new T.LngLat(log, lastLat), { icon: icon }); |
| | | var newPoint = new T.LngLat(log, lat); |
| | | locationMarker = new T.Marker(newPoint, { icon: icon }); |
| | | map.addOverLay(locationMarker); |
| | | // 移动地图视角到最后一个位置 |
| | | map.panTo(newPoint); |
| | | // }, 500); |
| | | |
| | | } |
| | | //保存定位坐标生成轨迹 |
| | | var path = []; |
| | | var lineLayer = new T.Polyline([], { color: '#ff4500', weight: 3, opacity: 0.8 }); |
| | | let locationMarker; |
| | | function updateInspectionLocation(log, lat) { |
| | | var lastLat = lat; |
| | | // const intervalId = setInterval(() => { |
| | | // lastLat=lastLat+0.0001; |
| | | var newPoint = new T.LngLat(log, lastLat); |
| | | path.push(newPoint); |
| | | lineLayer.setLngLats(path); |
| | | map.addOverLay(lineLayer); |
| | | // 移动地图视角到最后一个位置 |
| | | |
| | | // }, 500); |
| | | } |
| | | |
| | | var aginPath = []; |
| | | var oldLineLayer = new T.Polyline([], { color: '#ff4500', weight: 3, opacity: 0.8 }); |
| | | //显示因异常关闭的巡检记录 |
| | | function aginShowLocation(lng, lat) { |
| | | // 调用 Android 提供的接口,获取数据 |
| | | console.log("aginShowLocation>>lng:"+lng+">>>lat:"+lat); |
| | | console.log("aginShowLocation>>lng:" + lng + ">>>lat:" + lat); |
| | | var newPoint = new T.LngLat(lng, lat); |
| | | aginPath.push(newPoint); |
| | | lineLayer.setLngLats(aginPath); |
| | | map.addOverLay(lineLayer); |
| | | oldLineLayer.setLngLats(aginPath); |
| | | map.addOverLay(oldLineLayer); |
| | | } |
| | | //清除巡检记录轨迹 |
| | | function cleanLocationLay() { |
| | | if (lineLayer) { |
| | | map.removeOverLay(lineLayer); |
| | | path=[] |
| | | } |
| | | if (locationMarker) { |
| | | map.removeOverLay(locationMarker); |
| | | } |
| | | if (oldLineLayer) { |
| | | aginPath=[] |
| | | map.removeOverLay(oldLineLayer); |
| | | } |
| | | } |
| | | //清除本地定位坐标位置 |
| | | function cleanLoclLay(){ |
| | | if (locationMarker) { |
| | | map.removeOverLay(locationMarker); |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | package com.dayu.pipirrapp.activity; |
| | | |
| | | import android.net.ConnectivityManager; |
| | | import android.net.Network; |
| | | import android.os.Bundle; |
| | | import android.view.KeyEvent; |
| | | import android.view.LayoutInflater; |
| | | import android.widget.Toast; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.appcompat.app.AppCompatActivity; |
| | | import androidx.core.content.ContextCompat; |
| | | import androidx.fragment.app.Fragment; |
| | |
| | | import com.dayu.pipirrapp.fragment.MapFragment; |
| | | import com.dayu.pipirrapp.fragment.MyFragment; |
| | | import com.dayu.pipirrapp.net.MqttManager; |
| | | import com.dayu.pipirrapp.tool.InspectionUtils; |
| | | import com.dayu.pipirrapp.utils.MyLog; |
| | | import com.dayu.pipirrapp.utils.NetUtils; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | |
| | | super.onCreate(savedInstanceState); |
| | | binding = ActivityMainBinding.inflate(LayoutInflater.from(this)); |
| | | setContentView(binding.getRoot()); |
| | | |
| | | registNetCallBack(); |
| | | setupFragments(); |
| | | initView(); |
| | | initTab(); |
| | | |
| | | TagBean tagBean = DaoSingleton.getInstance(this).tagDao().findFirst(); |
| | | MyApplication.myApplication.myTag = tagBean.getTag(); |
| | | try { |
| | | TagBean tagBean = DaoSingleton.getInstance(this).tagDao().findFirst(); |
| | | MyApplication.myApplication.myTag = tagBean.getTag(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | mqttManager = new MqttManager(this); |
| | | mqttManager.connect(); |
| | | } |
| | |
| | | protected void onDestroy() { |
| | | super.onDestroy(); |
| | | //关闭MQ |
| | | mqttManager.disconnect(); |
| | | try { |
| | | mqttManager.disconnect(); |
| | | unregisterNetworkCallback(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | binding.myImg.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.bottom_my_black)); |
| | | binding.myText.setTextColor(ContextCompat.getColor(this, R.color.black)); |
| | | } |
| | | |
| | | // 注册网络监控 |
| | | private void registNetCallBack() { |
| | | NetUtils.registerNetCallBack(this, networkCallback); |
| | | } |
| | | |
| | | private void unregisterNetworkCallback() { |
| | | NetUtils.unregisterReceiver(this, networkCallback); |
| | | } |
| | | |
| | | //网络监控 |
| | | ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() { |
| | | @Override |
| | | public void onAvailable(@NonNull Network network) { |
| | | super.onAvailable(network); |
| | | // 这里可以执行网络可用后的逻辑,比如进行数据请求等 |
| | | MyLog.d("onAvailable"); |
| | | InspectionUtils.aginPutInspectionData(MainActivity.this); |
| | | } |
| | | |
| | | @Override |
| | | public void onLost(@NonNull Network network) { |
| | | super.onLost(network); |
| | | // 这里可以执行网络丢失后的逻辑,比如停止数据请求等 |
| | | MyLog.d("onLost"); |
| | | } |
| | | }; |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | } |
| | |
| | | package com.dayu.pipirrapp.bean.db; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.room.Entity; |
| | | import androidx.room.PrimaryKey; |
| | | |
| | |
| | | */ |
| | | @Entity |
| | | public class InspectionBean { |
| | | @PrimaryKey(autoGenerate = true) |
| | | public long id; |
| | | @PrimaryKey() |
| | | @NonNull |
| | | public String id; |
| | | public String inspectId;//巡检ID |
| | | public String mInspectId;//本地数据库巡检ID |
| | | public String startTime;//开始巡检时间 |
| | | public String stopTime;//停止巡检时间 |
| | | public String inspectorId;//巡检员ID |
| | | |
| | | |
| | | public String getInspectorId() { |
| | | return inspectorId; |
| | | } |
| | | |
| | | public void setInspectorId(String inspectorId) { |
| | | this.inspectorId = inspectorId; |
| | | } |
| | | |
| | | @NonNull |
| | | public String getId() { |
| | | return id; |
| | | } |
| | | |
| | | public void setId(@NonNull String id) { |
| | | this.id = id; |
| | | } |
| | | |
| | | public String getmInspectId() { |
| | | return mInspectId; |
| | |
| | | @PrimaryKey() |
| | | @NonNull |
| | | public String id; |
| | | public String inspectorId;//巡检员ID |
| | | |
| | | public String inspectId;//巡检ID |
| | | public String mInspectId;//本地数据库巡检ID |
| | | public String lng;//经度 |
| | |
| | | this.mInspectId = mInspectId; |
| | | } |
| | | |
| | | public String getInspectorId() { |
| | | return inspectorId; |
| | | } |
| | | |
| | | public void setInspectorId(String inspectorId) { |
| | | this.inspectorId = inspectorId; |
| | | } |
| | | |
| | | public String getInspectId() { |
| | | return inspectId; |
| | |
| | | public class InsectionResult { |
| | | |
| | | |
| | | String inspectId;//巡检id |
| | | String inspectorId;//巡检员id |
| | | long inspectId;//巡检id |
| | | long inspectorId;//巡检员id |
| | | |
| | | public String getInspectId() { |
| | | public long getInspectId() { |
| | | return inspectId; |
| | | } |
| | | |
| | | public void setInspectId(String inspectId) { |
| | | public void setInspectId(long inspectId) { |
| | | this.inspectId = inspectId; |
| | | } |
| | | |
| | | public String getInspectorId() { |
| | | public long getInspectorId() { |
| | | return inspectorId; |
| | | } |
| | | |
| | | public void setInspectorId(String inspectorId) { |
| | | public void setInspectorId(long inspectorId) { |
| | | this.inspectorId = inspectorId; |
| | | } |
| | | } |
| | |
| | | Completable insert(InspectionBean inspectionBean); |
| | | |
| | | @Update |
| | | void update(InspectionBean inspectionBean); |
| | | Completable update(InspectionBean inspectionBean); |
| | | |
| | | @Delete |
| | | void delete(InspectionBean inspectionBean); |
| | |
| | | InspectionLocationBean findFirst(); |
| | | |
| | | //查询所有没有上传的坐标 |
| | | @Query("select * from InspectionLocationBean where isPost=false") |
| | | List<InspectionLocationBean> findByNoPost(); |
| | | @Query("select * from InspectionLocationBean where isPost=false AND mInspectId=:mInspectId ORDER BY locateTime ASC") |
| | | Single<List<InspectionLocationBean>> findByNoPostAndInspectId(String mInspectId); |
| | | |
| | | |
| | | //查询所有该巡检id的坐标 |
| | | @Query("select * from InspectionLocationBean where mInspectId=:mInspectId ORDER BY locateTime ASC") |
| | | Single<List<InspectionLocationBean>> findByInspectId(String mInspectId); |
| | | |
| | | //查询所有未上传的mInspectId |
| | | @Query("SELECT DISTINCT mInspectId FROM InspectionLocationBean WHERE isPost = false") |
| | | Single<List<String>> getUnpostedMInspectIds(); |
| | | |
| | | //更新所有mInspectId下的已上传状态 |
| | | @Query("UPDATE InspectionLocationBean SET isPost = true WHERE mInspectId = :mInspectId") |
| | | Completable updataByInspectIdSetIsPost(String mInspectId); |
| | | } |
| | |
| | | import android.location.LocationManager; |
| | | import android.os.Build; |
| | | import android.os.Bundle; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | |
| | | import com.dayu.pipirrapp.tool.InspectionUtils; |
| | | import com.dayu.pipirrapp.utils.CommonData; |
| | | import com.dayu.pipirrapp.utils.CommonKeyName; |
| | | import com.dayu.pipirrapp.utils.DateUtils; |
| | | import com.dayu.pipirrapp.utils.MapJpgUtils; |
| | | import com.dayu.pipirrapp.utils.MyLog; |
| | | import com.dayu.pipirrapp.utils.SharedPreferencesHelper; |
| | |
| | | @Override |
| | | public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { |
| | | String url = request.getUrl().toString(); |
| | | Log.i(TAG, "加载地址>>>" + url); |
| | | // Log.d(TAG, "加载地址>>>" + url); |
| | | //判断当前是否为加载瓦片 |
| | | if (MapJpgUtils.getInsatance().isTianDiTuTileRequest(url)) { |
| | | String androidUrl = url.replace(CommonData.webKey, CommonData.androidKey); |
| | | // 检查本地缓存 |
| | | File cachedTile = MapJpgUtils.getInsatance().getCachedTile(androidUrl); |
| | | if (cachedTile != null && cachedTile.exists()) { |
| | | Log.i(TAG, "本地缓存>>>" + androidUrl); |
| | | // Log.d(TAG, "本地缓存>>>" + androidUrl); |
| | | // if (MapJpgUtils.getInsatance().validateImageFile(androidUrl,request.)) |
| | | // 判断缓存是否过期 |
| | | // if (!MapJpgUtils.getInsatance(MapFragment.this.getContext()).isCacheExpired(cachedTile)) { |
| | |
| | | // } |
| | | } else { |
| | | //下载瓦片 |
| | | ApiManager.getInstance().donwLoadTile(androidUrl); |
| | | ApiManager.getInstance().donwLoadTile(MapFragment.this.getContext(),androidUrl); |
| | | } |
| | | Log.i(TAG, "在线加载>>>" + url); |
| | | // Log.d(TAG, "在线加载>>>" + url); |
| | | } |
| | | |
| | | |
| | |
| | | */ |
| | | private void pushLocationData(InspectionLocationBean inspectionLocationBean) { |
| | | inspectionRequest.setInspectId(inspectionLocationBean.getInspectId()); |
| | | inspectionRequest.setInspectorId(inspectionLocationBean.getInspectorId()); |
| | | inspectionRequest.setInspectorId(mInspectionBean.getInspectorId()); |
| | | if (mInspectionBean != null) { |
| | | inspectionRequest.setStartTime(mInspectionBean.getStartTime()); |
| | | inspectionRequest.setStopTime(mInspectionBean.getStopTime()); |
| | | if (!TextUtils.isEmpty(mInspectionBean.getStopTime())) { |
| | | inspectionRequest.setStopTime(mInspectionBean.getStopTime()); |
| | | } |
| | | } |
| | | InspectionRequest.Track track = new InspectionRequest.Track(); |
| | | track.setLat(inspectionLocationBean.getLat()); |
| | |
| | | try { |
| | | if (t.isSuccess()) { |
| | | if (t.getContent() != null) { |
| | | |
| | | mInspectionBean.setInspectId(String.valueOf(t.getContent().get(0).getInspectId())); |
| | | InspectionUtils.upataInspectionData(MapFragment.this.getContext(), mInspectionBean); |
| | | } |
| | | inspectionLocationBean.setPost(true); |
| | | InspectionUtils.updateInspectionLocationData(MapFragment.this.getContext(), inspectionLocationBean); |
| | |
| | | InspectionLocationBean inspectionLocationBean = InspectionUtils.createInspectionLocation(latLonBean, mInspectionBean); |
| | | InspectionUtils.addInspectionLocationData(MapFragment.this.getContext(), inspectionLocationBean); |
| | | //更新到地图 |
| | | mWebView.evaluateJavascript("javascript:updateInspectionLocation(\"" + latLonBean.getLongitude() + "\",\"" + latLonBean.getLatitude() + "\")", value -> { |
| | | }); |
| | | mWebView.evaluateJavascript("javascript:updateLocation(\"" + latLonBean.getLongitude() + "\",\"" + latLonBean.getLatitude() + "\")", value -> { |
| | | }); |
| | | //上传坐标 |
| | |
| | | location.putExtra("isSingle", false); |
| | | //0没有开始,1开始,2暂停,3关闭 |
| | | switch (inspectionState) { |
| | | case 0: |
| | | case InspectionUtils.NO_INSPECTION: |
| | | binding.inspectButton.setVisibility(View.VISIBLE); |
| | | binding.inspectRL.setVisibility(View.GONE); |
| | | binding.inspectButton.setText("巡"); |
| | |
| | | |
| | | startLocation(location); |
| | | break; |
| | | case 2://暂停 |
| | | case InspectionUtils.PAUSE_INSPECTION://暂停 |
| | | try { |
| | | //关闭定位 |
| | | this.getActivity().stopService(location); |
| | |
| | | binding.inspectPause.setText("继续"); |
| | | binding.inspectRL.setBackgroundColor(this.getContext().getResources().getColor(R.color.inspect_rl_bg_color)); |
| | | SharedPreferencesHelper.getInstance(this.getContext()).put(CommonKeyName.inspectionState, InspectionUtils.PAUSE_INSPECTION); |
| | | //清除地图巡检轨迹 |
| | | mWebView.evaluateJavascript("javascript:cleanLoclLay()", value -> { |
| | | }); |
| | | } catch (Resources.NotFoundException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | break; |
| | | case 3: |
| | | case InspectionUtils.STOP_INSPECTION://关闭 |
| | | ConfirmDialog confirmDialog = new ConfirmDialog(MapFragment.this.getActivity(), "提示", "确认关闭巡检吗?", new ConfirmDialog.ConfirmOnClickListener() { |
| | | @Override |
| | | public void onClick(ConfirmDialog confirmDialog, View v) { |
| | | confirmDialog.dismiss(); |
| | | chageInspecState(InspectionUtils.NO_INSPECTION); |
| | | SharedPreferencesHelper.getInstance(MapFragment.this.getContext()).put(CommonKeyName.inspectionState, InspectionUtils.NO_INSPECTION); |
| | | //更新本地数据库结束时间 |
| | | mInspectionBean.setStopTime(DateUtils.getNowDateStr()); |
| | | InspectionUtils.upataInspectionData(MapFragment.this.getContext(), mInspectionBean); |
| | | //上报结束时间 |
| | | InspectionLocationBean inspectionLocationBean = InspectionUtils.createInspectionLocation(lastLatLonBean, mInspectionBean); |
| | | InspectionUtils.addInspectionLocationData(MapFragment.this.getContext(), inspectionLocationBean); |
| | | pushLocationData(inspectionLocationBean); |
| | | mInspectionBean = new InspectionBean(); |
| | | //清除地图巡检轨迹 |
| | | mWebView.evaluateJavascript("javascript:cleanLocationLay()", value -> { |
| | | }); |
| | | lastLatLonBean = null; |
| | | } |
| | | }); |
| | | confirmDialog.show(); |
| | |
| | | import com.dayu.pipirrapp.utils.SharedPreferencesHelper; |
| | | import com.dayu.pipirrapp.utils.ToastUtil; |
| | | import com.dayu.pipirrapp.view.ConfirmDialog; |
| | | import com.dayu.pipirrapp.view.TagDialog; |
| | | import com.dayu.pipirrapp.view.TipDialog; |
| | | import com.dayu.pipirrapp.view.TipUtil; |
| | | |
| | | /** |
| | |
| | | package com.dayu.pipirrapp.net; |
| | | |
| | | import android.content.Context; |
| | | import android.net.ConnectivityManager; |
| | | import android.net.NetworkCapabilities; |
| | | import android.net.NetworkInfo; |
| | | import android.os.Build; |
| | | import android.util.Log; |
| | | |
| | | import com.dayu.pipirrapp.MyApplication; |
| | |
| | | import com.dayu.pipirrapp.net.upload.UploadFileListener; |
| | | import com.dayu.pipirrapp.utils.MapJpgUtils; |
| | | import com.dayu.pipirrapp.utils.MyJsonParser; |
| | | import com.dayu.pipirrapp.utils.NetUtils; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | |
| | | public <T> void requestGet(final Context context, final String path, final Class<T> tClass, final Map<String, Object> params, final SubscriberListener listener) { |
| | | request(context, false, path, true, tClass, params, listener); |
| | | } |
| | | |
| | | public <T> void requestGetHideLoading(final Context context, final String path, final Class<T> tClass, final Map<String, Object> params, final SubscriberListener listener) { |
| | | request(context, true, path, true, tClass, params, listener); |
| | | } |
| | |
| | | * @param <T> |
| | | */ |
| | | public <T> void getCode(Map<String, Object> params, CodeListener listener) { |
| | | if (isNetworkAvailable(MyApplication.myApplication)) { |
| | | if (NetUtils.isNetworkAvailable(MyApplication.myApplication)) { |
| | | Observable observable; |
| | | observable = apiService.getCode(params); |
| | | observable.subscribeOn(Schedulers.io()) |
| | |
| | | * |
| | | * @param urlPath |
| | | */ |
| | | public void donwLoadTile(String urlPath) { |
| | | Observable<ResponseBody> observable; |
| | | observable = apiService.downloadTile(urlPath); |
| | | public void donwLoadTile(Context context, String urlPath) { |
| | | if (NetUtils.isNetworkAvailable(context)) { |
| | | Observable<ResponseBody> observable; |
| | | observable = apiService.downloadTile(urlPath); |
| | | observable |
| | | .subscribeOn(Schedulers.io()) // 网络请求在 I/O 线程中进行 |
| | | .observeOn(Schedulers.io()) // 回调处理也在 I/O 线程 |
| | | .subscribe( |
| | | responseBody -> { |
| | | |
| | | |
| | | // compositeDisposable.add( |
| | | observable |
| | | .subscribeOn(Schedulers.io()) // 网络请求在 I/O 线程中进行 |
| | | .observeOn(Schedulers.io()) // 回调处理也在 I/O 线程 |
| | | .subscribe( |
| | | responseBody -> { |
| | | |
| | | boolean success = false; |
| | | if (!MapJpgUtils.getInsatance().isHasFiles(urlPath)) { |
| | | success = MapJpgUtils.getInsatance().saveTileToCache(urlPath, responseBody); |
| | | } |
| | | if (success) { |
| | | Log.d(TAG, "Download success for tile: " + urlPath); |
| | | } else { |
| | | Log.e(TAG, "Failed to save tile to disk: " + urlPath); |
| | | } |
| | | }, |
| | | throwable -> Log.e(TAG, "Download failed for tile: " + urlPath) |
| | | ); |
| | | // ); |
| | | |
| | | |
| | | boolean success = false; |
| | | if (!MapJpgUtils.getInsatance().isHasFiles(urlPath)) { |
| | | success = MapJpgUtils.getInsatance().saveTileToCache(urlPath, responseBody); |
| | | } |
| | | if (success) { |
| | | Log.d(TAG, "Download success for tile: " + urlPath); |
| | | } else { |
| | | Log.e(TAG, "Failed to save tile to disk: " + urlPath); |
| | | } |
| | | }, |
| | | throwable -> Log.e(TAG, "Download failed for tile: " + urlPath) |
| | | ); |
| | | } else { |
| | | Log.e(TAG, "没有网络不下载: " + urlPath); |
| | | } |
| | | } |
| | | |
| | | // 停止线程池,释放资源 |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 判断当前是否有网络 |
| | | * |
| | | * @param context |
| | | * @return |
| | | */ |
| | | public boolean isNetworkAvailable(Context context) { |
| | | ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| | | |
| | | if (connectivityManager != null) { |
| | | // 对于 Android 版本大于等于 Android Q (API level 29) |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| | | NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork()); |
| | | if (capabilities != null) { |
| | | // 检查网络是否有传输能力(包括 Wi-Fi 和移动数据) |
| | | if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { |
| | | return true; |
| | | } |
| | | } |
| | | } else { |
| | | // 对于 Android Q 以下版本 |
| | | NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); |
| | | if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) { |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | |
| | | return false; // 无网络连接 |
| | | } |
| | | |
| | | /** |
| | | * 上传文件 |
| | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |
| | |
| | | import android.text.TextUtils; |
| | | |
| | | import com.dayu.pipirrapp.MyApplication; |
| | | import com.dayu.pipirrapp.utils.ToastUtil; |
| | | import com.dayu.pipirrapp.utils.NetUtils; |
| | | import com.tencent.bugly.crashreport.CrashReport; |
| | | |
| | | import java.io.IOException; |
| | |
| | | public Response intercept(Chain chain) throws IOException { |
| | | Request request = chain.request(); |
| | | try { |
| | | |
| | | Request myRequest = createRequest(chain.request()); |
| | | if (myRequest != null) { |
| | | if (NetUtils.isNetworkAvailable(MyApplication.myApplication)) { |
| | | Request myRequest = createRequest(chain.request()); |
| | | Response response = null; |
| | | try { |
| | | response = chain.proceed(myRequest); |
| | |
| | | } |
| | | return response; |
| | | } else { |
| | | ToastUtil.showToastLong(MyApplication.myApplication, "请检查网络连接!!"); |
| | | return getNullResponse(request); |
| | | |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | public Response getNullResponse(Request request) { |
| | | int code = 20010; |
| | | String message = "咦,请检查网络"; |
| | | String errJson = "{\"code\":" + code + ",\"message\":\"" + message + "\"}"; |
| | | String errJson = "{\"code\":" + code + ",\"msg\":\"" + message + "\"}"; |
| | | Response.Builder builder = new Response.Builder().body(ResponseBody.create(MediaType.parse("application/json"), errJson)); |
| | | builder.request(request); |
| | | builder.protocol(Protocol.HTTP_1_1); |
| | |
| | | |
| | | 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) { |
| | |
| | | package com.dayu.pipirrapp.tool; |
| | | |
| | | import static com.dayu.pipirrapp.net.Constants.BASE_URL; |
| | | |
| | | import android.content.Context; |
| | | import android.location.Location; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | |
| | | import androidx.room.Transaction; |
| | |
| | | import com.dayu.pipirrapp.bean.db.InspectionBean; |
| | | import com.dayu.pipirrapp.bean.db.InspectionLocationBean; |
| | | import com.dayu.pipirrapp.bean.db.LatLonBean; |
| | | import com.dayu.pipirrapp.bean.net.InsectionResult; |
| | | import com.dayu.pipirrapp.bean.net.InspectionRequest; |
| | | import com.dayu.pipirrapp.dao.DaoSingleton; |
| | | import com.dayu.pipirrapp.fragment.MapFragment; |
| | | import com.dayu.pipirrapp.net.ApiManager; |
| | | import com.dayu.pipirrapp.net.BaseResponse; |
| | | import com.dayu.pipirrapp.net.subscribers.SubscriberListener; |
| | | import com.dayu.pipirrapp.utils.DateUtils; |
| | | import com.tencent.bugly.crashreport.CrashReport; |
| | | |
| | | import java.util.List; |
| | | import java.util.UUID; |
| | | |
| | | import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; |
| | | import io.reactivex.rxjava3.functions.Action; |
| | | import io.reactivex.rxjava3.functions.Consumer; |
| | | import io.reactivex.rxjava3.schedulers.Schedulers; |
| | | |
| | | /** |
| | |
| | | InspectionBean inspectionBean = new InspectionBean(); |
| | | inspectionBean.setmInspectId(UUID.randomUUID().toString()); |
| | | inspectionBean.setStartTime(DateUtils.getNowDateStr()); |
| | | inspectionBean.setId(UUID.randomUUID().toString()); |
| | | inspectionBean.setInspectorId(MyApplication.myApplication.userId); |
| | | // inspectionBean. |
| | | // 异步插入到数据库 |
| | | DaoSingleton.getAsynchInstance(context) |
| | |
| | | .subscribe(() -> { |
| | | Log.i(TAG, "Inspection started and inserted successfully."); |
| | | }, throwable -> { |
| | | Log.e(TAG, "Error inserting inspection data: "+throwable); |
| | | Log.e(TAG, "Error inserting inspection data: " + throwable); |
| | | }); |
| | | // 获取Dao并执行插入操作 |
| | | return inspectionBean; // 插入完成后切换到主线程 |
| | |
| | | ; |
| | | } |
| | | |
| | | |
| | | //上传因网络问题产生的未上传数据 |
| | | public static void aginPutInspectionData(Context context) { |
| | | //查询没有上传的所有本地数据库的巡检id |
| | | DaoSingleton.getAsynchInstance(context).inspectionLocationDao().getUnpostedMInspectIds() |
| | | .subscribeOn(Schedulers.io()) |
| | | .observeOn(Schedulers.io()) |
| | | .subscribe(strings -> { |
| | | //循环查询所有id |
| | | for (String data : strings) { |
| | | DaoSingleton.getAsynchInstance(context).inspectionDao().findBymInspectId(data) |
| | | .subscribeOn(Schedulers.io()) |
| | | .observeOn(Schedulers.io()) |
| | | .subscribe(inspectionBeans -> { |
| | | DaoSingleton.getAsynchInstance(context).inspectionLocationDao().findByNoPostAndInspectId(data) |
| | | .subscribeOn(Schedulers.io()) |
| | | .observeOn(Schedulers.io()) |
| | | .subscribe(inspectionLocationBeans -> { |
| | | postInspectionData(context, inspectionBeans, inspectionLocationBeans); |
| | | }); |
| | | }); |
| | | } |
| | | |
| | | }); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 上传未上传成功的坐标 |
| | | * |
| | | * @param inspectionBean |
| | | * @param inspectionLocationBeans |
| | | */ |
| | | private static void postInspectionData(Context context, InspectionBean inspectionBean, List<InspectionLocationBean> inspectionLocationBeans) { |
| | | InspectionRequest inspectionRequest = new InspectionRequest(); |
| | | inspectionRequest.setInspectId(inspectionBean.getInspectId()); |
| | | inspectionRequest.setInspectorId(inspectionBean.getInspectorId()); |
| | | inspectionRequest.setStartTime(inspectionBean.getStartTime()); |
| | | if (!TextUtils.isEmpty(inspectionBean.getStopTime())) { |
| | | inspectionRequest.setStopTime(inspectionBean.getStopTime()); |
| | | } |
| | | for (InspectionLocationBean inspectionLocationBean : inspectionLocationBeans) { |
| | | InspectionRequest.Track track = new InspectionRequest.Track(); |
| | | track.setLat(inspectionLocationBean.getLat()); |
| | | track.setLng(inspectionLocationBean.getLng()); |
| | | track.setLocateTime(inspectionLocationBean.getLocateTime()); |
| | | inspectionRequest.addTracks(track); |
| | | } |
| | | ApiManager.getInstance().requestPostHideLoading(context, BASE_URL + "/app/inspect/save", InsectionResult.class, inspectionRequest.toMap(inspectionRequest), new SubscriberListener<BaseResponse<List<InsectionResult>>>() { |
| | | @Override |
| | | public void onNext(BaseResponse<List<InsectionResult>> t) { |
| | | try { |
| | | if (t.isSuccess()) { |
| | | if (t.getContent() != null) { |
| | | inspectionBean.setInspectId(String.valueOf(t.getContent().get(0).getInspectId())); |
| | | InspectionUtils.upataInspectionData(context, inspectionBean); |
| | | } |
| | | DaoSingleton.getAsynchInstance(context).inspectionLocationDao().updataByInspectIdSetIsPost(inspectionBean.getmInspectId()).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).subscribe(() -> { |
| | | // 插入成功的回调 |
| | | Log.d(TAG, "addInspectionLocationData数据插入成功"); |
| | | }, throwable -> { |
| | | // 处理错误 |
| | | Log.e(TAG, "addInspectionLocationData数据插入失败", throwable); |
| | | }); |
| | | } else { |
| | | |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | CrashReport.postCatchedException(e); |
| | | } |
| | | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 修改巡检记录坐标 |
| | | * |
| | |
| | | Log.e(TAG, "updateInspectionLocationData数据插入失败", throwable); |
| | | }); |
| | | ; |
| | | } |
| | | |
| | | /** |
| | | * 修改巡检信息 |
| | | * |
| | | * @param context |
| | | * @param inspectionBean |
| | | */ |
| | | public static void upataInspectionData(Context context, InspectionBean inspectionBean) { |
| | | DaoSingleton.getAsynchInstance(context).inspectionDao().update(inspectionBean).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(() -> { |
| | | // 插入成功的回调 |
| | | Log.d(TAG, "uupataInspectionData数据插入成功"); |
| | | }, throwable -> { |
| | | Log.e(TAG, "upataInspectionData数据插入失败", throwable); |
| | | }); |
| | | } |
| | | |
| | | /** |
| | |
| | | inspectionLocationBean.setmInspectId(mInspectionBean.getmInspectId()); |
| | | inspectionLocationBean.setLocateTime(DateUtils.getNowDateStr()); |
| | | inspectionLocationBean.setPost(false); |
| | | inspectionLocationBean.setInspectorId(MyApplication.myApplication.userId); |
| | | |
| | | inspectionLocationBean.setLng(String.valueOf(latLonBean.getLongitude())); |
| | | inspectionLocationBean.setLat(String.valueOf(latLonBean.getLatitude())); |
| | | return inspectionLocationBean; |
| | |
| | | Log.i(TAG, data); |
| | | } |
| | | |
| | | public static void d(String data) { |
| | | Log.d(TAG, data); |
| | | } |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.dayu.pipirrapp.utils; |
| | | |
| | | import android.content.Context; |
| | | import android.net.ConnectivityManager; |
| | | import android.net.NetworkCapabilities; |
| | | import android.net.NetworkInfo; |
| | | import android.net.NetworkRequest; |
| | | import android.os.Build; |
| | | |
| | | /** |
| | | * NetUtils - 网络相关公共方法 |
| | | * |
| | | * @author zuoxiao |
| | | * @version 1.0 |
| | | * @since 2024-12-04 |
| | | */ |
| | | public class NetUtils { |
| | | public static final String TAG = "NetUtils"; |
| | | |
| | | /** |
| | | * 判断当前是否有网络 |
| | | * |
| | | * @param context |
| | | * @return |
| | | */ |
| | | public static boolean isNetworkAvailable(Context context) { |
| | | ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| | | |
| | | if (connectivityManager != null) { |
| | | // 对于 Android 版本大于等于 Android Q (API level 29) |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| | | NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork()); |
| | | if (capabilities != null) { |
| | | // 检查网络是否有传输能力(包括 Wi-Fi 和移动数据) |
| | | if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { |
| | | return true; |
| | | } |
| | | } |
| | | } else { |
| | | // 对于 Android Q 以下版本 |
| | | NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); |
| | | if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) { |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | |
| | | return false; // 无网络连接 |
| | | } |
| | | |
| | | |
| | | public static void registerNetCallBack(Context context, ConnectivityManager.NetworkCallback networkCallback) { |
| | | // 注册网络状态监听 |
| | | |
| | | ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| | | |
| | | // 构建一个 NetworkRequest,指定感兴趣的网络类型 |
| | | NetworkRequest networkRequest = new NetworkRequest.Builder() |
| | | |
| | | .build(); |
| | | |
| | | // 网络回调 |
| | | |
| | | |
| | | // 注册网络监听 |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| | | cm.registerNetworkCallback(networkRequest, networkCallback); |
| | | } else { |
| | | // 对于较老版本,可以使用旧的方法注册网络状态监听 |
| | | // ConnectivityManager.setNetworkCallback() 无法在 API 级别 24 以下使用 |
| | | } |
| | | } |
| | | |
| | | // 注销广播接收器 |
| | | public static void unregisterReceiver(Context context,ConnectivityManager.NetworkCallback networkCallback) { |
| | | ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| | | cm.unregisterNetworkCallback(networkCallback); |
| | | } |
| | | |
| | | } |
| | |
| | | android:background="@drawable/ic_edt_gray_bg" |
| | | android:hint="请输入账号" |
| | | android:maxLines="1" |
| | | android:text="admin" |
| | | android:text="15802220723" |
| | | android:paddingLeft="15dp" |
| | | android:singleLine="true" /> |
| | | |
| | |
| | | android:hint="请输入密码" |
| | | android:inputType="textPassword" |
| | | android:maxLines="1" |
| | | android:text="dyyjy" |
| | | android:text="abc_123" |
| | | android:paddingLeft="10dp" |
| | | android:singleLine="true" /> |
| | | |