开发者

Stop a thread if dont receive any response in Android?

i have a progressDialog and i connect to a webservice. While i dont receive the response, i show the progressDialog but if i dont receive nothing, the progressDialog dont close.

progressDialog = ProgressDialog.show(ProfilesMenu.this, "", Utils.CONTACT_WEBSERVICE, true);
Thread thread = new Thread(ProfilesMenu.this);
thread.start();

@Override
public void run() {
    linkToWebService = new Utils();
    T_VERSION version = new T_VERSION(Utils.MTA, Utils.STA);
    try {
        // é envio o nº de tlm do item selecionado para verificar o seu status
        Vector<T_COMMAND_OUT> command =  linkToWebService.getCardInfoDemand(version, "0", 
                profilesArrayList.get(menuInfoPosition).getPhoneNumber());

        responseWebservice = command.get(0);
        handler.sendEmptyMessage(0);
    } catch(Exception e) {

    }
}

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        progressDialog.dismiss();

        if(responseWebservice.getid()==Utils.CARD_INFO_RESPONSE_ID){
            if(responseWebservice.getLocked().equals(Utils.UNLOCKED)){

                profilesArrayList.get(menuInfoPosition).setCalypsoNumber(responseWebservice.getCalypsoNumber());
                Log.d("calypsoNumber", profilesArrayList.get(menuInfoPosition).getCalypsoNumber());
                if(profilesArrayList.get(menuInfoPosition).getCalypsoNumber()!="")
                    profilesArrayList.get(menuInfoPosition).setState(true);
                SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES_PROFILES_NAME+getProfilesCounter(), MODE_PRIVATE);
                SharedPreferences.Editor prefEdit = preferences.edit();
                prefEdit.putString(CALYPSO_NUMBER, profilesArrayList.get(menuInfoPosition).getCalypsoNumber());
                prefEdit.commit();

                createAlertDialog("Status of Profile", "This phone number is unlocked!").setIcon(R.drawable.right_icon).show();
            }else if (responseWebservice.getLocked().equals(Utils.LOCKED)) {
                createAlertDialog("Status of Profile", "This phone number is locked! You have to active it " +
                "for buy tickets").setIcon(R.drawable.wrong_icon).show();
            }
        } else if(responseWebservice.getid()==Utils.ERROR_MESSAGE_ID){
            createAlertDialog(responseWebservice.getPage().getTemplateRef(), "Data not recognized by webservice.").setIcon(R.drawable.wrong_icon).show();
        }
    }
};

I try to stop thread with the .sleep 开发者_开发知识库but didn't work. How i can stop thread if i dont receive nothing in the ruin() ?? thanks


Consider calling Log.d in catch to discover why the call is not completing.

Then consider wrapping the exception in a message. Use message.what(ERROR_COMMAND) where ERROR_COMMAND is 1 and wrapping the exception description in the message as an immutable value using message.setData. Then switch on what in your handler trapping for what SUCCESS_COMMAND value 0 or ERROR_COMMAND value 1.

IMHO, you should sometimes use asyncTask and sometimes use message based concurrency. Use the technique that works best for the requirements and your level of understanding of handlers vs generics.


I would suggest instead of Thread use AsyncTask. It makes your life easier, it gives you lots of flexibility to do bulk operations in another thread and when result is available you can update it in UI thread. Here is quick example:

public class WebOperation extends AsyncTask<Void, Void,Void> {

    private final ProgressDialog mProgressDialog;
    public WebOperation(Context context){
        mProgressDialog = new ProgressDialog(context);
        mProgressDialog.setMessage("Your Message");
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog.show();//show your prorgress dialog
    }
    @Override
    protected Void doInBackground(Void... params) {
        // Do Network Related Operation
        return null; //return result
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        //runs on UI Thread
        mProgressDialog.dismiss();
    }

}

And you can publish progress to ProgressDialog by using publishProgress and onProgressUpdate methods. Here is a refrence of AsyncTask

http://developer.android.com/reference/android/os/AsyncTask.html

Hope this help in solving your progressDialog problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜