3

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?

mehulmpt
  • 15,861
  • 12
  • 48
  • 88
  • Will [Firebase JobDispatcher](https://github.com/firebase/firebase-jobdispatcher-android) come handy for you? It will kick in when certain constrains are fulfiled, among which is internet connection. – azizbekian Feb 25 '17 at 15:12
  • 1
    @azizbekian Its not that internet connection is cut off, my service goes offline. The service cannot access internet. – mehulmpt Feb 25 '17 at 18:39
  • @mehulmpt any solution ? – UMESH0492 Jun 08 '17 at 13:41
  • Got any solution for this @mehulmpt? I'm facing a similar problem: https://stackoverflow.com/q/47954615/1259763 – Federico Alvarez Dec 23 '17 at 17:46

1 Answers1

0
      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

michael
  • 25
  • 7