Usage of Threads in "AddEvidenceORCarImageNotes" of my JOR Project sampleCode

//onclick for add notes done button...Thread called from Here
    public void notesDonebtn(View v)
    {
        SharedVariables.imm.hideSoftInputFromWindow(notesDoneBtn.getWindowToken(), 0);
        ps = Pattern.compile(".*\\w.*");
        ms = ps.matcher(addnotesText.getText().toString());
        bs = ms.matches();
        if(addnotesText.getText().toString().length()>0 && bs==true)
        {           
            SharedVariables.addnotesText=addnotesText.getText().toString();
            if (SharedVariables.checkInternetConnection(AddEvidenceORCarImageNotes.this)) {
                progressDialog = ProgressDialog.show(
                        AddEvidenceORCarImageNotes.this, "",
                        "Uploading Vehicle image...", true);
                try {                   
                        startThread();                   
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                showAlert(SharedVariables.internetErrorMsg);
            }
           
        }

public void startThread()
    {
        Thread backgroundthread =new Thread( new Runnable()
        {
            public void run()
            {
                //AddNotes Web service calling...
                        clsCredentials crediantials=new clsCredentials(AddEvidenceORCarImageNotes.this.getString(R.string.TokenId),SharedVariables.userName,SharedVariables.password);       
                        clsNotes notesObj=new clsNotes(0, caseId, 0, addnotesText.getText().toString(),SharedVariables.date());       
                        clsAddNotesRequest clsaddnoteReq=new clsAddNotesRequest(crediantials,notesObj);       
                        wsAddNotes wsAddnote=new wsAddNotes();       
                        int noteId=wsAddnote.addNotes(clsaddnoteReq);
                        if(noteId>0)
                        {
                            notesObj.writeNotesToDBWithId(noteId);
                        }           
                        responceHandler.sendEmptyMessage(0);
            }
            });backgroundthread.start();
    }   

private Handler responceHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (progressDialog != null) {
                if (progressDialog.isShowing()) {
                    Intent in = new Intent();
                    setResult(3,in);//Here I am Setting the Requestcode 1, you can put according to your requirement
                    finish();
                    progressDialog.dismiss();
                }
            }
            if (msg.what == 0) {
                AddEvidenceORCarImageNotes.this.finish();
                /*Intent in = new Intent();
                setResult(3,in);//Here I am Setting the Requestcode 1, you can put according to your requirement
                finish();*/
            }
        }
    };

Comments

  1. here if u use Tablets,then u get Error at line
    progressDialog = ProgressDialog.show(
    AddEvidenceORCarImageNotes.this, "",
    "Uploading Vehicle image...", true);

    so replace above lines with belowcode as follows:
    progressDialog = ProgressDialog.show(
    getActivity(), "",
    "Updatin data...", true);

    ReplyDelete
  2. ////One more important thing u need to remind,We cannot handle(update) UserInterface Views in public void run(){}
    method..so that code should be written in responseHandler of Thread...

    ReplyDelete

Post a Comment

Popular posts from this blog

Custom Dialog with no title and full screen in Android

code to display .pdf,.text,.DOC,.DOCX,.doc extension files exists in SDCard

Best working code to set Date limits on Date Picker Dialog,in Android