Browse Source

2.7.5 改动ZlwRecordManager未单例,各类判空

xulh 6 months ago
parent
commit
0b693c6aec

+ 29 - 1
app/src/main/java/com/hh/arome/common/microphone/ZlwRecordManager.java

@@ -1,5 +1,6 @@
 package com.hh.arome.common.microphone;
 
+import android.app.Activity;
 import android.content.Context;
 import android.media.AudioFormat;
 import android.media.MediaExtractor;
@@ -12,11 +13,13 @@ import android.os.Looper;
 import android.text.TextUtils;
 
 import androidx.annotation.RequiresApi;
+import androidx.fragment.app.FragmentActivity;
 
 import com.blankj.utilcode.util.FileUtils;
 import com.common.lib_base.utils.StringUtils;
 import com.hh.arome.MyApplication;
 import com.hh.arome.common.scan.idcard.callback.IDCardScanResultCallback;
+import com.hh.arome.common.scan.idcard.manymodels.ZlwRecordManager;
 import com.hh.arome.utlis.log.AppLogUtils;
 import com.hh.arome.utlis.log.LogType;
 import com.zlw.main.recorderlib.RecordManager;
@@ -51,7 +54,32 @@ public class ZlwRecordManager {
 
     RecordManager recordManager = RecordManager.getInstance();
 
-    public ZlwRecordManager() {
+
+    //用volatile关键字确保 instance 在多线程下的可见性
+    private static volatile ZlwRecordManager instance = null;
+
+    //将构造方法私有化,禁止外部通过构造方法创建实例
+
+    //提供一个公共的访问方法,用于获取该类的唯一实例
+    public static ZlwRecordManager getInstance(Context context) {
+        //如果instance为空,就进行实例化
+        if (instance == null) {
+            //保证多线程下只有一个线程进行实例化
+            synchronized (ZlwRecordManager.class) {
+                //第二次判断,避免多线程下创建多个实例
+                if (instance == null) {
+                    instance = new ZlwRecordManager(context);
+                }
+            }
+        }
+        return instance;
+    }
+
+
+    Context mContext;
+
+    public ZlwRecordManager(Context context) {
+        this.mContext = context;
         init();
     }
 

+ 5 - 1
app/src/main/java/com/hh/arome/common/scan/idcard/manymodels/XzxIDCardScan.java

@@ -416,7 +416,11 @@ public class XzxIDCardScan implements IIDCardScan {
 //                    idCardReaderThread = new IDCardReaderThread();
 //                }
                 if(idCardReaderThread!=null){
-                    idCardReaderThread.read();
+                    try {
+                        idCardReaderThread.read();
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
                 }
             }
 //        },time*1000);

+ 19 - 9
app/src/main/java/com/hh/arome/ui/MainActivity.java

@@ -542,7 +542,7 @@ public class MainActivity extends BaseHHActivity implements DeviceConfigContract
             return;
         }
 
-        zlwRecordManager = new ZlwRecordManager();
+        zlwRecordManager = ZlwRecordManager.getInstance(mContext);
 
         deleteRecordAllFile();
 
@@ -1098,6 +1098,10 @@ public class MainActivity extends BaseHHActivity implements DeviceConfigContract
 
         isCloseMicrophoneMessage = false;
 
+        if(zlwRecordManager == null){
+            zlwRecordManager = ZlwRecordManager.getInstance(mContext);
+        }
+
         // 开始录音,主动推送的
         zlwRecordManager.startRecordSize(intervalTime, new IMicroPhoneSizeCallback() {
             @Override
@@ -1340,7 +1344,7 @@ public class MainActivity extends BaseHHActivity implements DeviceConfigContract
                     // 成功
                     appletId = ampeConfigResultBean.getMiniProgramAppid();
                     AppLogUtils.e("initAndActive--appId&appletId:" + appletId);
-
+                    // 启动小程序
                     launchApplet(appletId);
                 } else if (errorCode == 2002) {
                     // 可能是设备ampe未授权
@@ -2313,7 +2317,7 @@ public class MainActivity extends BaseHHActivity implements DeviceConfigContract
                     ampeActionMap.put(ACTION_START_MICROPHONE, startMicroBean);
 
                     if (zlwRecordManager == null) {
-                        zlwRecordManager = new ZlwRecordManager();
+                        zlwRecordManager = ZlwRecordManager.getInstance(mContext);
                     }
 
                     // 语音生成mp3
@@ -2331,7 +2335,7 @@ public class MainActivity extends BaseHHActivity implements DeviceConfigContract
 
                     //
                     if (zlwRecordManager == null) {
-                        zlwRecordManager = new ZlwRecordManager();
+                        zlwRecordManager = ZlwRecordManager.getInstance(mContext);
                     }
 
                     zlwRecordManager.stopResultListener(new IMicroPhoneResultCallback() {
@@ -2359,7 +2363,7 @@ public class MainActivity extends BaseHHActivity implements DeviceConfigContract
                     isCloseMicrophoneMessage = false;
 
                     if (zlwRecordManager == null) {
-                        zlwRecordManager = new ZlwRecordManager();
+                        zlwRecordManager = ZlwRecordManager.getInstance(mContext);
                     }
 
                     AppLogUtils.e("ACTION_START_MICROPHONE_LISTENER---1");
@@ -2398,7 +2402,7 @@ public class MainActivity extends BaseHHActivity implements DeviceConfigContract
                     isCloseMicrophoneMessage = true;
 
                     if (zlwRecordManager == null) {
-                        zlwRecordManager = new ZlwRecordManager();
+                        zlwRecordManager = ZlwRecordManager.getInstance(mContext);
                     }
 
                     stopMicrophoneListener();
@@ -2420,7 +2424,7 @@ public class MainActivity extends BaseHHActivity implements DeviceConfigContract
                     ampeActionMap.put(ACTION_MICROPHONE_STATE, microStateBean);
 
                     if (zlwRecordManager == null) {
-                        zlwRecordManager = new ZlwRecordManager();
+                        zlwRecordManager = ZlwRecordManager.getInstance(mContext);
                     }
 
                     zlwRecordManager.getState(new IMicroPhoneStateCallback() {
@@ -2643,7 +2647,7 @@ public class MainActivity extends BaseHHActivity implements DeviceConfigContract
     private void startMicroPhone(String event, String taskId, AromeServiceInvoker.BridgeCallback bridgeCallback) {
 
         if (zlwRecordManager == null) {
-            zlwRecordManager = new ZlwRecordManager();
+            zlwRecordManager = ZlwRecordManager.getInstance(mContext);
         }
         zlwRecordManager.start();
 
@@ -3534,7 +3538,13 @@ public class MainActivity extends BaseHHActivity implements DeviceConfigContract
 
 
         // 打印初始化
-        iPrinter.initPrint(new PrintInitResultCallback() {
+        if(iPrinter == null){
+            iPrinter = BasePrinter.getInstance().getPrinter(mContext);
+        }
+
+        iPrinter
+                .initPrint(
+                new PrintInitResultCallback() {
             @Override
             public void onPrintInitSuccess() {
                 // 日志打印和保存(本地和网络)

+ 5 - 1
idCard/src/main/java/com/synjones/idcardusb_reader/IDCardReaderThread.java

@@ -244,7 +244,11 @@ public class IDCardReaderThread {
 
         public void cancel(){
             stopRead=true;
-            interrupt();
+            try {
+                interrupt();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
             try {
                 join();
             } catch (InterruptedException e) {

+ 4 - 0
lib_base/src/main/java/com/common/lib_base/utils/NetworkUtils.java

@@ -55,6 +55,10 @@ public class NetworkUtils {
         ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
         NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
 
+        if(activeNetwork == null){
+            return "";
+        }
+
         int type = activeNetwork.getType();
         Log.e(BuildConfig.LOG_TAG,"getNetType--type--"+type);
 

+ 3 - 1
lib_print/src/main/java/com/common/print/many/MlaPrinter.java

@@ -242,7 +242,9 @@ public class MlaPrinter implements IPrinter {
     @Override
     public void disconnect() {
 
-        mUsbPrinter.close();
+        if(mUsbPrinter!=null){
+            mUsbPrinter.close();
+        }
 
     }