alaurenz / metrobike

0 stars 1 forks source link

SearchActivity must run UI code in a separate thread #54

Closed dutchscout closed 11 years ago

dutchscout commented 11 years ago

example code for a new thread from a UI call

In the UI method, such as a click handler, write: Thread directionsThread = new Thread(new DirThread());

In the activity .java file, add a private class:

private class DirThread implements Runnable { @Override public void run(){ // Code to run doRequest(); } }

dutchscout commented 11 years ago

An example of some code I used to test my live backend:

private void requestForRoutes() {
    Thread dirThread = new Thread(new DirThread());
    dirThread.start();
}

private class DirThread implements Runnable {

    @Override
    public void run() {
        DirectionsRequest dReq =
                (new DirectionsRequest())
                        .setStartAddress(startFromEditText.getText().toString())
                        .setEndAddress(toEditText.getText().toString())
                        .setTravelMode(TravelMode.MIXED).setArrivalTime(1368644400);

        dReq.doLiveRequest();

        Intent intent = new Intent(SearchActivity.this, ResultsActivity.class);
        intent.putExtra("List of Routes", (Serializable) dReq.getSolutions());
        intent.putExtra("Current Route Index", 0);
        startActivity(intent);
        pd.dismiss();
    }

}
dutchscout commented 11 years ago

A new version of this sample code includes an example of getting the error message back:

In SearchActivity.java (NOT in the repo currently):

private void requestForRoutes() { Thread dirThread = new Thread(new DirThread()); dirThread.start(); }

/**
 * An inner class that allows the backend request to run on its own thread.
 * 
 * @author dutchscout
 */
private class DirThread implements Runnable {

    /**
     * {@inheritDoc}
     */
    @Override
    public void run() {
        DirectionsRequest dReq =
                (new DirectionsRequest())
                        .setStartAddress(startFromEditText.getText().toString())
                        .setEndAddress(toEditText.getText().toString())
                        .setTravelMode(TravelMode.MIXED).setArrivalTime(1368644400);

        DirectionsStatus retVal = dReq.doRequest();

        if (retVal.isError()) {
            // There was an error in the request, display this to the user
            // NOTE: This version uses a Toast. Replace with whatever
            // display
            // method you prefer.
            final CharSequence errorMessage =
                    "Error getting directions: " + dReq.getErrorMessages();

            runOnUiThread(new Runnable() {
                public void run() {
                    Toast errorToast =
                            Toast.makeText(getApplicationContext(), errorMessage,
                                    Toast.LENGTH_LONG);
                    errorToast.show();
                }
            });

            // Stay in the search activity
            pd.dismiss();
            return;
        }

        Intent intent = new Intent(SearchActivity.this, ResultsActivity.class);
        intent.putExtra("List of Routes", (Serializable) dReq.getSolutions());
        intent.putExtra("Current Route Index", 0);
        startActivity(intent);
        pd.dismiss();
    }
}
CoolCapri commented 11 years ago

I will add it by Sunday.

CoolCapri commented 11 years ago

I successfully replace a toast with an alert dialog if error happens. I will push it to repo this morning. Yes, we must use runOnUiThread when creating a dialog or toast to avoid crashes due to looper problem.

CoolCapri commented 11 years ago

I have pushed it to the repo. You may try it now.