【问题标题】:Can I do both Wakeful and Non Wakeful works inside WakefulIntentService alternatively我可以在 WakefulIntentService 中同时进行 Wakeful 和 Non Wakeful 工作吗
【发布时间】:2014-06-17 15:19:40
【问题描述】:

假设我想交替运行唤醒和非唤醒服务。

类似于切换按钮的东西。如果它打开,那么我必须使用doWakefulWork() 方法启动我的WakefulIntentService。 如果它关闭,那么我必须在同一个WakefulIntentService 中执行所有相同的任务,但使用不同的方法,在睡眠模式下不与 CPU 和 wifi 交互,就像我所说的非唤醒工作..

我刚刚在我的服务中添加了一个新方法,但无法继续。

public class NetworkCommunicationService extends WakefulIntentService{
private static String UserID;

public NetworkCommunicationService() {
   super("NetworkCommunicationService");
}

@Override
protected void doWakefulWork(Intent intent) {
    if (Utils.isNetworkAvailable(this))
       new SyncValidater().execute();
}

/**I have to do something like this....  */
protected void doNonWakefulWork() {
    if (Utils.isNetworkAvailable(this))
       new SyncValidater().execute();
}

他们有什么解决方案吗?或者我必须创建一个新服务来完成非清醒工作。

请帮忙。谢谢

【问题讨论】:

    标签: android service commonsware-cwac


    【解决方案1】:

    假设我想交替运行唤醒和非唤醒服务

    我不清楚什么情况下对此有意义。 WakefulIntentService 不支持这种情况(或者,如果支持,那是偶然的,不是故意的)。

    我会推荐switching to WakefulBroadcastReceiver。然后,当您希望服务“清醒地”运行时,按照WakefulBroadcastReceiver 配方并调用startWakefulService()。如果您希望服务正常运行,只需让接收方调用startService()。但请注意,在这种情况下,如果设备在服务启动前进入休眠状态,服务可能无法运行。

    【讨论】:

    • 我无法理解“正常运行服务”部分。你能用一个小例子解释一下吗?
    • @user3678972: "run service normal" == "run the service without it using a WakeLock to keep the device awake"
    • 不,我的意思是我如何以及在哪里调用 startService() ?在 onReceive() 内部或从我启动 WakefulBroadcastReceiver 的活动中。
    • @user3678972:我一直在考虑从onReceive() 调用它,所以你的“启动服务”逻辑都在一个地方,这只是你使用哪种方法启动它的问题.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多