管灌系统巡查员智能手机App
zuoxiao
2024-12-12 c91d4503a63013be3242120ae0ad9cd1b0b8127e
优化定位服务相关代码
4个文件已修改
123 ■■■■■ 已修改文件
app/src/main/java/com/dayu/pipirrapp/activity/AddIssueActivity.java 29 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/dayu/pipirrapp/activity/OrderDealActivity.java 30 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/dayu/pipirrapp/fragment/MapFragment.java 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/dayu/pipirrapp/utils/ServiceUtils.java 38 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/dayu/pipirrapp/activity/AddIssueActivity.java
@@ -4,7 +4,6 @@
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
@@ -31,7 +30,6 @@
import com.dayu.pipirrapp.net.BaseResponse;
import com.dayu.pipirrapp.net.subscribers.SubscriberListener;
import com.dayu.pipirrapp.net.upload.UploadFileListener;
import com.dayu.pipirrapp.service.MyLocationService;
import com.dayu.pipirrapp.tool.FullyGridLayoutManager;
import com.dayu.pipirrapp.tool.GlideEngine;
import com.dayu.pipirrapp.tool.ImageFileCompressEngine;
@@ -229,6 +227,7 @@
            existingPaths.add(uplodData.getFilePath());
        }
        //获取在PictureSelector的相册时取消选中的图片
        Set<String> resultPaths = new HashSet<>();
        for (LocalMedia media : result) {
            if (media.getWidth() == 0 || media.getHeight() == 0) {
@@ -267,8 +266,19 @@
                // 执行上传图片的操作
                uplodeImg(uplodFileState);
            }
            resultPaths.add(media.getCompressPath());
        }
        //处理从相册返回时取消的图片处理
        existingPaths.removeAll(resultPaths);
        if (!existingPaths.isEmpty()) {
            for (String path : existingPaths) {
                for (UplodFileState uplodFileState : uplodFileStates) {
                    if (path.equals(uplodFileState.getFilePath())) {
                        uplodFileState.getThisCall().cancel();
                        uplodFileStates.remove(uplodFileState);
                    }
                }
            }
        }
        runOnUiThread(new Runnable() {
            @Override
@@ -341,16 +351,7 @@
     * 开始定位相关逻辑
     */
    private void startLocation() {
        if (!ServiceUtils.isServiceRunning(this, MyLocationService.class)) {
            Intent location = new Intent(this, MyLocationService.class);
            location.putExtra("isSingle", true);
            //开启定位
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(location);
            } else {
                startService(location);
            }
        }
        ServiceUtils.startLocationService(this,  true);
        //获取定位服务传过来的坐标点
        LiveEventBus.get(CommonKeyName.locationData).observeForever(locationObserver);
    }
app/src/main/java/com/dayu/pipirrapp/activity/OrderDealActivity.java
@@ -4,7 +4,6 @@
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
@@ -30,7 +29,6 @@
import com.dayu.pipirrapp.net.BaseResponse;
import com.dayu.pipirrapp.net.subscribers.SubscriberListener;
import com.dayu.pipirrapp.net.upload.UploadFileListener;
import com.dayu.pipirrapp.service.MyLocationService;
import com.dayu.pipirrapp.tool.FullyGridLayoutManager;
import com.dayu.pipirrapp.tool.GlideEngine;
import com.dayu.pipirrapp.tool.ImageFileCompressEngine;
@@ -227,10 +225,13 @@
     * @param result
     */
    private void analyticalSelectResults(ArrayList<LocalMedia> result) {
        //获取当前uplodFileStates所有的图片地址
        Set<String> existingPaths = new HashSet<>();
        for (UplodFileState uplodData : uplodFileStates) {
            existingPaths.add(uplodData.getFilePath());
        }
        //获取在PictureSelector的相册时取消选中的图片
        Set<String> resultPaths = new HashSet<>();
        for (LocalMedia media : result) {
            if (media.getWidth() == 0 || media.getHeight() == 0) {
                if (PictureMimeType.isHasImage(media.getMimeType())) {
@@ -269,7 +270,19 @@
                uplodeImg(uplodFileState);
            }
            resultPaths.add(media.getCompressPath());
        }
        //处理从相册返回时取消的图片处理
        existingPaths.removeAll(resultPaths);
        if (!existingPaths.isEmpty()) {
            for (String path : existingPaths) {
                for (UplodFileState uplodFileState : uplodFileStates) {
                    if (path.equals(uplodFileState.getFilePath())) {
                        uplodFileState.getThisCall().cancel();
                        uplodFileStates.remove(uplodFileState);
                    }
                }
            }
        }
        runOnUiThread(new Runnable() {
            @Override
@@ -343,16 +356,7 @@
     * 开始定位相关逻辑
     */
    private void startLocation() {
        if (!ServiceUtils.isServiceRunning(this, MyLocationService.class)) {
            Intent location = new Intent(this, MyLocationService.class);
            location.putExtra("isSingle", true);
            //开启定位
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(location);
            } else {
                startService(location);
            }
        }
        ServiceUtils.startLocationService(this,true);
        //获取定位服务传过来的坐标点
        LiveEventBus.get(CommonKeyName.locationData).observeForever(locationObserver);
    }
app/src/main/java/com/dayu/pipirrapp/fragment/MapFragment.java
@@ -5,8 +5,6 @@
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
@@ -43,13 +41,13 @@
import com.dayu.pipirrapp.net.Constants;
import com.dayu.pipirrapp.net.subscribers.SubscriberListener;
import com.dayu.pipirrapp.observer.MapFragmenObserver;
import com.dayu.pipirrapp.service.MyLocationService;
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.ServiceUtils;
import com.dayu.pipirrapp.utils.SharedPreferencesHelper;
import com.dayu.pipirrapp.utils.ToastUtil;
import com.dayu.pipirrapp.utils.WebViewUtils;
@@ -490,8 +488,7 @@
     * @param inspectionState
     */
    private void chageInspecState(int inspectionState) {
        Intent location = new Intent(this.getActivity(), MyLocationService.class);
        location.putExtra("isSingle", false);
        //0没有开始,1开始,2暂停,3关闭
        switch (inspectionState) {
            case InspectionUtils.NO_INSPECTION:
@@ -500,21 +497,21 @@
                binding.inspectButton.setText("巡");
                LiveEventBus.get(CommonKeyName.locationData).removeObserver(locationObserver);
                //关闭定位
                this.getActivity().stopService(location);
                ServiceUtils.stopLocationService(MapFragment.this.getContext());
                break;
            case InspectionUtils.STAT_INSPECTION_ONCLICK:
                //添加新的巡检记录
                mInspectionBean = InspectionUtils.startInspection(this.getContext());
                startLocation(location);
                startLocation();
                break;
            case InspectionUtils.STAT_INSPECTION://1开始
                startLocation(location);
                startLocation();
                break;
            case InspectionUtils.PAUSE_INSPECTION://暂停
                try {
                    //关闭定位
                    this.getActivity().stopService(location);
                    ServiceUtils.stopLocationService(MapFragment.this.getContext());
                    binding.stateText.setText("已暂停巡检");
                    binding.inspectPause.setText("继续");
                    binding.inspectRL.setBackgroundColor(this.getContext().getResources().getColor(R.color.inspect_rl_bg_color));
@@ -584,9 +581,9 @@
    /**
     * 开始巡检的相关逻辑
     *
     * @param location
     * @param
     */
    private void startLocation(Intent location) {
    private void startLocation() {
        //获取定位服务传过来的坐标点
        LiveEventBus.get(CommonKeyName.locationData).observeForever(locationObserver);
        binding.inspectRL.setVisibility(View.VISIBLE);
@@ -594,14 +591,9 @@
        binding.inspectPause.setText("暂停");
        binding.stateText.setText("已开启巡检");
        binding.inspectRL.setBackgroundColor(this.getContext().getResources().getColor(R.color.base_blue));
        //开启定位
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            this.getActivity().startForegroundService(location);
        } else {
            this.getActivity().startService(location);
        }
        inspectionRequest = new InspectionRequest();
        SharedPreferencesHelper.getInstance(this.getContext()).put(CommonKeyName.inspectionState, InspectionUtils.STAT_INSPECTION);
        ServiceUtils.startLocationService(MapFragment.this.getContext(), false);
    }
app/src/main/java/com/dayu/pipirrapp/utils/ServiceUtils.java
@@ -2,6 +2,10 @@
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import com.dayu.pipirrapp.service.MyLocationService;
/**
 * ServiceUtils - 服务相关的公共方法
@@ -28,4 +32,38 @@
        }
        return false;
    }
    /**
     * 开启定位服务
     *
     * @param context
     * @param isSingle
     */
    public static void startLocationService(Context context, boolean isSingle) {
        if (!isServiceRunning(context, MyLocationService.class)) {
            Intent location = new Intent(context, MyLocationService.class);
            location.putExtra("isSingle", isSingle);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(location);
            } else {
                context.startService(location);
            }
        }
    }
    /**
     * 关闭定位服务
     *
     * @param context
     */
    public static void stopLocationService(Context context) {
        try {
            Intent location = new Intent(context, MyLocationService.class);
            context.stopService(location);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}