[英]How to run specific methods infinite time in background per 5 seconds in android?
I am developing a Vaccine Viewer app and I want to implement a function that run till vaccine get available.我正在开发一个疫苗查看器应用程序,我想实现一个 function 运行直到疫苗可用。
Here is my vaccine checker method这是我的疫苗检查方法
boolean checkVaccine(String pinCode,String data){
isVaccineAvailable = false;
String url = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode="+pinCode+"&date="+data;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray dataArray = response.getJSONArray("centers");
if(dataArray.length()==0){
Toast.makeText(MainActivity.this,"No Center Available",Toast.LENGTH_SHORT).show();
}
for(int i=0;i<dataArray.length();i++){
JSONObject centers = dataArray.getJSONObject(i);
String centerName = centers.getString("name");
String centerAddress = centers.getString("address");
String vaccinationStartTime = centers.getString("from");
String vaccinationEndTime = centers.getString("to");
String vaccinePrice = centers.getString("fee_type");
JSONObject sessions = centers.getJSONArray("sessions").getJSONObject(0);
int minimumAge = sessions.getInt("min_age_limit");
String vaccineName = sessions.getString("vaccine");
int totalVaccineAvailable = sessions.getInt("available_capacity");
if(totalVaccineAvailable>0){
isVaccineAvailable = true;
}
vaccinationData addData = new vaccinationData(centerName,
centerAddress,vaccinationStartTime,vaccinationEndTime,
vaccinePrice,vaccineName,totalVaccineAvailable,minimumAge);
dataList.add(addData);
}
vaccinationDataAdapter adapter = new vaccinationDataAdapter(MainActivity.this,dataList);
recyclerView.setAdapter(adapter);
}catch (JSONException e){
Toast.makeText(MainActivity.this,"Unable To Get Data From Server.",Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,"Unable To Get Data From Server.\nMake Sure Your Internet Connection ON.",Toast.LENGTH_SHORT).show();
}
});
queue.add(request);
return isVaccineAvailable;
}
I want to run above methods till function not return true.我想运行上述方法,直到 function 不返回 true。 I was searched but I am unable to understand what exact I have to use?
我被搜查了,但我无法理解我必须使用什么? And How?
如何?
There are Handler, TimerTask, Runnable to repeat a task after few seconds and I got Job Scheduler but that need api <=21 And I want to give support for <=18, but I am not getting How to do this in background?有Handler,TimerTask,Runnable几秒钟后重复一个任务,我得到了 Job Scheduler,但需要 api <=21 我想支持 <=18,但我没有得到如何在后台执行此操作?
I tried below Code in MainActivity, but after calling that app get stuck and restart.我在 MainActivity 中尝试了下面的代码,但在调用该应用程序后卡住并重新启动。 I am beginner in android development, I dont know much more about service, and broadcast receiver.
我是 android 开发的初学者,对服务和广播接收器了解不多。
while(!checkVaccine(pinCode,date)){
Handler waitForFiveSeconds = new Handler();
waitForFiveSeconds.postDelayed(new Runnable() {
@Override
public void run() {
}
},5000);
dataList.clear();
}
Toast.makeText(MainActivity.this,"Vaccine Is AVailable.",Toast.LENGTH_SHORT).show();
notifyToTheUser();
What I have to use to repeat checkVaccine method forever in background after every 5 Seconds?每 5 秒后,我必须使用什么来在后台永远重复checkVaccine 方法?
What you are looking for is background service.您正在寻找的是后台服务。 You can run operation in a background service for a long time.
您可以在后台服务中长时间运行操作。 As you want to run these infinitely, this won't work as android has put some limitations prior Android O so your service would be stopped at some point
当您想无限运行这些时,这将不起作用,因为 android 在 Android O 之前设置了一些限制,因此您的服务将在某个时候停止
These are some articles on how to create it and it's limitations这些是一些关于如何创建它及其限制的文章
https://www.tutorialspoint.com/how-to-create-background-service-in-android https://www.tutorialspoint.com/how-to-create-background-service-in-android
https://medium.com/@kevalpatel2106/how-to-handle-background-services-in-android-o-f96783e65268 https://medium.com/@kevalpatel2106/how-to-handle-background-services-in-android-o-f96783e65268
This is not a good approach to run any task infinitely in the background and indeed it is a bad practice.这不是在后台无限运行任何任务的好方法,而且确实是一种不好的做法。 This will drain battery and system resources.
这将耗尽电池和系统资源。
You want to build something which notifies you when vaccines get available for a certain Pincode, you should build something which runs on servers and sends you a notification when it is available, not your device checking every 5 seconds for it.您想要构建一些东西,当疫苗可用于某个 Pincode 时通知您,您应该构建一些在服务器上运行并在可用时向您发送通知的东西,而不是您的设备每 5 秒检查一次。
You could build a Telegram bot and deploy your application on a service like Heroku or AWS.您可以构建一个 Telegram 机器人并将您的应用程序部署在 Heroku 或 AWS 等服务上。
I want to let you know, that cowin API is Geo-Restricted so if you built something server-side it won't work at all if you choose any instance that is not located in India, You need an Indian server instance to make this work.我想让你知道,那个 Cowin API 是受地理限制的,所以如果你在服务器端构建了一些东西,如果你选择任何不在印度的实例,它根本不起作用,你需要一个印度服务器实例来完成这个工作。
I got the solution of my answer by watching this video .通过观看此视频,我得到了答案的解决方案。
I have to use Thread Or Runnable Interface to do that in background.我必须使用线程或可运行接口在后台执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.