简体   繁体   English

无法暂停我的Android应用

[英]Fail to pause my android app

I would like my android app to pause for a while after receive the resond from the server. 我希望我的Android应用在收到服务器的回复后暂停一会儿。 I am making use of the following code to try to pause my program: 我正在使用以下代码来尝试暂停程序:

private Handler mHandler;
private Runnable mCountUpdater = new Runnable() {
    @Override
    public void run() {
        // TODO Auto-generated method stub
         mHandler.postDelayed(this, 15000); 
    } 
}; 

but no matter I put call the wait in either way mentioned below, it does not work 但是无论我以下述哪种方式致电等待,它都行不通
1. put inside the button handler: 1.放入按钮处理程序:

btnin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    //set the values
    grab.onPreExecute("TechID", Build.SERIAL);
    grab.onPreExecute("Type", "Checkin");
    //set the destination and send the request
    grab.execute(new String[]{"http://192.168.1.150/Me/check.php"});
    //close the activity
    mHandler = new Handler(); 
    mHandler.post(mCountUpdater);
    finish();
    }
});

2.put in the onPostExecute() method of AsynTask: 2.放入AsynTask的onPostExecute()方法:

private class GrabURL extends AsyncTask<String, Void, Void>{
//ArrayList object for storing the string pairs
ArrayList<NameValuePair> nameValuePairs;

public GrabURL() { 
    //constructor of the class
    nameValuePairs = new ArrayList<NameValuePair>(); 
} 

protected void onPreExecute(String key, String value) {
    //store the pair of values into the ArrayList 
    nameValuePairs.add(new BasicNameValuePair(key,value));
}

@Override
protected Void doInBackground(String... urls) {
      // TODO Auto-generated method stub
      //Operation being executed in another thread
      try{
          //set up the type of HTTPClient
          HttpClient client = new DefaultHttpClient();
          //set up the location of the server
          HttpPost post = new HttpPost(urls[0]);
          //translate form of pairs to UrlEncodedFormEntity 
          UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8);
          //set up the entity being sent by post method
          post.setEntity(ent);
          //execute the url and post the values
          //client.execute(post);
          HttpResponse responsePOST = client.execute(post); 
          HttpEntity resEntity = responsePOST.getEntity();
          line = EntityUtils.toString(resEntity);
    } catch (Exception e) {
        //catch the exception
        line = "Can't connect to server";
    }
    return null;
}

protected void onPostExecute(Void unused) {
        mRespond.setVisibility(View.VISIBLE); 
        mRespond.setText(line); 
        btnin.setVisibility(View.GONE);
        Toast.makeText(getApplicationContext(), "Value updated", Toast.LENGTH_SHORT).show();
        mHandler = new Handler(); 
        mHandler.post(mCountUpdater);   
}
}

Is it the way I used to pause the program is incorrect or I have put the wait function in a wrong position? 是我以前用来暂停程序的方式不正确,还是我将等待功能放在错误的位置?

I don't get what's the point to make a pause after you have received the response from the server. 收到服务器的响应后,我不知道要暂停一下了。 By the way, what do you mean exactly with "make a pause"? 顺便说一句,“停顿”到底是什么意思? I can't imagine any reason to pause an application. 我无法想象有任何理由暂停应用程序。

You may want to add a loader while (not after) the response from server is coming, and possibly, you may want to deactivate or hide buttons too. 您可能希望在服务器响应即将到来时(而不是之后)添加加载程序,并且可能还希望停用或隐藏按钮。 In that case, you should do that changes before grab.execute and remove changes in onPostExecute. 在这种情况下,您应该在执行grap.execute之前进行更改,并删除onPostExecute中的更改。

Moreover, I think that mCountUpdater is creating an infinite loop. 而且,我认为mCountUpdater正在创建一个无限循环。 So, I would rethink that code deeply. 因此,我将重新考虑该代码。 Take in to account, that Runnable.postDelay doesn't stop application by no means, it simply delays the execution of some Runnable by the specified time, but meanwhile, the application is still running. 考虑一下,Runnable.postDelay绝不会停止应用程序,它只是将某些Runnable的执行延迟了指定的时间,但与此同时,应用程序仍在运行。

I might help you more if you explain better the need of that solution. 如果您能更好地解释该解决方案的需求,我可能会为您提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM