Cannot immediately connect to the server when android device is on sleep even though my app gets the PARTIAL_WAKE_LOCK at PowerManager (Sony Xperia)

岁酱吖の 提交于 2019-12-25 06:37:11

问题


[Abstract of my android app] My app monitors the server at fixed intervals (30 seconds).

[Results(Issue)]

  1. 18[h] 00[m] 00[s] server check ok.
  2. 18[h] 00[m] 30[s] server check ok.
  3. 18[h] 03[m] 14[s] server check ok. <= This is issue. My app should check the server at 18:01:00.

    My app cannot connect the server at fixed intervals at Sony xperia.

[Expected results]

  1. 18[h] 00[m] 00[s] server check ok.
  2. 18[h] 00[m] 30[s] server check ok.
  3. 18[h] 01[m] 00[s] server check ok.

[About my program] My app recieves the Broadcast from AlarmManager at fixed intervals. BroadcastReceiver starts service. At service, my app connects to the server. The details of my app is below.

[Step1] : setRepeating by AlarmManager

private void setRepeating () {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
          am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),pollingIntervalmsec, pendingIntent);
}

[Step2] : Recieve Broadcast from AlarmManager and start Service

 public class PollingAlarmBroadcastReceiver extends BroadcastReceiver {
        private static final String WAKELOCK_TAG = "xxx.xxx.xxx.xxx.PollingAlarmBroadcastReceiver";
        private static PowerManager.WakeLock wl;
        @Override
        public void onReceive(Context context, Intent arg1) {
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG);
            wl.acquire();
            Intent intent = new Intent(context, MonitoringService.class);
            context.startService(intent);
        }

        private void doOperation(Context context) {
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG);
            wl.acquire();
            Intent intent = new Intent(context, MonitoringService.class);
            context.startService(intent);
         }
     }

AndroidManifest.xml

    <receiver
        android:name="xxx.xxx.xxx.xxx.PollingAlarmBroadcastReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="cxxx.xxx.xxx.xxx.PollingAlarm" />
        </intent-filter>
    </receiver>

[Step3] : At service, connect to the server.

HttpGet request = new HttpGet(url);
request.setHeaders(headers);
HttpParams params = new BasicHttpParams();
DefaultHttpClient httpClient = new DefaultHttpClient(params);
HttpResponse response = httpClient.execute(request);

[Step4] : release wakeup lock after connecting the server.

wl.release();

[Issue log] My app successfully receives the braodcast from AlarmManager and my service successfully starts too. But my program suspends at "httpClient.execute" for about 2 minutes with the logs below.

I/QCNEA(10674): |NIMS| getaddrinfo: hostname google.co.jp servname NULL numeric 4 appname I/QCNEJ(686): |CORE| CNE_NOTIFY_NSRM_BLOCKED_UID received

[Normal log]

I/QCNEA(10674): |NIMS| getaddrinfo: hostname google.co.jp servname NULL numeric 4 appname I/QCNEA(10674): |NIMS| getaddrinfo: hostname google.co.jp servname NULL numeric 0 appname I/QCNEA(278): |NIMS| getaddrinfo: hostname google.co.jp servname NULL numeric 0 appname I/QCNEA(10674): |NIMS| getaddrinfo: hostname 173.194.126.151 servname NULL numeric 4 appname I/QCNEA(10674): |NIMS| connect: for 43 saddr 00000000000000000000000000000000:52017 (28) daddr 10 0000000000000000ffff00007681007d:80

[Environment] Sony xperia SOL22, this is japanese model of Sony Xperia Z. Android 4.2.2. Network environment is mobile internet(LTE) only. Wifi switch is off.

[Issue analysis] My app works fine when my xperia device does not sleep. My app also works fine when I use Wifi only. I tried this app at Nexus7 too. It works fine. My xperia network device or software may continues to sleep even though my app acquires WakeLock from PowerManager.

[Question] How to fix this issue? Any ideas are very welcome.


回答1:


I don't know anything about developing phone apps but I own an Xperia Z. When the phone is on sleep, it often switches of the Wifi & Mobile Internet - which comes back on when the screen is switched on.

Could this be your problem?



来源:https://stackoverflow.com/questions/23540491/cannot-immediately-connect-to-the-server-when-android-device-is-on-sleep-even-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!