我有一个意图服务,我将数据库ID传递给它。服务然后从数据库中获取相关行。 然后使用volley将此数据发送到Web服务器。
Handle Intent方法检查是否有Internet连接,如果没有,则在再次检查之前使线程休眠。这感觉不对劲而且不好,但我确实需要该服务等待Internet。
我还需要该服务按照填充顺序处理工作队列。
以下是当前的代码。 有更好的处理此场景的方法吗?
Handle Intent方法检查是否有Internet连接,如果没有,则在再次检查之前使线程休眠。这感觉不对劲而且不好,但我确实需要该服务等待Internet。
我还需要该服务按照填充顺序处理工作队列。
以下是当前的代码。 有更好的处理此场景的方法吗?
public class CommandUploadService extends IntentService {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
private ServiceCallbacks serviceCallbacks;
public void setCallbacks(ServiceCallbacks callbacks) {
serviceCallbacks = callbacks;
}
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
public CommandUploadService getService() {
// Return this instance of LocalService so clients can call public methods
return CommandUploadService.this;
}
}
// TODO: Rename actions, choose action names that describe tasks that this
// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
private static final String ACTION_UPLOAD_COMMAND = "com.brandfour.tooltracker.services.action.UPLOAD_COMMAND";
// TODO: Rename parameters
private static final String ID = "com.brandfour.tooltracker.services.id";
/**
* Starts this service to perform action Foo with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
// TODO: Customize helper method
public static void startActionUploadCommand(Context context, String actionID) {
Intent intent = new Intent(context, CommandUploadService.class);
intent.setAction(ACTION_UPLOAD_COMMAND);
intent.putExtra(ID, actionID);
context.startService(intent);
}
/**
* Unless you provide binding for your service, you don't need to implement this
* method, because the default implementation returns null.
*
* @param intent
* @see Service#onBind
*/
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public CommandUploadService() {
super("CommandUploadService");
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_UPLOAD_COMMAND.equals(action)) {
final String id = intent.getStringExtra(ID);
handleActionUpload(id);
}
}
}
/**
* Handle action Foo in the provided background thread with the provided
* parameters.
*/
private void handleActionUpload(String actionID) {
final ActionCommand ac = new RushSearch().whereId(actionID).findSingle(ActionCommand.class);
serviceCallbacks.refreshList();
ConnectionManager manager = new ConnectionManager();
Boolean connected = manager.isNetworkOnline(this);
while (connected == false) {
connected = manager.isNetworkOnline(this);
SystemClock.sleep(10000);
}
JSONObject json = new JSONObject();
JSONObject wrapper = new JSONObject();
try {
json.put("Command", ac.getCommand());
json.put("TimeStamp", ac.getTimeStamp());
json.put("State", ac.getState());
wrapper.put("command", json);
} catch (Exception e) {
}
String url = "[ommitted]";
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, wrapper,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
ac.setState("SENT");
ac.save();
serviceCallbacks.complete();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("BRANDFOUR", "Error: " + error.getMessage());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
};
// Adding request to request queue
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(jsonObjReq);
}
public interface ServiceCallbacks {
void complete();
void refreshList();
}
}