如何在Web服务调用中处理onResume

时间:2014-06-04 11:09:51

标签: android web-services android-asynctask

我正在处理基于付款的申请。所以在这种情况下,在第一个屏幕中,用户需要selectproduct项并移动到下一个屏幕,在屏幕2中他必须选择payment方法 现金或卡。在屏幕一中,商店名称从Web服务显示。我处理过了 在同步任务中。如果他从屏幕2中选择现金模式,付款将获得authorized和 显示respective message

但是,如果他想从screen(screen 1)遗嘱返回产品screen 2,screen 1 不再调用Web服务。它应该是一样的。但如果paymentcompleted它 将被刷新。我该如何实现这一目标?我是否在Resume method上提到了整个操作。

1 个答案:

答案 0 :(得分:0)

请通过Commonsware查看以下示例,希望这对您有所帮助。

public class RotationAsync extends Activity {
  private ProgressBar bar=null;
   RotationAwareTask task=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    bar=(ProgressBar)findViewById(R.id.progress);

    Button next = (Button) findViewById(R.id.next);

    next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i = new Intent (RotationAsync.this, First.class);
            startActivity(i);
        }
    });

    task=(RotationAwareTask)getLastNonConfigurationInstance();

    if (task==null) {
      task=new RotationAwareTask(this);
      task.execute();
    }
    else {
      task.attach(this);
      updateProgress(task.getProgress());

      if (task.getProgress()>=100) {
        markAsDone();
      }
    }
  }




@Override
  public Object onRetainNonConfigurationInstance() {
    task.detach();

    return(task);
  }

  void updateProgress(int progress) {
    bar.setProgress(progress);
  }

  void markAsDone() {
    findViewById(R.id.completed).setVisibility(View.VISIBLE);
  }

  static class RotationAwareTask extends AsyncTask<Void, Void, Void> {
    RotationAsync activity=null;
    int progress=0;

    RotationAwareTask(RotationAsync activity) {
      attach(activity);
    }

    @Override
    protected Void doInBackground(Void... unused) {
      for (int i=0;i<20;i++) {
        SystemClock.sleep(500);
        publishProgress();
      }

      return(null);
    }

    @Override
    protected void onProgressUpdate(Void... unused) {
      if (activity==null) {
        Log.w("RotationAsync", "onProgressUpdate() skipped -- no activity");
      }
      else {
        progress+=5;
        activity.updateProgress(progress);
      }
    }

    @Override
    protected void onPostExecute(Void unused) {
      if (activity==null) {
        Log.w("RotationAsync", "onPostExecute() skipped -- no activity");
      }
      else {
        activity.markAsDone();
      }
    }

     void detach() {
      activity=null;
    }

    void attach(RotationAsync activity) {
      this.activity=activity;
    }

    int getProgress() {
      return(progress);
    }
  }
}

<强> First.java

public class First extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);

        Button b = (Button) findViewById(R.id.button1);

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                finish();
            }
        });
    }

<强> main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <ProgressBar android:id="@+id/progress"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
  />
  <TextView android:id="@+id/completed"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Work completed!"
      android:visibility="invisible"
  />

  <Button
      android:id="@+id/next"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Next" />

</LinearLayout>

<强> first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Back" />

</LinearLayout>