没有按下按钮的AsyncTask或进度条

时间:2014-08-15 23:35:26

标签: android android-asynctask android-progressbar

我想在此程序中使用asynctask或进度条。我可以在此程序中使用Asynctask或Progress Bar而无需按任何按钮吗?

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    // Creating new JSON Parser
    JSONParser jParser = new JSONParser();
    // Getting JSON from URL
    JSONObject json = jParser.getJSONFromUrl(url);
    try {
      // Getting JSON Array
      user = json.getJSONArray(TAG_USER);
      JSONObject c = user.getJSONObject(0);
      // Storing  JSON item in a Variable
      String id = c.getString(TAG_ID);
      String name = c.getString(TAG_NAME);
      String email = c.getString(TAG_EMAIL);
      //Importing TextView
      final TextView uid = (TextView)findViewById(R.id.uid);
      final TextView name1 = (TextView)findViewById(R.id.name);
      final TextView email1 = (TextView)findViewById(R.id.email);
      //Set JSON Data in TextView
      uid.setText(id);
      name1.setText(name);
      email1.setText(email);
  } catch (JSONException e) {
    e.printStackTrace();
  }
    }
}

2 个答案:

答案 0 :(得分:0)

<强> What you are doing :: 您正在network request中执行UI-thread(i:e-onCreate)


What you should do ::

  • 执行时间较长(eg: network request)的任务必须在 separate thread
  • 为此使用Async-Task

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /* CALL THE ASYNC TASK*/
        new DownloadTask().execute();
}

private class DownloadTask extends AsyncTask<String, Void, Void> {
    private ProgressDialog pDialog; 

    /* Start the progress in onPreExecute */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
         pDialog = new ProgressDialog(getApplicationContext()); 
            pDialog.setMessage("Downloading Image ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
    }

    /* Do the background task in do in background*/
    @Override
    protected Void doInBackground(String... params) {

       /** ADD your code here **/

        // Creating new JSON Parser
        JSONParser jParser = new JSONParser();
        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        try {
              // Getting JSON Array
              user = json.getJSONArray(TAG_USER);
              JSONObject c = user.getJSONObject(0);
              // Storing  JSON item in a Variable
              String id = c.getString(TAG_ID);
              String name = c.getString(TAG_NAME);
              String email = c.getString(TAG_EMAIL);
              //Importing TextView
              final TextView uid = (TextView)findViewById(R.id.uid);
              final TextView name1 = (TextView)findViewById(R.id.name);
              final TextView email1 = (TextView)findViewById(R.id.email);
              //Set JSON Data in TextView
              uid.setText(id);
              name1.setText(name);
              email1.setText(email);
          } catch (Exception e) {
            e.printStackTrace();
          } 
        return Void;
    }

    /**
    * After completing background task Dismiss the progress dialog
    * **/
    protected void onPostExecute(Void Void) {
        pDialog.dismiss();
    }
}

答案 1 :(得分:-1)

是。许多新程序员认为显示ProgressBar是一项非常简单的任务,这是很常见的。它通常不像“看起来应该如此”那么容易。

也就是说,您需要在后台任务/线程中运行所有网络调用(例如从URL获取数据)。如果要显示进度条,则需要在完成后在UI线程上引用它。

许多人发现AsyncTask的便利性,使用它的“postExecute”方法就可以了。我在这里添加一些其他关键字,以帮助您找到适合您的问题的答案,因为我很少使用AsyncTask。当我第一次开始这样做时,似乎比应该更难(尽管我错了)。