簡體   English   中英

連接到服務器時的Java Android加載微調器

[英]Java Android Loading spinner while connecting to a server

所以我制作了一個可以通過特定端口連接到服務器的應用程序。 當它嘗試連接時,我希望它顯示一個加載符號。 我嘗試了這個:

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

}

你做吧 :

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

因此,您甚至根本看不到它,而是使其可見並直接消失是很正常的。

操作完成后,必須調用setVisibility(View.GONE)。 如果您的操作非常快,無論如何您都不會看到它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM