Java Android在连接到服务器时加载微调器
因此,我制作了一个应用程序,可以通过特定端口连接到服务器。 当它尝试连接时,我希望它显示一个加载符号。 我试过这个: StartActivity.java:Java Android在连接到服务器时加载微调器,java,android,multithreading,android-progressbar,Java,Android,Multithreading,Android Progressbar,因此,我制作了一个应用程序,可以通过特定端口连接到服务器。 当它尝试连接时,我希望它显示一个加载符号。 我试过这个: StartActivity.java: package com.ed.istick; public class StartActivity extends AppCompatActivity{ private ProgressBar LS; private Button connectButt; private Button scanButt; @Ov
package com.ed.istick;
public class StartActivity extends AppCompatActivity{
private ProgressBar LS;
private Button connectButt;
private Button scanButt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
LS = (ProgressBar)findViewById(R.id.LodingSymbol);
LS.setVisibility(View.GONE);
final IntentIntegrator a = new IntentIntegrator(this); // `this` is the current Activity
a.setCaptureActivity(CaptureActivityAnyOrientation.class);
a.setOrientationLocked(false);
connectButt = (Button) findViewById(R.id.ConnectButt);
scanButt = (Button) findViewById(R.id.ScanButton);
connectButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//open the connect by Ip & pass screen
final String IP = "10.0.0.2";
final String pass = "hi";
ClientLogic CL = null;
try {
if(createConnection(IP, pass)){
//connection created susecfully, open the template activity
}
else{
//ERROR
Globals g = Globals.getInstance();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
public boolean createConnection(final String IP, final String pass) throws IOException, InterruptedException {
LS.setVisibility(View.VISIBLE);
Globals g = Globals.getInstance();
ClientLogic CL = new ClientLogic(IP, pass);
Thread createClientLogic = new Thread(CL);
createClientLogic.start();
createClientLogic.join();
LS.setVisibility(View.GONE);
if(CL.getStatus()){
g.setCL(CL);
return true;
}
else{
//connection didn't successful
return false;
}
}
ClientLogic.java:
public class ClientLogic implements Runnable{
String IP;
String pass;
private Socket sock;
private Queue<String> messagesToDiagnose;
private Queue<String> messagesToSend;
private DispatchMessage DM;
private SendMessage SM;
private boolean status;
public ClientLogic(String IP, String pass){
messagesToDiagnose = new Queue<String>() {};
messagesToSend = new Queue<String>() {};
this.IP = IP;
this.pass = pass;
status = true;
}
public void addToDiagnose(String msg){
this.messagesToDiagnose.add(msg);
}
public void addToSend(String msg){
this.messagesToSend.add(msg);
}
public String getFirstDiagnose(){
return this.messagesToDiagnose.remove();
}
public String getFirstSend(){
return this.messagesToSend.remove();
}
public boolean processMassage(String msg){
/*
* TO DO: get the code from msg and do a switch case of what to do in a couple of situations
* mostly when the server toss you out
*/
int msgCode = Integer.parseInt(msg.substring(0, msg.indexOf('|')));
switch(msgCode){
case 100:
//connection created susucfully
break;
case 102:
//logout
case 200:
//connection error
case 201:
//iliagle Massage
}
return true;
}
public boolean getStatus(){
return this.status;
}
public void setStatus(boolean status) {
this.status = status;
}
@Override
public void run() {
Globals g = Globals.getInstance();
DataInputStream input = null;
PrintStream output = null;
try {
this.sock = new Socket();
this.sock.connect(new InetSocketAddress(IP, 6580), 10000);
} catch (IOException e) {
status = false;
g.setError(e.toString());
g.setLoading(false);
return;
}
try {
input = new DataInputStream(sock.getInputStream());
output = new PrintStream(sock.getOutputStream());
}
catch (IOException e) {
System.out.println(e);
}
DM = new DispatchMessage(input, this);
SM = new SendMessage(output, this);
status = true;
g.setLoading(false);
}
}
}您可以这样做:
LS.setVisibility(View.VISIBLE);
//some very fast operation
LS.setVisibility(View.GONE);
所以你甚至看不到它是正常的,你让它可见,然后直接消失
操作完成后,必须调用setVisibility(View.GONE)。
如果您的操作非常快,您在任何情况下都看不到它。您可以共享xml吗?@PradeepGupta see edit但即使在服务器关闭时(连接功能将尝试连接10秒,然后将达到超时,线程将完成它的操作,在这10秒内,它应该显示加载徽标,不是吗?
public class StartActivity extends AppCompatActivity{
private ProgressBar LS;
private Button connectButt;
private Button scanButt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
LS = (ProgressBar)findViewById(R.id.LodingSymbol);
LS.setVisibility(View.GONE);
connectButt = (Button) findViewById(R.id.ConnectButt);
scanButt = (Button) findViewById(R.id.ScanButton);
final IntentIntegrator a = new IntentIntegrator(this); // `this` is the current Activity
a.setCaptureActivity(CaptureActivityAnyOrientation.class);
a.setOrientationLocked(false);
connectButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//open the connect by Ip & pass screen
/*Intent loginScreen = new Intent(StartActivity.this, LoginActivity.class);
startActivity(loginScreen);*/
connectButt.setVisibility(View.INVISIBLE);
scanButt.setVisibility(View.INVISIBLE);
LS.setVisibility(View.VISIBLE);
final String IP = "10.0.0.2";
final String pass = "hi";
ClientLogic CL = null;
try {
if(createConnection(IP, pass)){
//connection created susecfully, open the template activity
LS.setVisibility(View.GONE);
}
else{
//ERROR
Globals g = Globals.getInstance();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
//connectButt.setVisibility(View.VISIBLE);
//scanButt.setVisibility(View.VISIBLE);
}
});
scanButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
a.initiateScan();
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
String contents = null;
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
contents = data.getStringExtra("SCAN_RESULT");
String format = data.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
final String IP = contents.substring(0, contents.indexOf('|'));
final String pass = contents.substring(contents.indexOf('|') + 1, contents.length());
try {
if(createConnection(IP, pass)){
//connection created susecfully, open the template activity
}
else{
Globals g = Globals.getInstance();
Toast.makeText(this, "the scan didn't go as plan" + g.getError(), Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else if (resultCode == RESULT_CANCELED) {
// Handle cancel
Toast.makeText(this, "the scan didn't go as plan", Toast.LENGTH_LONG).show();
}
}
public boolean createConnection(final String IP, final String pass) throws IOException, InterruptedException {
Globals g = Globals.getInstance();
g.setLoading(true);
ClientLogic CL = new ClientLogic(IP, pass);
Thread createClientLogic = new Thread(CL);
createClientLogic.start();
createClientLogic.join();
if(CL.getStatus()){
g.setCL(CL);
return true;
}
else{
//connection didn't sucssesfull
return false;
}
}
LS.setVisibility(View.VISIBLE);
//some very fast operation
LS.setVisibility(View.GONE);