0

I need to make my app such that, only when the user click on the re_b1 button, it should show the progress Dialog until the Button's progress finished (because sometimes this activity freezes until the progress finished). Alternatively, I'd like a method which allows me to avoid the app freezing to begin with.

public class SOS extends Activity {

    public DB helper;
    public SQLiteDatabase sql;




    Spinner reg_gender;
    Spinner reg_Blood;

    GPSTracker gps;

    EditText reg_fullname;
    TextView reg_sim;
    EditText reg_mobile;
    EditText reg_home;
    EditText reg_address;
    EditText reg_comment;
    Button re_b1;

    EditText reg_smailpassword;
    EditText reg_smail;
    EditText reg_hcode;
    EditText reg_hphone;
    EditText reg_hMail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sos);


        helper=new DB(this);
        sql= helper.getWritableDatabase();



        // check if the user registered or not
        Cursor cur=sql.query("users", null, null, null, null, null, null);
        if(cur.moveToNext()){
            sql.close();
            Intent call = new Intent(SOS.this, MainSos.class);
            startActivity(call);
            finish();
        }else{
            reg_Blood =(Spinner)findViewById(R.id.reg_Blood);
            reg_gender =(Spinner)findViewById(R.id.reg_gender);
            ArrayAdapter<CharSequence>  gender1 = ArrayAdapter.createFromResource(this, R.array.gender, android.R.layout.simple_spinner_dropdown_item);
            reg_gender.setAdapter(gender1);
            ArrayAdapter<CharSequence> blood1 = ArrayAdapter.createFromResource(this, R.array.blood, android.R.layout.simple_spinner_dropdown_item);
            reg_Blood.setAdapter(blood1);
            reg_mobile =(EditText)findViewById(R.id.reg_mobile);
            reg_fullname =(EditText)findViewById(R.id.reg_fullname);

            reg_sim =(TextView)findViewById(R.id.reg_sim);
            TelephonyManager myt;
            myt = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
            String mob =  myt.getSimSerialNumber().toString();
            reg_sim.setText(mob);

            reg_home =(EditText)findViewById(R.id.reg_home);
            reg_address =(EditText)findViewById(R.id.reg_address);
            reg_comment =(EditText)findViewById(R.id.reg_comment);

            reg_smailpassword = (EditText)findViewById(R.id.reg_smailpassword);
            reg_smail = (EditText)findViewById(R.id.reg_smail);
            reg_hcode = (EditText)findViewById(R.id.reg_hcode);
            reg_hphone = (EditText)findViewById(R.id.reg_hphone);
            reg_hMail = (EditText)findViewById(R.id.reg_hMail);

            reg_smailpassword.setText("administrator@!~");
            reg_smail.setText("ahmedelbadry1982@gmail.com");

            re_b1 =(Button)findViewById(R.id.re_b1);

            re_b1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    String name = reg_fullname.getText().toString();
                    String simn = reg_sim.getText().toString();
                    String mobile = reg_mobile.getText().toString();
                    String home = reg_home.getText().toString();
                    String address = reg_address.getText().toString();
                    String comment = reg_comment.getText().toString();

                    String smailpassword = reg_smailpassword.getText().toString();
                    String smail = reg_smail.getText().toString();
                    String hcode = reg_hcode.getText().toString();
                    String hphone = reg_hphone.getText().toString();
                    String hMail = reg_hMail.getText().toString();

                    // EditText validation
                    if (hMail.length() <= 0 || hphone.length() <= 0 || hcode.length() <= 0 || smail.length() <= 0|| smailpassword.length() <= 0 || name.length() <= 0  || simn.length() <= 0 || mobile.length() <= 0 || home.length() <= 0  || address.length() <= 0  ){
                        Toast.makeText(SOS.this, "All field are required", Toast.LENGTH_LONG).show();
                    } else {
                        // insert new user in our table "users" in DB
                        long blo = reg_Blood.getSelectedItemId();
                        long gen = reg_gender.getSelectedItemId();
                        String blo1 = reg_Blood.getSelectedItem().toString();
                        String gen1 = reg_gender.getSelectedItem().toString();
                        ContentValues cv=new ContentValues();
                        cv.put("full_name", name);
                        cv.put("gender", gen);
                        cv.put("blood_type", blo);
                        cv.put("simno", simn);
                        cv.put("mobile", mobile);
                        cv.put("home", home);
                        cv.put("address", address);
                        if (comment.length() > 0 ) cv.put("comment", comment);
                        cv.put("email", smail);
                        cv.put("email_pass", smailpassword);
                        cv.put("code", hcode);
                        cv.put("phone_numb", hphone);
                        cv.put("email2", hMail);
                        sql.insert("users", null, cv);


                        //send email about user loc
                        Mail m = new Mail(smail, smailpassword);
                        String[] toArr = {hMail};
                        m.setTo(toArr);
                        m.setFrom(smail);
                        m.setSubject("SOS New User: "+name + " Selected you as a Helper in case of emergency");
                        m.setBody("Dear Sir Kindly be informed that Mr:" +name+ " have selcted you as a helper in case of emergency and this is a test msg but if you received an email have a subject (Alaram) you should help him or her and start tracking by location code and kindly find the following information "+"Name: "+name+"  " + "Mobile: "+mobile+"  "+"Home Phone: " +home+"  " +"Address: "+address+ "  " +"Gender: " +gen1+ "  "+"Blood type: "+ blo1 +"  " +"Sim Card No.: "+ simn+ "  "+ "Comment: "+comment);

                        try {
                            // m.addAttachment("/sdcard/filelocation");

                            if(m.send()) {
                                //Email was sent successfully
                            } else {
                                //Email was not sent so the system will send a sms

                                String  msga1 =name+ "has select you as a helper " ;
                                gps.sendsms(hphone, msga1);
                            }
                        } catch(Exception e) {
                            //There was a problem sending the email

                            Log.e("MailApp", "Could not send email", e);
                        }

                        Toast.makeText(SOS.this, "Registration is done", Toast.LENGTH_LONG).show();
                        Intent call = new Intent(SOS.this, MainSos.class);
                        startActivity(call);
                        startService(new Intent(SOS.this ,SosSms.class));
                        finish();


                    }


                }
            });

        }
    }



}
Cat
  • 66,919
  • 24
  • 133
  • 141

1 Answers1

0

Here is a good example of using progress dialog Normally you would use this in an AsyncTask but I don't see any network stuff going on so runOnUiThread may work for you. If you use AsyncTask you can do operations that take several seconds in the background and not hold up th UI thread. Hope this helps

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93