gpt4 book ai didi

没有互联网连接时暂停的 Android Intent 服务

转载 作者:行者123 更新时间:2023-11-29 00:08:13 27 4
gpt4 key购买 nike

我有一个 Intent 服务,我将数据库 ID 传递给该服务。然后该服务从数据库中获取相关行。然后它使用 volley 将此数据发送到 Web 服务器。

Handle Intent 方法检查互联网连接,如果没有找到,则在再次检查之前让线程休眠。这感觉很不对劲,但我确实需要服务来等待互联网。

我还需要服务按照填充顺序处理工作队列。

这是当前代码。有没有更好的方法来处理这种情况?

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();
}

最佳答案

如果请求的顺序不重要,我会简单地取消请求并重新启动相同的 IntentService 作为 onHandleIntent 的一部分。像这样:

protected void onHandleIntent(Intent intent) {
// ... Code to get the actionID ...
Boolean connected = manager.isNetworkOnline(this);
if (!connected) {
this.startService(intent);
return;
}
// .. connected to internet, run code that fires the request ...
}

但是,这种方法会导致由于连接问题而无法处理的当前请求被放置在工作队列的末尾,并且您声明这破坏了您的逻辑。

这种方法的另一个问题是,您会不断重启服务,直到互联网连接恢复,这可能会耗尽电池电量。

现在,一个不同的解决方案可能是放弃您的 IntentService 并改为创建一个常规的 Service。让我们将此服务命名为 UploadServiceUploadService 应该启动(以保持其运行)但也使用服务绑定(bind)(用于通信目的)。

UploadService 应该管理一个内部工作队列,以确保您的请求以正确的顺序处理。您应该公开一种方法,通过您的 IBinder 实现对请求进行排队。

UploadService 的主要功能应该是获取(但不删除!- 使用 peek())队列前端的方法。让我们将此方法命名为 handleRequest。如果队列为空,您应该关闭 UploadService。如果队列不为空,您应该生成一个 AsyncTask 来处理放在队列前端的请求。如果请求被成功处理,您将在 onPostExecute 期间移除队列的前端,并重新调用 handleRequest 以检查是否有其他请求排队。如果请求失败 - 很可能是由于互联网连接丢失 - 您不要在 onPostExecute 期间删除前面的元素。相反,您检查互联网连接是否已丢失。如果情况确实如此,您可以注册一个 BroadcastReceiver 来监听 Internet 连接。此 BroadcastReceiver 应在再次建立互联网连接以恢复处理请求时调用 handleRequest

上述方法的伪代码实现看起来像这样:

public class UploadService extends Service {

private final BroadtcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()) {
boolean connected;
// Use extras to verify that connection has been re-established...
if (connected) {
// Unregister until we lose network connectivity again.
UploadService.this.unregisterReceiver(this);
// Resume handling requests.
UploadService.this.handleRequest();
}
}
}
};

private final Queue<RequestData> mRequestQueue = new XXXQueue<RequestData>(); // Choose Queue implementation.

private final UploadServiceBinder mBinder = new UploadServiceBinder();

public class UploadServiceBinder extends Binder {
public void enqueueRequest(RequestData requestData) {
UploadService.this.mRequestQueue.offer(requestData);
}
}

@Override
public IBinder onBind(Intent intent) {
return mBinder;
}

private void handleRequest() {
RequestData request = mRequestsQueue.peek();
if (request == null) {
// No more requests to process.
// Shutdown self.
stopSelf();
} else {
// Process the request at the head of the queue.
new Request().execute(request);
}
}

private class Request extends AsyncTask<RequestData, Void, Boolean> {
@Override
protected void doInBackground(RequestData... requests) {
try {
// ... Code that executes the web request ...
// Return true if request succeeds.
return true;
} catch(IOException ioe) {
// Request failed, return false.
return false;
}
}

@Override
protected void onPostExecute(Boolean success) {
if (success) {
// Remove request from work queue.
UploadService.this.mRequestQueue.remove();
// Continue by processing next request.
UploadService.this.handleRequest();
} else {
// Request failed, properly due to network error.
// Keep request at the head of the queue, i.e. do not remove it from the queue.
// Check current internet connectivity
ConnectionManager manager = new ConnectionManager();
boolean connected = manager.isNetworkOnline(UploadService.this);
if (connected) {
// If connected, something else went wrong.
// Retry request right away.
UploadService.this.handleRequest();
} else {
// Lack of internet.
// Register receiver in order to resume processing requests once internet connectivity is restored.
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
UploadService.this.registerReceiver(UploadService.this.mReceiver, filter);
}
}
}
}
}

关于没有互联网连接时暂停的 Android Intent 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32243859/

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