Show a ProgressDialog and send an SMS in the background I have written a procedure that


shows a ProgressDialog
sends an SMS
updates the View from another thread
hides the ProgressDialog


But it seems to me that this is not the best way to do this. I would like to hear advice and my mistakes. It seems to me that it is possible to optimize the code for quick work procedures.

public boolean sendCommand(commandXDev command, final String uniValue) {
    SharedPreferences prefs = this.getSharedPreferences("serjaru.bsh.xdev2", Context.MODE_PRIVATE);
    final String phoneNumber = prefs.getString("phonenumber", "");
    int gateway = prefs.getInt("gateway", 0);

    if (gateway == 0) {


        if (command == commandXDev.Get) {
            final ProgressDialog progress = ProgressDialog.show(Relays.this, "Please wait", "Sending SMS...", true);
            progress.setCancelable(true);


            final Handler handler = new Handler();
            Runnable runnable = new Runnable() {

                public void run() {
                    if (myPhone.sendSMSMessage("+" + phoneNumber, ImeiPhone + "#get")) {
                        handler.post(new Runnable() {
                            public void run() {
                                statusLabel.setText("SMS sended!");
                            }
                        });
                    } else {
                        handler.post(new Runnable() {
                            public void run() {
                                statusLabel.setText("Error. SMS not sended.");
                            }
                        });
                    }
                    progress.dismiss();
                }
            };
            new Thread(runnable).start();
        }
    }
}
What you are trying to achieve is such a common pattern (run some command in separate thread and then modify the UI with the result) that Android has a class just for that: AsyncTask.  There is also a small example in the dev guide and I'm sure all Android books must have a full section, if not a full chapter, about AsyncTask.

Be very careful about Java conventions.  When you don't respect them, the code is quite confusing for Java developers.  


Class names always start a capital letter, so CommandXDev instead of commandXDev.
commandXDev.Get.  I'm not quite sure what that is.  In Java, if you write a getter method, you always call it get followed by what you are getting, i.e. getName(), getId(), getSharedPreferences(), etc.  Your commandXDev.Get seems to be a static getter, which is best avoided.  I'm not sure what you are trying to do.  Maybe look into the singleton pattern.


Minor point: in English it is "sent" instead of "sended".  It does not make sense, but it is how it is.