akeLock不等待网络连接 [英] wakeLock does not wait for network connectivity

查看:122
本文介绍了akeLock不等待网络连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用唤醒锁来发出警报,以定期更新应用程序状态. wifi需要一段时间才能连接到三星手机.同样,Wifi上的保持唤醒"选项在三星手机上不起作用(

I am using a wakelock for a alarm to update the app state regularly. The wifi takes a while to connect on Samsung phones. Also the "keep awake" option on Wifi does not work on Samsung phones (nor are they interested in fixing the issue). So when the wakelock does happen, it should wait for wifi to connect. Do I need to create a listener for the wifi connectivity for this to work, or should wakelock, kinda block for that wifi to connect ?

mWakeLock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK, "Taxeeta");
    mWakeLock.acquire();
// do some network activity, in a asynctask
// in the doPost of asyscTask, release lock

问题是,在AsyncTask中,如果未连接网络,或者需要花费一些时间才能接通(3g需要一段时间才能接通),则Async doInBackground中的Web服务调用将失败.而且我还是要释放锁.

Edit : The question is, that in the AsyncTask if the network is not connected, OR takes time to get on (3g takes a while to get on), the webservice call in the Async doInBackground will fail. And I will have to release the lock anyways.

SO

我应该在其中放置wifi/数据连接侦听器吗?还是有更好的方法?

Should I put in wifi/data connection listeners in ? Or is there a better way ?

推荐答案

我有一个类似的情况-我被警报警报的 BroadcastReceiver 启动 WakefulIntentService ,该服务将开始扫描网络. 我使用了笨蛋保持锁的方式 1 -我打算将其替换为闩锁.我建议您用 WakefulIntentService 替换"AsyncTask".很有可能永远不会触发AsyncTask.在 WakefulIntentService 中,您必须获取并保持wifi锁定-我将其设为YourWakefulIntentService的 static 字段-对此尚不完全清楚-这已经有一段时间了.如果这不起作用,我将在YourWakefulIntentService中使用闩锁:

I have a similar scenario - I am woken up by an alarm, the alarm's BroadcastReceiver launches a WakefulIntentService and the service starts a scan for networks. I use a stupid way of holding on to the lock1 - I intend to replace this with a latch. I suggest you replace the "AsyncTask" with a WakefulIntentService. Chances are the AsyncTask is not ever fired. In the WakefulIntentService you must acquire and hold on to a wifi lock - I would make this a static field of the YourWakefulIntentService - not entirely clear on this - it's a while back. If this does not work I would use a latch in the YourWakefulIntentService :

// register an alarm
Intent i = new Intent(context, YourReceiver.class);
PendingIntent alarmPendingIntent= PendingIntent.getBroadcast(context, 0, i,
            PendingIntent.FLAG_UPDATE_CURRENT);

public class YourReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        WakefulIntentService.sendWakefulWork(context, YourWIS.class);
    }
}

//pseudocode !
public class YourWIS extends WakefulIntentService { // you must add a cstor !

    @Override
    doWakefulWork() {
      acquireWifiLock();
      enableScanReceiver();
      startScan();
      serviceLatch.wait();
      releaseWifiLock();
    }
}

// in YourScanReceiver
onReceive() {
  if(action.equals(SCAN_RESULTS) {
   // do something that does not take time or start another/the same
   // WakefulIntentService
   serviceLatch.notify();
  }
}

首先尝试WakefulIntentService(我想您是从警报接收器启动AsyncTask的).扫描接收器是已注册以接收扫描结果的接收器(请参阅WifiManager文档-对于睡眠问题,首选接收器而不是侦听器)

Try first the WakefulIntentService (I guess you launch the AsyncTask from the alarm receiver). The scan receiver is a receiver registered to receive the scan results (see WifiManager docs - prefer Receivers to listeners for sleep issues)

1:这是一个工作类-我只是使用第二个唤醒意图服务来保持唤醒锁-仍必须重构它以使用闩锁,但是这种方法至少可以起作用(我拥有第二个服务( Gatekeeper)在监视器上等待并在Gatekeeper内拥有唤醒锁,并且Gatekeeper还持有其CPU锁,因此一切正常(而且很丑)

这篇关于akeLock不等待网络连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆