且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关于GcmTaskService几个问题

更新时间:2023-12-03 14:37:40

你可以找到答案,大部分的问题就在这里。

https://github.com/jacktech24/gcmnetworkmanager-android-example/blob/master/README.md

要未回答没有答案

7:你不会得到通知时,wakelock被删除,因为它们在文档中说,这个API仅仅是短期的任务,如果你有时间越长,写自己的实施

9:不,你不能截至目前,该API不允许它

10:这是因为谷歌Play业务是照顾它,也没有办法解决它。你必须检测服务的应用程序是否设置(我希望这是你的问题),例如。配置创建等,并最终取消所有任务时,他们中的一个被称为

Background

I wanted to use the new JobScheduler API that was presented on Lollipop, but sadly it doesn't have an official port for pre-Lollipop.

However, there is the GcmTaskService, which provides very similar functionalities.

The problem

This API is quite new, so there are very few places to look for information of how to use it (here and here, for example).

The questions

I have a few questions about this new API :

  1. It seems that it requires Google Play Services (here) to be used (except for when using Lollipop version of Android, which will use the normal JobScheduler). What should I do in case the Google play services aren't available?

  2. It seems that even though I've used "setPersisted(true)" for a repeated task, when I restart the device the task won't be called again. How come? EDIT: that's because I missed a permission of RECEIVE_BOOT_COMPLETED .

  3. What is the default behavior of a task, in case I don't use "setRequiredNetwork" ? Is it "NETWORK_STATE_ANY" ?

  4. The docs say about what's returned from onRunTask , I can return any of the values "RESULT_FAILURE", "RESULT_RESCHEDULE", "RESULT_SUCCESS" (info here). It seems both the FAILURE and SUCCESS options will do the same thing - remove the task from the queue. Is it true? If so, what exactly is the difference between them ? Do they function differently?

  5. Are "TaskParams" used only for the tag of the task? Can I somehow pass a bundle to the task using the API? Otherwise, I would need to set a DB for storing what should be passed to the tasks, right?

  6. Is it possible for the app to get the queue of the tasks? I know it's possible using adb, but is it possible using the API too?

  7. They say (here) that each task has a wakelock of up to 3 minutes. What should be done if the task needs more than that? Should it acquire another wakelock for itself? Will the API warn that the wakelock was released? Here's what the docs say:

The scheduler will hold a PowerManager.WakeLock for your service, however after three minutes of execution if your task has not returned it will be considered to have timed out, and the wakelock will be released. Rescheduling your task at this point will have no effect. If you suspect your task will run longer than this you should start your own service explicitly or use some other mechanism; this API is intended for relatively quick network operations.

  1. They say (here) that all networks-tasks are removed each time the app gets upgraded/replaced, and there is a call for "onInitializeTasks" when this happens, and that you can re-schedule them again. How can I re-schedule the tasks? I don't think I can even get the list of tasks...

  2. Is it possible to tell the task to prefer specific times during the day ? For example, between the 14:00-15:00 ?

  3. I've noticed that if you schedule a task, and then you force-stop and/or clear data of the app, the task will still run. How can I avoid this behavior?

you can find answers to most of your questions it here.

https://github.com/jacktech24/gcmnetworkmanager-android-example/blob/master/README.md

To answers that are not answered there

7: You won't get notified when wakelock is removed, and as they say in documentation, this API is only for short tasks, if you have longer, write your own implementation

9: No you can't as of now, the API doesn't allow it

10: That's because Google Play services are taking care of it, and there is no way around it. You have to detect in the service whether the app is setup (I expect that's your problem) eg. configuration is created etc. and eventually cancel all tasks when one of them is called.