2016-04-13 42 views
1

所以我做了一個應用程序,連接到某個端口的服務器。當它試圖連接時我想讓它顯示一個加載符號。 我想這:Java Android Android連接服務器時加載微調器

StartActivity.java:

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); 
} 
} 

activity_start.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.ed.istick.StartActivity"> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="By Ip &amp; Pass" 
    android:id="@+id/ConnectButt" 
    android:textAllCaps="false" 
    android:textColor="#FFFFFF" 
    android:textSize="20sp" 
    android:background="@drawable/barcode_button_shape" 
    android:shadowColor="#A8A8A8" 
    android:shadowDx="0" 
    android:shadowDy="0" 
    android:shadowRadius="5" 
    android:layout_centerVertical="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Welcome To iStick" 
    android:id="@+id/textView" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:autoText="false" /> 

<ProgressBar 
    android:id="@+id/LodingSymbol" 
    style="?android:attr/progressBarStyleLarge" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:indeterminate="true" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true"/> 

</RelativeLayout> 

,但它甚至不顯示商標。 我在哪裏弄錯了?

編輯1:我已經改變了一點點的代碼,現在它顯示的標誌和所有的視圖更改只有在連接線程完成後,儘管視圖更改的代碼行在線程運行之前。 (如果服務器出現故障,基本上應用程序將凍結10秒鐘,然後顯示標識)。 我想讓它顯示的標誌,然後嘗試連接

StartActivity.java(有改動):

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; 
    } 
} 

}

+0

你可以分享xml嗎? –

+0

@PradeepGupta查看編輯 – etamar211

回答

2

你做到這一點:

LS.setVisibility(View.VISIBLE); 
//some very fast operation 
LS.setVisibility(View.GONE); 

所以是正常的,你甚至沒有看到它,你使它可見並直接消失。

當您的操作完成時,您必須調用setVisibility(View.GONE)。 如果你的操作非常快,你甚至不會在任何情況下看到它。

+0

,但即使服務器關閉(連接功能將嘗試連接10秒,然後將達到超時,線程將完成它的操作,對於那些10秒它應該顯示加載標誌,isn是嗎? – etamar211