gpt4 book ai didi

java - 当我离开 Activity 并返回时,Activity 不断重启

转载 作者:行者123 更新时间:2023-11-29 02:32:06 24 4
gpt4 key购买 nike

我有两个 Activity ,当我从 Activity A 移动到 B 时,B 不断重启或“刷新”,当我从 B 返回到 A 时,它也不断重启。代码非常大,我在这里发布我认为问题原因所在的区域:

  Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
deviceStatus();

try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

}

});
t.start();

这是 deviceStatus();

  public void deviceStatus(){
try {
RequestQueue requestQueue = Volley.newRequestQueue(InActivate.this);
String URL = "http://gickuwait-dev.com/electionapi/api/DeviceStatus";
JSONObject jsonBody = new JSONObject();

jsonBody.put("device_PK", device_ID2);


final String requestBody = jsonBody.toString();

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {



if(response.equals("true")){

Intent intent = new Intent(InActivate.this, Vote.class);
startActivity(intent);
finish();

}else if(response.equals("false")) {


}
// Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_SHORT).show();

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

Log.e("VOLLEY", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}

@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}

@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {

String responseString;
String json = null;

try {
json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
responseString = String.valueOf(json).trim();
ArrayList<DeviceStatusResponse> list = new ArrayList<DeviceStatusResponse>();
Type listType = new TypeToken<List<DeviceStatusResponse>>() {}.getType();
list = new Gson().fromJson(responseString, listType);

device_Status = list.get(0).getIsActive().toString();
// Toast.makeText(getApplicationContext(), ""+device_Status+" null ", Toast.LENGTH_LONG).show();
return Response.success(device_Status, HttpHeaderParser.parseCacheHeaders(response));
}
};

requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

在 Activity B 中,我有相同的代码来检查数据库中的设备状态,我们将不胜感激

最佳答案

您可以使用 Handle 来检查重复的任务。

     private Handler delayHandler = new Handler();
private Runnable runnable = new Runnable() {
@Override
public void run() {
deviceStatus();
driverDelayHandler.postDelayed(runnable, 1000);

}
};

不要忘记在 onStop 方法上取消。

 delayHandler.removeCallbacks(runnable);

关于java - 当我离开 Activity 并返回时,Activity 不断重启,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49110850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com