簡體   English   中英

如何掩蓋響應來自android中的服務器而不顯示黑屏的時間?

[英]How to cover up the time taken by response to come from the server in android without showing black screen?

我有一個請求響應活動,其中來自服務器的響應用於膨脹動態布局,我的問題是第一個請求來得快,第二個請求有一個延遲期間所以有時屏幕變黑。我已經嘗試過此鏈接 ,但它沒有多大幫助

以下是我現在使用的代碼。

 ClientResource clientResource=new ClientResource(IP.IP+"/api/device/view_device/"+r_no);
       // ClientResource clientResource=new ClientResource("http://10.30.10.243:8080/api/device/view_device/101");

        clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC,username, password);
        clientResource.get();
        Response response= clientResource.getResponse();
        System.out.println("Response from server is ----> "+response.getEntityAsText());
        text=response.getEntityAsText().toString();
        try {
            JSONObject obj = new JSONObject(text);
            JSONArray jArray = new JSONArray((String) obj.get("message"));
            System.out.println("\n String  is\t"+jArray+"\t\t");
            len=jArray.length();
            System.out.println("Length of Array is\t"+len);
            for (int i=0;i<len;i++){
                String dname=jArray.getJSONObject(i).getString("device_name").toUpperCase();
                 port= (String) jArray.getJSONObject(i).get("device_port");
                Rno=jArray.getJSONObject(i).getString("room_no");
                 dev_id=(String)jArray.getJSONObject(i).getString("device_id");
                String d_stat=(String)jArray.getJSONObject(i).get("device_control");
                cList.add(d_stat);
                list.add(dname);
                lis.add(port);
                stat.add(dev_id);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        if (len==0){Toast.makeText(Test.this, "No Devices Found For This Room", Toast.LENGTH_SHORT).show();
           /* Intent intent1=new Intent(Test.this,Landing_page.class);
            finish();
            startActivity(intent1);
*/
            LinearLayout r1 = new LinearLayout(this);
            r1.setBackgroundResource(R.drawable.border);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams
                    (LinearLayout.LayoutParams.MATCH_PARENT,90);
            r1.setPadding(0,10, 0, 0);
            r1.setLayoutParams(lp);

            TextView tv1 = new TextView(this);
            tv1.setTextColor(getResources().getColor(R.color.black));
            tv1.setText(" No Devices Found");
            tv1.setTextSize(30);
            tv1.setPadding(10,10,0,0);
            LinearLayout.LayoutParams tv1params = new LinearLayout.LayoutParams
                    (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            tv1.setLayoutParams(tv1params);
            tv1.setGravity(View.TEXT_ALIGNMENT_CENTER);
            r1.addView(tv1);

            ll1.addView(r1);

        }
   try{
           ClientResource clientResource1=new ClientResource(IP.IP+"/api/resource/device_status/"+r_no+"/"+dev_id);



    System.out.println(IP.IP+"/api/resource/device_status/"+r_no+"/"+dev_id);

           //clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC, username, password);
           clientResource1.get();
           Response response1= clientResource1.getResponse();
           System.out.println("Response from status server is ----> "+response1.getEntityAsText());
           String txt=response1.getEntityAsText().toString();


           try {
               JSONObject obj = new JSONObject(txt);
               JSONArray jArray = new JSONArray((String) obj.get("message"));
               System.out.println("\n String  is\t"+jArray+"\t\t");
               len1=jArray.length();
               System.out.println("Length of Second Array is\t"+len1);
               for (int i=0;i<len1;i++){
                   String dstat=jArray.getJSONObject(i).getString("Soft_status");
                   String istat=jArray.getJSONObject(i).getString("Inten_value");
                   list1.add(dstat);
                   list2.add(istat);
               }
           } catch (JSONException e) {
               e.printStackTrace();
           }}catch(Exception e){e.printStackTrace();}
       /* if (len1==0){
            progress.dismiss();
            Toast.makeText(getApplicationContext(),"Device Status not Obtained",Toast.LENGTH_LONG);
            list1.add("0");list1.add("0");list1.add("0");list1.add("0");list1.add("0");list1.add("0");list1.add("0");list1.add("0");
            list2.add("2");list2.add("2");list2.add("2");list2.add("5");list2.add("2");list2.add("2");list2.add("2");list2.add("2");
        }else{
            progress.dismiss();

            Toast.makeText(getApplicationContext(),"Devices Discovered",Toast.LENGTH_LONG);
        }

*/
        if (len1>0&&len>0){
            progress.dismiss();
        }

        System.out.println(cList.toString().toUpperCase());

      /*  LinearLayout.LayoutParams.WRAP_CONTENT*/
        for (int i = 0; i <list.size(); i++) {

            cnid=stat.get(i);

                LinearLayout layout = new LinearLayout(this);
            layout.setBackgroundResource(R.drawable.border);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                    (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
           // params.setMargins(100, 15, 50, 0);

            layout.setLayoutParams(params);

            layout.setOrientation(LinearLayout.HORIZONTAL);

            layout.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
            layout.setPadding(30, 10, 0, 30);

                System.out.println(list);


            ImageView iv[];
            iv=new ImageView[len];
            iv[i]=new ImageView(this);
          String buffer=list.get(i);
            if (list1.get(i).equals("1")){
                flag=1;
                iv[i].setImageResource((Integer)dm.get(""+buffer.replace(" ","").toUpperCase()+""));
            }else if (list1.get(i).equals("0")){ flag=0;
         iv[i].setImageResource((Integer) hm.get(""+buffer.replace(" ","").toUpperCase()+""));}
            iv[i].setId(i+100);
            iv[i].setTag(list);
             LinearLayout.LayoutParams ivparams = new LinearLayout.LayoutParams
            (60, LinearLayout.LayoutParams.MATCH_PARENT);
            iv[i].setPadding(10,0,0,0);
         // ivparams.setMargins(0,0,0,0);
                iv[i].setLayoutParams(ivparams);
            if(list.get(i).equals("BULB")||list.get(i).equals("LED1")||list.get(i).equals("FAN"))
           {
               String d=list.get(i);
               System.out.println("\n\t\t:: Device Controllable ::" +i+
                       "\n");
                iv[i].setOnClickListener(HandleOnClick(d,iv[i],iv[i].getId()));
           }else if (list.get(i).equals("CFL")||list.get(i).equals("LED")){
                System.out.println("Device not Controllabe:: "+i);
            }

            layout.addView(iv[i]);


            TextView tv = new TextView(this);
            LinearLayout.LayoutParams tvparams = new LinearLayout.LayoutParams
                    (90, LinearLayout.LayoutParams.WRAP_CONTENT);
            tv.setLayoutParams(tvparams);
            tv.setPadding(27,27,27,27);                 //tv.setPadding(27,27,27,27);
            tvparams.setMargins(30, 0, 0, 0);
            tv.setTextSize(15);
           tv.setTextColor(getResources().getColor(R.color.black));
            //tv.setPadding(10,10,10,10);


            tv.setText(list  .get(i));
                layout.addView(tv);



            ToggleButton tb[];
            tb=new ToggleButton[len];
            tb[i]=new ToggleButton(this);

            LinearLayout.LayoutParams tbparams = new LinearLayout.LayoutParams
                    (60, 40);
            tbparams.setMargins(200, 5, 0, 0);
            tb[i].setBackgroundResource(R.drawable.bgtoggle);
               tb[i].setId(i);
            tb[i].setTextOff("off");
            tb[i].setTextOn("on");
            tb[i].setTextColor(getResources().getColor(R.color.transparent));
            tb[i].setTag(list.get(i));
            //tb[i].setPadding(4, 4, 4, 4);
            tb[i].setLayoutParams(tbparams);
           if (list1.get(i).equals("1")){
                tb[i].setChecked(true);
            }else if(list1.get(i).equals("0")) {
               flag=0;
               tb[i].setChecked(false);
           }

    tb[i].setOnCheckedChangeListener(handleOnClick(
            tb[i], tb[i].getId(),tb[i].getTag(),list ,lis,stat,iv[i],iv[i].getTag()));
            layout.addView(tb[i]);

            //Image View Loop
            ImageView Iv[];
            Iv=new ImageView[len];
            Iv[i]=new ImageView(this);
            Iv[i].setId(i+1);
              //ImageView Iv = new ImageView(context);
              Iv[i].setImageResource(R.drawable.enrgy);
              LinearLayout.LayoutParams Ivparams = new LinearLayout.LayoutParams
                      (60, 50);
              Ivparams.setMargins(150, 25, 0, 0);
              Iv[i].setLayoutParams(Ivparams);
            Iv[i].setOnClickListener(handleOnClick(Iv[i],r_no,dev_id,Iv[i].getId(),cntr,tb[i].getId()));
              layout.addView(Iv[i]);


            ll1.addView(layout);

        }

請指導。

您可以使用異步任務來做到這一點......

private class ClassName extends AsyncTask<Void, Void, Void>
{
     ProgressDialog pd = new ProgressDialog(SignUp.this);

     @Override
     protected void onPreExecute()
     {
            pd.setMessage("Please wait..");
            pd.setCancelable(false);
            pd.show();
   }

   @Override
   protected Void doInBackground(Void... arg0)
   {
    ..................................
    //your code for json calling -- Server call 1
    ..................................
    //use runOnUi thread to update ui
    ..................................

     runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // WORK on UI thread here
            }
        });

    ..................................
    //your code for json calling -- Server call 2
    ..................................
    //use runOnUi thread to update ui
    ..................................

    runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // WORK on UI thread here
            }
        });



       return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
      super.onPostExecute(result);

      if(pd.isShowing())
         pd.dismiss();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM