- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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
。让我们将此服务命名为 UploadService
。 UploadService
应该启动(以保持其运行)但也使用服务绑定(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/
我在 Window 2008 x64 上的 IIS 7 下托管了一个网站。 IIS 以 64 位模式运行,该站点有自己的 64 位应用程序池等。该网站大部分时间似乎运行良好,然后每小时突然卡住用户请求
我有一个 imageView 并希望它像这样工作: ImageViewer可见 5秒暂停 ImageView 不可见 5秒暂停 ImageViewer可见 等等... 我该怎么做?我试过 sleep
我在我的 JavaScript 函数中使用了下面的代码。我想在 10 秒后调用这个函数。然而该函数立即被触发!?!不确定发生了什么。 function testing() { //oth
我想暂停计时器,点击按钮后我想继续计时器计数...我搜索但找不到与此相关的功能.. 怎么办? 最佳答案 您将需要一个变量来跟踪自 Chronometer 启动以来耗时: long timeWhenSt
我目前有一个程序可以从麦克风收集声音信号并在 python 上实时显示波形。对于 matplotlib funcanimation,我正在尝试通过这种方式向我的程序添加一些暂停和启动按钮或功能。但它没
我有一个由套接字提供的热Observable。我可以使用pausable暂停套接字供稿。但是一旦“取消暂停”可观察对象,就需要显示套接字在暂停订阅时可能发送的最后一个值。我不想跟踪套接字手动发送的最后
我知道这是可能的,但我还没有找到方法,所以我在问。 在播放 3rd 方音乐(例如 Spotify)时开始在我的应用程序中播放 mp3 声音时。 Spotify 暂停,您必须恢复 Spotify,让它再
我正在尝试使用 iPhone 的前置摄像头录制有声视频。因为我还需要支持暂停/恢复功能,所以我需要使用 AVAssetWriter .我在网上找到了一个用 Objective-C 编写的示例,它几乎实
我知道互斥锁可以作为一种实现,但是我想知道是否有一种方法可以像视频播放一样暂停/恢复另一个线程。当其他正在运行的线程很复杂时,此方法更易于编程。 最佳答案 SIGTSTP是用于暂停进程的信号,如果您有
到目前为止,我已经看到了以下停止动画的技术,但我在这里寻找的是旋转 View 停止在当前的角度,而不是返回到 0。 struct DemoView: View { @State private
我一般在问有关多线程的问题。例如我锁定了一个互斥锁并恢复任务,然后我想挂起它,我的问题是,我应该在挂起之前解锁互斥锁吗?这样当我再次使用互斥锁恢复它时,它会成功恢复吗? 我刚刚开始使用多线程的东西,我
我有2个缩略图链接,单击它们时,它们会以灯箱样式打开视频。我的目标是让它们在打开时播放,在关闭时暂停(单击背景区域时关闭)。 我的HTML代码在这里: M
到目前为止,我没有将我发现的几种不同方法拼凑在一起: http://192.185.121.49/~steveobr/ 我需要所有的语音演示像第一个“商业”一样工作 正如您在实时示例中看到的那样,每个
所以我正在制作某种游戏,玩家可以在其中获得一些能力。玩家回合结束后,服务器应有 5 秒的超时时间,其中不执行任何代码,然后在该时间后结束回合。但是,如果客户端单击其中一项电源,服务器应停止 5 秒超时
我尝试将自己的方法添加到 Tween 类中以暂停/恢复所有补间。这就是我所拥有的: createjs.Tween.pauseAllTweens = function() { for ( var
我对 Azure 搜索标准级别的成本有疑问。是否可以将 Azure 搜索级别从标准更改为基本?是否可以暂时暂停 Azure 搜索标准?我在门户中没有看到此控件。我是否需要将 Azure 搜索实现重新创
如何用相同的代码制作play/Pause按钮。 - (IBAction)min:(id)sender { NSString *path = [[NSBundle mainBundle] pathF
我知道这很可能超出了沙箱范围,但我还是想问一下: 我想在我的应用程序中放置一个“暂停/播放”按钮,以暂停或播放任何背景音频。基本上,我希望实现在多任务栏中找到的播放/暂停按钮。 一个简单的例子是有人用
我正在制作一款编程游戏,玩家可以在其中对盟友的行为进行编程。玩家为给定的盟友编写decide()函数的主体,可以用任何java代码填写,但必须返回一个 Action 。我想为每个盟友提供一组有限的每个
我有功能 1 用于播放音乐,第二个用于设置实际音乐的暂停,我的暂停功能不起作用。我该如何设置暂停? function play(id){ var audio = new Audio('
我是一名优秀的程序员,十分优秀!