我正在尝试使用InputOutput流与USB通信,我使用构造函数并将通信部分放在一个公共类中,我确实在片段中使用它,如果我在一个活动中使用它,如果我在一个内部使用它正常工作片段,应用程序挂起以克服我在 OnPause 时终止连接的问题,但是当恢复连接不仅仅保留在片段上相同的逻辑工作时,我在内部使用一些动画视频是个原因吗?
@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 Usb_Communciation(Context mContext, Intent intent) {
try {
PackageManager manager = mContext.getPackageManager();
PackageInfo info = manager.getPackageInfo(mContext.getPackageName(), 0);
Log.d(TAG, "Info:" + info.packageName + "\n" + info.versionCode + "\n" + info.versionName);
} catch (PackageManager.NameNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
accessoryManager = new USBAccessoryManager(handler, USBAccessoryWhat);
accessoryManager.enable(mContext, intent);
}
AppDisconnect
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();
}