我正在研究一個應用程序,其中我必須每5分鐘打一次REST風格的Web服務,以檢查新數據是否已更新,即使應用程序是由用戶關閉。我做到了這一點如何在android中運行無限期的服務
@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;
}
但沒有得到成功。當我關閉應用服務時,START_STICKY不起作用。
添加機器人:任何地方提到的服務在mainfest文件導出= 「真」。 –
什麼android:exported =「true」將工作? – hitesh
嘗試它可能是你錯過了。 –