Reputation: 16587
My app uses a service which needs internet connection to work in the background. However, when android goes to sleep, my service cannot access internet anymore. I've seen that wakelock can fix it, but it seems to be an overkill for just keeping the network up. Is there any other way to achieve this?
How does apps like whatsapp handle this?
Upvotes: 3
Views: 1241
Reputation: 25
namespace blablabla
{
[BroadcastReceiver]
public class blablabla : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager)Android.App.Application.Context.GetSystemService(Context.PowerService);
PowerManager.WakeLock wakeLock = pm.NewWakeLock(WakeLockFlags.Full,
"BackgroundReceiver");
wakeLock.Acquire();
//var context = this.ApplicationContext;
WifiManager wifiManager =
(WifiManager)context.GetSystemService("wifi");
WifiLock mWifiLock =
wifiManager.CreateWifiLock(Android.Net.WifiMode.Full, "WifiLock");
mWifiLock.Acquire();
//you code
mWifiLock.Release();
wakeLock.Release();
}}}
answer for other people looking for
Upvotes: 0