我可以在WakefulIntentService中同时执行Wakeful和Non Wakeful工作

时间:2014-06-17 15:19:41

标签: android service commonsware-cwac

假设我想要同时运行唤醒和非唤醒服务。

像切换按钮之类的东西。如果是On,那么我必须使用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();
}

他们是否有任何解决方案...或者我必须创建一项新的服务来完成非清醒的工作。

请帮忙。感谢

1 个答案:

答案 0 :(得分:1)

  

假设我想要同时运行唤醒和非唤醒服务

我不清楚哪种情况对此有意义。 WakefulIntentService不支持这种情况(或者,如果是这样,则是偶然的,而不是意图)。

我会推荐switching to WakefulBroadcastReceiver。然后,当您希望服务"唤醒"时,请按照WakefulBroadcastReceiver配方并致电startWakefulService()。如果您希望服务正常运行,请让接收方呼叫startService()。但请注意,如果设备在服务启动之前入睡,则服务有可能无法在此情况下运行。