部分唤醒锁不起作用

发布于 2024-12-29 00:31:18 字数 558 浏览 2 评论 0原文

我的应用程序具有活动和后台服务,必须24*7运行,

我的应用程序必须通过Wi与服务器通信-Fi 发送和接收信息。

问题:每当服务器发送任何警报时,我的应用程序都应该接收并弹出该应用程序,无论它是在前台还是后台运行,并向用户告知有关警报的信息。

因此,当设备处于活动状态时,此功能可以完美运行,但当设备进入睡眠模式时,1 或 2 分钟后,它会与服务器断开连接并停止通信。因此,为了解决这个问题,我编写了代码,将 WiFi 睡眠策略设置为 NEVER,并在后台服务的 OnCreate() 方法中获取部分锁定,并在 OnDestroy() 中释放锁定 服务的方法。现在观察一段时间它工作正常意味着5或10分钟之后它再次停止通信。

应用程序在Android 2.1上开发,并部署在支持Android 2.3版本的设备上。

我无法理解为什么部分锁定会这样,请帮助我解决这个问题。

问候, 皮克斯。

My application is having activities and background service which has to run 24*7,

My application has to talk to the server via Wi-Fi to send and receive the information.

Problem: whenever any alarm send by the server my app should receive and pops up the app whether it is running in foreground or background and intimate to the user about the alarm.

So when device is in active state this feature working perfectly but when device goes to sleep mode, after 1 or 2 mins it disconnects from the server and stop communicating. so in order to resolve it I written code which will set the WiFi sleep policy to NEVER and acquire the partial lock in the OnCreate() method of Background service and releasing the lock in OnDestroy() method of the service.Now observation is for some time it is working fine means for 5 or 10 mins thereafter again it stop communicating.

App is developed on Android 2.1 and deployed on device supports Android 2.3 version.

I am not able to understand why partial lock behaves like this, please help me to resolve this issue.

regards,
Piks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

女皇必胜 2025-01-05 00:31:18

我也面临着同样的问题,最终找到了完美的解决方案。

尝试通过扩展 Application 类来获取唤醒锁:
代码:

package com.example.MyApp.UserView;

import android.app.Application;
import android.content.Intent;
import android.os.PowerManager;
import android.provider.Settings;

/**
 * @author SDurai
 * 
 */
public class MyApp extends Application 
{
    private static final String TAG = MyApp.class.getSimpleName();
    private PowerManager.WakeLock mWakeLock = null;

    @Override
    public void onCreate() {
        super.onCreate();

        final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
        mWakeLock.acquire();
    }

    @Override
    public void onTerminate() {
        if (mWakeLock.isHeld())
            mWakeLock.release();
        super.onTerminate();
    }
}

如果您还有任何其他疑问,请告诉我。准备好帮忙了!

I was also facing the same problem and finally found the solution which works perfectly.

Try to acquire the wake lock by extending Application class:
Code:

package com.example.MyApp.UserView;

import android.app.Application;
import android.content.Intent;
import android.os.PowerManager;
import android.provider.Settings;

/**
 * @author SDurai
 * 
 */
public class MyApp extends Application 
{
    private static final String TAG = MyApp.class.getSimpleName();
    private PowerManager.WakeLock mWakeLock = null;

    @Override
    public void onCreate() {
        super.onCreate();

        final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
        mWakeLock.acquire();
    }

    @Override
    public void onTerminate() {
        if (mWakeLock.isHeld())
            mWakeLock.release();
        super.onTerminate();
    }
}

Let me know if you have any other doubts. Ready to help!

南…巷孤猫 2025-01-05 00:31:18

您可能还需要一个 WifiManager.WifiLock:(

WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wl = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, "myId");
wl.acquire();

其中 wl 是您的 WifiManager.WifiLock,即 s)

You probably need a WifiManager.WifiLock, too:

WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wl = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, "myId");
wl.acquire();

(where wl is your WifiManager.WifiLock, which s)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文