2
\$\begingroup\$

I have written a procedure that

  1. shows a ProgressDialog
  2. sends an SMS
  3. updates the View from another thread
  4. 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();
        }
    }
}
\$\endgroup\$
0
3
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.