2013-02-20 113 views
0

我需要讓我的應用程序只有當用戶點擊re_b1按鈕時,它應該顯示進度Dialog,直到Button的進度完成(因爲有時該活動會凍結,直到進度完成)。另外,我想要一種方法,讓我避免應用程序凍結開始。進度對話框

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("[email protected]"); 

      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(); 


        } 


       } 
      }); 

     } 
    } 



} 

回答

0

Here is a good example of using progress dialog通常你會在AsyncTask利用這一點,但我沒有看到任何網絡的東西怎麼回事,這樣runOnUiThread可以爲你工作。如果使用AsyncTask,則可以在後臺執行幾秒鐘的操作,而不會佔用線程的UI。希望這可以幫助

+0

你能否更新您的問題並添加此代碼?比評論更容易閱讀 – codeMagic 2013-02-20 02:04:12