0

我目前正在开发一个应用程序,它在活动结束时计算一些简单的变量,然后在下一个活动开始时将它们上传到服务器:

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


}

@Override
   public void onResume(){
        super.onResume();
        is_active = true;
        Log.d(TAG, "onResume enter FA");
        //Download Variable Ids start
        int WeightId = postmeasurements.getStatisticId(postmeasurements.mHCAW);     
        int BFPId = postmeasurements.getStatisticId(postmeasurements.mHCABFP);
        int HydId = postmeasurements.getStatisticId(postmeasurements.mHCAHyd);
        int MuscMass = postmeasurements.getStatisticId(postmeasurements.mHCAMM);
        //Download Variable Ids End
        Log.w("ANTApp", "WeightId is:  " + WeightId);
        Log.w("ANTApp", "BFPId is:  " + BFPId);
        Log.w("ANTApp", "HydId is:  " + HydId);
        Log.w("ANTApp", "MuscMass is:  " + MuscMass);
        //Post Measurements to Server Start
        postmeasurements.postScaleMeasurements(WeightId, PostMeasurements.Weight);
        postmeasurements.postScaleMeasurements(BFPId, PostMeasurements.BFP);
        postmeasurements.postScaleMeasurements(HydId, PostMeasurements.Hyd);
        postmeasurements.postScaleMeasurements(MuscMass, PostMeasurements.MuscMass); 
        //Post Measurements to Server End
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = 100 / 112.0f;
        getWindow().setAttributes(lp);

        setTimer();

        Log.d(TAG, "onResume exit FA");
   }

我在这里遇到的问题是,这会导致每个活动之间出现黑屏,直到上传所有测量值。每个下载和上传方法都使用 Httppost 在自己的后台线程上运行:

public void postScaleMeasurements(int StatId, double Value){
   //int Id = 0;
   String result = "";
    //the serial data to send
     final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
     nameValuePairs.add(new BasicNameValuePair("s", mDeviceId));
     nameValuePairs.add(new BasicNameValuePair("statisticId", Integer.toString(StatId)));
     nameValuePairs.add(new BasicNameValuePair("userId", Integer.toString(mHCAId)));
     nameValuePairs.add(new BasicNameValuePair("statisticValue", Double.toString(Value)));
     InputStream is = null;

     Thread PostScaleData = new Thread(){
         public void run(){
             try{
                 HttpClient httpclient = new DefaultHttpClient();
                 HttpPost httppost = new HttpPost("https://www.website.com/app/postStatisticValue.php");
                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                 HttpResponse response = httpclient.execute(httppost);
                 HttpEntity entity = response.getEntity();
                 streamresponse = entity.getContent();
             }catch(Exception e){
                 Log.e(TAG, "Error in http connection (verifytablet) "+e.toString());
             }
         }
     };

     PostScaleData.start();
     try {
        PostScaleData.join(10000);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

我仍然收到大约 1-4 秒的黑屏。我真正想要的是在上传任何测量值之前完成布局绘图过程。

提前感谢大家的大力支持。

4

1 回答 1

0

在 Worker Thread 中编写您的网络上传工作,最好使用AsyncTask

于 2013-08-17T05:08:04.117 回答