亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何重復發布和接收數據但使應用程序仍然可響應編輯

如何重復發布和接收數據但使應用程序仍然可響應編輯

MM們 2023-07-19 10:39:12
我需要重復將數據發布到 php 并取回它們,但使應用程序仍然可響應。我的代碼可以工作,但一段時間后(大約 1 分鐘)我的應用程序停止響應。起初,我嘗試將 POST() 函數添加到 onResponse() 函數(onRespose 調用 POST 函數,一次又一次地發出新請求),但這凍結了我的應用程序,所以我添加了每 1 毫秒調用一次函數的計時器...編輯后,我的應用程序與以前一樣工作(僅在連接到電腦時才有效),如果我打開應用程序并且我的手機未連接到電腦,應用程序會在 1 分鐘后崩潰,所以我仍然遇到與開始時相同的問題。如果有人發現錯誤請告訴我哪里。。..我編輯的代碼:.MainActivity.javapublic class MainActivity extends AppCompatActivity implements Sync.CallBack {    final String URL = "***";    final String KEY = "***";    Data data;    RequestQueue requestQueue;    TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        data = new Data();        data.key = KEY;        data.pressed = "0";        textView = findViewById(R.id.status);        requestQueue = Volley.newRequestQueue(this);        StartSync();    }    public void btnClick(View view) {        data.pressed = "1";    }    @Override    public void onCallBack(String data) {        textView.setText(data);    }    public void StartSync(){        Sync thread = new Sync(this,data,this);        thread.start();    }}Sync.javapublic class Sync extends Thread{    private Context context;    boolean wait = false;    final String URL = "***";    public CallBack listener;    RequestQueue requestQueue;    public Data data;    public interface CallBack{        void onCallBack(String data);    }    public Sync(CallBack listener,Data data,Context context){        this.listener = listener;        this.data = data;        this.context = context;        requestQueue = Volley.newRequestQueue(context);    };    @Override    public void run() {        while(true){            wait = true;            POST();            while(wait){}        }    }
查看完整描述

1 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

好吧,我可以給你一個如何實現這一點的例子,但你確實需要研究我將使用的東西。


創建線程來發送和接收數據


 public class myReceiveThread extends Thread{

        public gotNewData listener;


        //Create an interface to pass data to Activity

        public interface gotNewData(){

            void gotNewDataToDisplay(String data); //or int or data or what ever.

        }


        //CTor

        public myThread(gotNewData listener){

            this.listener = listener;

        }


        @Override

        public void run(){

            while(myAppisRunnung == true){




                //Receive Data

                listener.gotNewDataToDisplay("New Data");

            }

        }

        }

在主要活動中:


//In Main activity

public class MainActivity extends AppCompactActiity implements gotNewData{


//what ever


public void startThread(){

     myReceiveThread thread = new myReceiveThread(this);

     thread.start();

}


@Override

public void gotNewDataToDisplay(String data){

    someTextView.setText(data);

  }

}

創建發送線程


public class mySendingThread extends Thread{

private BlockingQueue<String> toSendMessages= new BlockingQueue<>();


Public mySendingThread (BlockingQueue<String> toSendMessages){

this.toSendMessages = toSendMessages;

}


@Override

        public void run(){

            while(myAppisRunnung == true){

             String message= toSendMessages.take();

//Send message

}

}

}

在主要活動中


public void startSendThread(){

     mySendingThread threadSend = new mySendingThread(MessageQueue);

     thread.start();

}

在 mainActivity 中,您需要一個BlockingQueue<String> MessageQueue 并且可以添加您想要的每條消息。


查看完整回答
反對 回復 2023-07-19
  • 1 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號