如何防止Android应用程序在夜间唤醒锁定时执行操作?

时间:2014-12-03 15:38:12

标签: android wakelock rx-java android-wake-lock

当活动在前台时,即使手机应该睡觉,我的应用也在晚上做事。未设置唤醒日志权限。

<!--uses-permission android:name="android.permission.WAKE_LOCK" /--> <!-- DISABLED -->

但是在服务器日志中,我可以看到应用程序多次访问服务器,depsite唤醒锁定已关闭。

2014-12-02 20:58:15.755 200 2.56 KB 70ms /StorageEndpoint.getSignedUrls
2014-12-02 22:57:30.111 200 2.57 KB 150ms /StorageEndpoint.getSignedUrls
2014-12-03 00:57:04.359 200 2.56 KB 65ms /StorageEndpoint.getSignedUrls
2014-12-03 02:57:05.138 200 2.56 KB 154ms /StorageEndpoint.getSignedUrls
2014-12-03 04:56:14.456 200 2.56 KB 72ms /StorageEndpoint.getSignedUrls
2014-12-03 05:46:02.137 200 3.62 KB 551ms /SessionEndpoint.signinUser
2014-12-03 05:56:03.048 200 3.64 KB 269ms /SessionEndpoint.signinUser
2014-12-03 06:06:01.658 200 3.6 KB 209ms /SessionEndpoint.signinUser
2014-12-03 06:16:02.138 200 3.62 KB 309ms /SessionEndpoint.signinUser
2014-12-03 06:26:02.253 200 3.62 KB 274ms /SessionEndpoint.signinUser

在这篇文章中When will android stop its cpu without wake lock? @AksharPrabhuDesai解释说其他一些进程可能会唤醒设备,这就解释了为什么我的应用程序在闹钟响起的早晨时间间隔10分钟访问服务器。

但是为什么它会在晚上2小时内访问服务器?两小时的间隔是签名网址到期的时间,我的应用程序想要续订它们。但是为什么设备在当时正好清醒呢?我自己的应用程序必须保持清醒,或其他一些应用程序。有没有办法调试这个,有没有办法检测用户是否实际使用我的应用程序,或者手机是否被某些其他活动唤醒?

-

以下是使客户端连接到服务器的代码[我知道它效率不高且尚未优化]:

private static Subscription maintainSignedUrlsSubscription = Observable.timer(0, Constants.SIGNED_URL_MAINTENANCE_EVERY_SECONDS, TimeUnit.SECONDS)
        .flatMap(new Func1<Long, Observable<UploadItem>>() {

            @Override public Observable<UploadItem> call(Long aLong) {
                try {
                    // count URLs
                    List<SignedUrl> expiringUrls = new ArrayList<SignedUrl>(); // find urls which will expire soon
                    List<SignedUrl> expiredUrls = new ArrayList<SignedUrl>(); // find expired urls
                    for (SignedUrl url : Session.getSession().getApiSession().getSignedUrls()) {
                        if (System.currentTimeMillis() > url.getExpires() - 1000 * Constants.SIGNED_URL_EXPIRED_TOLERANCE_SECONDS) {
                            expiredUrls.add(url);
                        } else if (System.currentTimeMillis() > url.getExpires() - 1000 * Constants.SIGNED_URL_EXPIRING_TOLERANCE_SECONDS) {
                            expiringUrls.add(url);
                        }
                    }
                    Session.getSession().getApiSession().getSignedUrls().removeAll(expiredUrls);

                    int reserve_IMAGE_JPEG = 0;
                    int reserve_VIDEO_MP4 = 0;
                    for (SignedUrl url : Session.getSession().getApiSession().getSignedUrls()) {
                        switch (ContentType.valueOf(url.getContentType())) {
                            case IMAGE_JPEG: reserve_IMAGE_JPEG++; break;
                            case VIDEO_MP4: reserve_VIDEO_MP4++; break;
                        }
                    }
                    for (SignedUrl url : expiringUrls) {
                        switch (ContentType.valueOf(url.getContentType())) {
                            case IMAGE_JPEG: reserve_IMAGE_JPEG--; break;
                            case VIDEO_MP4: reserve_VIDEO_MP4--; break;
                        }
                    }

                    System.out.println("**** Uploader: stats: signed urls = " + reserve_IMAGE_JPEG + "/" + reserve_VIDEO_MP4 + " (jpeg/mp4), queue items = " + uploadItemsQueue.size() + "/" + delayedItemsQueue.size() + " (upload/delayed), queued files = "  + uploadFilesQueue.size() + "/" + delayedFilesQueue.size() + " (upload/delayed)"); // report all uploader stats here

                    List<SignedUrl> newUrls = new ArrayList<SignedUrl>();
                    if (reserve_IMAGE_JPEG < Constants.SIGNED_URL_MINIMUM_RESERVE_IMAGE_JPEG) {
                        System.out.println("**** Uploader: signed urls: requesting IMAGE_JPEG pack");

                        newUrls.addAll(StorageEndpoint.getSignedUrls(Session.getUser().getId(), ContentType.IMAGE_JPEG));
                    }
                    if (reserve_VIDEO_MP4 < Constants.SIGNED_URL_MINIMUM_RESERVE_VIDEO_MP4) {
                        System.out.println("**** Uploader: signed urls: requesting VIDEO_MP4 pack");

                        newUrls.addAll(StorageEndpoint.getSignedUrls(Session.getUser().getId(), ContentType.VIDEO_MP4));
                    }

                    Session.getSession().getApiSession().getSignedUrls().addAll(newUrls); // add new urls
                } catch (Exception e) { // can be IOException, ConcurrentModificationException
                    // fail silently
                }
                return Observable.just(null);
            }

        }).subscribeOn(Schedulers.newThread()).observeOn(Schedulers.newThread())
        .subscribe();

任何一个rxjava人都可以告诉我一个Observable的行为 - 它会定期发出 - 而Android应该在睡觉吗?我是否需要明确地将Observable置于睡眠状态?

0 个答案:

没有答案