如何在android中无限期运行服务
我正在开发一个应用程序,在这个应用程序中,我必须每5分钟点击一次RESTfulWeb服务,以检查新数据是否更新,即使用户关闭了该应用程序。是我干的如何在android中无限期运行服务,android,web-services,service,Android,Web Services,Service,我正在开发一个应用程序,在这个应用程序中,我必须每5分钟点击一次RESTfulWeb服务,以检查新数据是否更新,即使用户关闭了该应用程序。是我干的 @Override public int onStartCommand(Intent intent, int flags, int startId) { RequestQueue requestQueue = Volley.newRequestQueue(this); String url = "http://www.abce
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "http://www.abcert.com/wp-json/wp/v2/posts";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
boolean Str;
try {
JSONArray jsonArray =new JSONArray(response);
Log.i("JSON",""+jsonArray);
JSONObject jsonObject = jsonArray.getJSONObject(0);
int id = jsonObject.getInt("id");
Log.i("MyService is on the",""+id);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Hey","Something went wrong");
}
});
//add request to queue
requestQueue.add(stringRequest);
return START_STICKY;
}
@覆盖
公共int onStartCommand(Intent Intent、int标志、int startId){
RequestQueue RequestQueue=Volley.newRequestQueue(this);
字符串url=”http://www.abcert.com/wp-json/wp/v2/posts";
StringRequest StringRequest=新的StringRequest(Request.Method.GET,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
布尔Str;
试一试{
JSONArray JSONArray=新JSONArray(响应);
Log.i(“JSON”和“+jsonArray”);
JSONObject JSONObject=jsonArray.getJSONObject(0);
intid=jsonObject.getInt(“id”);
Log.i(“MyService位于”、“”+id上);
}捕获(JSONException e){
e、 printStackTrace();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
我(“嘿”,“出了点问题”);
}
});
//将请求添加到队列
添加(stringRequest);
返回开始时间;
}
但是没有成功。当我关闭应用程序服务时,START\u STICKY不起作用也被终止
我正在开发一个应用程序,在这个应用程序中,我必须每5分钟点击一次RESTfulWeb服务,以检查新数据是否更新,即使用户关闭了该应用程序
运行无限期服务不是理想的解决方案
为此,请使用a,因为它还可以有效使用电池。许多应用程序使用连接到数据库的
服务来执行特定任务并在后台运行
像Facebook运行的是MessagingService
,Instagram运行的是NotificationService
。如果需要,您可以同时使用处理程序
,也可以选择调度程序
使用处理程序的示例
:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
String url = "http://www.abcert.com/wp-json/wp/v2/posts";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
boolean Str;
try {
JSONArray jsonArray =new JSONArray(response);
Log.i("JSON",""+jsonArray);
JSONObject jsonObject = jsonArray.getJSONObject(0);
int id = jsonObject.getInt("id");
Log.i("MyService is on the",""+id);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Hey","Something went wrong");
}
});
//add request to queue
requestQueue.add(stringRequest);
handler.postDelayed(this, 1000 * 60 * 5); //Code runs every 5 minutes
}
}, 0); //Initial interval of 0 sec
return START_STICKY;
}
@覆盖
公共int onStartCommand(Intent Intent、int标志、int startId){
最终处理程序=新处理程序();
handler.postDelayed(新的Runnable(){
公开募捐{
字符串url=”http://www.abcert.com/wp-json/wp/v2/posts";
StringRequest StringRequest=新的StringRequest(Request.Method.GET,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
布尔Str;
试一试{
JSONArray JSONArray=新JSONArray(响应);
Log.i(“JSON”和“+jsonArray”);
JSONObject JSONObject=jsonArray.getJSONObject(0);
intid=jsonObject.getInt(“id”);
Log.i(“MyService位于”、“”+id上);
}捕获(JSONException e){
e、 printStackTrace();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
我(“嘿”,“出了点问题”);
}
});
//将请求添加到队列
添加(stringRequest);
handler.postDelayed(这是1000*60*5);//代码每5分钟运行一次
}
},0);//初始间隔为0秒
返回开始时间;
}
在mainfest文件中提及服务的地方添加android:exported=“true”。什么android:exported=“true”会起作用?试试看,可能是你遗漏了它。不,它不起作用。@hitesh试试我刚才发布的答案。我们在Flipkart应用程序中使用相同的代码,你怎么能说不起作用。尝试将toast放入响应和跟踪中。当您的URL不提供任何数据时,我猜这是一个正在运行的网站,但当我关闭应用程序时,服务也被禁用。。。不知道为什么…你在哪部手机上测试?哈哈,小米也有同样的问题,请看我的答案,在小米手机上启用服务