【问题标题】:How to handle the onResume in web service call如何处理 web 服务调用中的 onResume
【发布时间】:2014-06-04 11:09:50
【问题描述】:

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

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

【问题讨论】:

  • 付款完成后,通过在 Screen1 的 onResume 中使用布尔标志来管理此调用异步。
  • 您的screen 1 是否在屏幕旋转时调用网络服务?
  • 支付完成后应该调用web服务吗?
  • @Neethu:谢谢你的想法。我是在此基础上做到这一点的。谢谢

标签: android web-services android-asynctask


【解决方案1】:

查看以下 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();
            }
        });
    }

ma​​in.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>

【讨论】:

  • @Anirudhha:感谢您的回复。我的应用只能在纵向模式下运行。我已经使用标志变量做到了这一点。谢谢
猜你喜欢
  • 2013-11-09
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
相关资源
最近更新 更多