public class FacesSet {
    private static FacesSet instance = null;
    private Set<String> faceNames = new HashSet<String>();

    private FacesSet(){}

    public static FacesSet createInstance(){
        synchronized (FacesSet.class.getClass()){
            if(instance == null){
                instance = new FacesSet();
            }
            return instance;
        }
    }

    public static FacesSet getInstance() {
        return instance;
    }

    public static void setInstance(FacesSet instance) {
        FacesSet.instance = instance;
    }

    public Set<String> getFaceNames() {
        return faceNames;
    }

    public void setFaceNames(Set<String> faceNames) {
        this.faceNames = faceNames;
    }
}

   首先新建一個存取面部資訊類的單例模式 

 //TODO:  視訊流讀取到會員資訊時發生互動,如打招呼
            if (msg_id.equals("FACE_DETECT_FACE_LIST_NTF")){
                    FacesSet mySet = FacesSet.createInstance();
                    JSONArray arr = jsonObject.getJSONArray("face_list");
                    JSONObject face_list = (JSONObject) arr.get(0);
                    JSONObject face_recg = face_list.getJSONObject("face_recg");
                    String face_name = face_recg.getString("name");
                    if (!face_name.equals(mySet.getFaceNames().toString())){
                        flag = true;
                    }
                    if(flag){
                        tts.startSpeaking("很高興見到你啊," + face_name + "請問需要什麼幫助呢?", new OnSpeakListener() {
                            @Override
                            public void onSpeakBegin() {
                                Log.d("startSpeaking", "startSpeaking");
                                }
                                @Override
                                public void onCompleted() {
                                Log.d("startSpeaking", "onCompleted");
                                }
                                });
                            Set<String> faceNames = new HashSet<>();
                            faceNames.add(face_name);
                            mySet.setFaceNames(faceNames);
                            flag = false;
                    }

     定義一個全域性變數flag=true,每執行此程式碼塊會優先判斷傳入face_name是否與之前傳入並置入雜湊表中的資料相等,若相等,則彈出,若不相等,則重置flag為true,再次執行本程式碼塊。

     可以忽略重複讀入相同資料的影響。