0

サーバーから応答を受信した後、Androidアプリをしばらく一時停止したいと思います。次のコードを使用して、プログラムを一時停止しようとしています。

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

しかし、以下のいずれかの方法で待機を呼び出しても、機能しません
。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. 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);   
}
}

プログラムを一時停止する方法が間違っているのですか、それとも待機機能を間違った位置に置いているのですか?

4

1 に答える 1

1

サーバーからの応答を受け取った後、一時停止する意味がわかりません。ちなみに、「一時停止」とはどういう意味ですか?アプリケーションを一時停止する理由は想像できません。

サーバーからの応答が来ている間(後ではなく)にローダーを追加したい場合があります。また、ボタンを非アクティブ化または非表示にしたい場合もあります。その場合、grab.executeの前にその変更を行い、onPostExecuteの変更を削除する必要があります。

さらに、mCountUpdaterは無限ループを作成していると思います。それで、私はそのコードを深く考え直します。Runnable.postDelayは決してアプリケーションを停止するのではなく、指定された時間だけRunnableの実行を遅らせるだけですが、その間、アプリケーションは実行されていることを考慮に入れてください。

あなたがその解決策の必要性をよりよく説明するならば、私はあなたをもっと助けるかもしれません。

于 2012-07-30T13:33:33.497 に答える