Java 在android内部处理OnPause和OnResume

Java 在android内部处理OnPause和OnResume,java,android,inputstream,onresume,onpause,Java,Android,Inputstream,Onresume,Onpause,我尝试使用InputOutput流与USB通信,我使用构造函数并将通信部分放在公共类中,我在片段中使用它,如果我在活动中使用它,如果我在片段中使用它,它会正常工作,应用程序挂起以解决问题我在暂停时终止了连接,但在恢复时连接不仅保留在片段上,而且在活动中使用了相同的逻辑,我在视频中使用了一些动画,这是原因吗 @SuppressLint("ValidFragment") public class Run_Screen extends Fragment implements OnClickListen

我尝试使用InputOutput流与USB通信,我使用构造函数并将通信部分放在公共类中,我在片段中使用它,如果我在活动中使用它,如果我在片段中使用它,它会正常工作,应用程序挂起以解决问题我在暂停时终止了连接,但在恢复时连接不仅保留在片段上,而且在活动中使用了相同的逻辑,我在视频中使用了一些动画,这是原因吗

@SuppressLint("ValidFragment")
public class Run_Screen extends Fragment implements OnClickListener {
    Usb_Communciation usbCom;
    RelativeLayout R_RelativeLayout, Run_Screen_Layout, Screen_Lock_Layout;
    static SharedPreferences sharedpreferences;    
    Animation wipe;
    static int machine;
    MyLog myLog = new MyLog();
    View root = null;
    static Activity thisActivity = null;

    @Override
    public void onAttach(Activity activity) {
        act = activity;
        super.onAttach(activity);
        thisActivity = Run_Screen.this.getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (Vars.Screen_Size == 400) {
            root = inflater.inflate(R.layout.run_screen_400, container, false);
        } else if (Vars.Screen_Size == 600) {
            root = inflater.inflate(R.layout.run_screen_600, container, false);
        } else if (Vars.Screen_Size == 800) {
            root = inflater.inflate(R.layout.run_screen_800, container, false);
        } else {
            root = inflater.inflate(R.layout.run_screen, container, false);
        }

        sharedpreferences = this.getActivity().getSharedPreferences(Vars.MyPre, Context.MODE_PRIVATE);
        GlobalClass.Current_Screen = "Run_Screen";
        cmg = new Comment_Generate(act);
        R_Wiper = (ImageButton) root.findViewById(R.id.R_Wiper);
        wipe = AnimationUtils.loadAnimation(act, R.anim.wipe);
        wipe.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation arg0) {
                // TODO Auto-generated method stub
                Wiper_anim.setVisibility(View.INVISIBLE);
                R_Wiper.setVisibility(View.VISIBLE);
            }
            @Override
            public void onAnimationRepeat(Animation arg0) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onAnimationStart(Animation arg0) {
                // TODO Auto-generated method stub
                Wiper_anim.setVisibility(View.VISIBLE);
            }
        });

        R_Pause_Play.setOnClickListener(this);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        usbCom = new Usb_Communciation(Run_Screen.this.getActivity(), getActivity().getIntent());      
    }

    @Override
    public void onPause() {
        super.onPause();
        Usb_Communciation.AppDisconnect(Run_Screen.this.getActivity());
    }


    public void CheckFunc(){
        if(Vars.ChangeMAchine==1){
            R_Progress.setVisibility(View.VISIBLE);
            R_Separator.setVisibility(View.VISIBLE);
            R_Run.setVisibility(View.INVISIBLE);
            R_Pause.setVisibility(View.VISIBLE);
            R_Stop.setVisibility(View.VISIBLE);
            R_Pause_Play.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.R_Pause_Play:
                DisableAll();
                Reset_Machine_State(-2);
                Vars.ChangeMAchine=1;
                CheckFunc();
                R_Progress.setVisibility(View.VISIBLE);
                Send_Comment(Vars.C_Start_Machine, 0, 0);
                myLog.Cmd("Write", Vars.C_Start_Machine, "0", "0",
                        "Start The Machine");
                break;
        }
    }

    private void Send_Comment(String Comment_No, int Address, int Data) {
        Sending_Comment = cmg.Generate_Comment_for(Comment_No, Address, Data);
        usbCom.Send_message(Sending_Comment);
    }


    public void SetUserLevelControls() {
        Vars.CurrentUserLevel = sharedpreferences.getInt(Vars.UserLevel, 5);
        if (Permission.Launcher_Operator_Screen_Defect_Height <= Vars.CurrentUserLevel) {
            Screen_Lock_Layout.setVisibility(View.GONE);
        } else {
            Screen_Lock_Layout.setVisibility(View.VISIBLE);
        }

        if (Permission.Launcher_Operator_Screen_General <= Vars.CurrentUserLevel) {
            R_Run.setOnClickListener(this);
            R_Pause.setOnClickListener(this);
            R_Stop.setOnClickListener(this);
            R_Pause_Play.setOnClickListener(this);
            Run_Lock.setVisibility(View.GONE);
        } else {
            R_Run.setOnClickListener(null);
            R_Pause.setOnClickListener(null);
            R_Stop.setOnClickListener(null);
            R_Pause_Play.setOnClickListener(null);
            Run_Lock.setVisibility(View.VISIBLE);
        }

    }

}
应用断开连接

 public static void AppDisconnect(Context context) {
        switch(firmwareProtocol) {
            case 2:
                byte[] commandPacket = new byte[2];
                commandPacket[0] = (byte) APP_DISCONNECT;
                commandPacket[1] = 0;
                accessoryManager.write(commandPacket);
                break;
        }

        try {
            while(accessoryManager.isClosed() == false) {
                Thread.sleep(2000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        accessoryManager.disable(context);
        disconnectAccessory();
    }

您的Usb_通信构造函数中有什么?你的Usb_communication.AppDisconnect-method做什么?为什么它是静态的?请检查更新的代码我认为你的问题是
Thread.sleep(2000)
。尝试使用AsyncTask,而不是暂停主线程。
 public static void AppDisconnect(Context context) {
        switch(firmwareProtocol) {
            case 2:
                byte[] commandPacket = new byte[2];
                commandPacket[0] = (byte) APP_DISCONNECT;
                commandPacket[1] = 0;
                accessoryManager.write(commandPacket);
                break;
        }

        try {
            while(accessoryManager.isClosed() == false) {
                Thread.sleep(2000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        accessoryManager.disable(context);
        disconnectAccessory();
    }