title: Turn on the Internet on Android when in sleep
body: <p>I have an Android app that needs to sync to the internet, but as soon as the phone goes to sleep I can't access the internet. It only happens when the user uses the "battery mode", when it turns off the data after 15 minutes. I wrote a test app and its turning the data on, but it still does connect to the server.</p>
  
  <p><strong>What I tried:</strong>  </p>
  
  <ul>
  <li>When I turn the data manually off, then the app is turning it on and it works</li>
  <li>I also tried WakeLock, but it did not help. </li>
  <li>The alarm works as expected, even when the phone goes to sleep for hours</li>
  </ul>
  
  <p>Tested on Motorola Atrix Android 2.3.3. I can't rely on Wifi. In real life it will sync every week. How can we make it possible?</p>
  
  <p><strong>AlarmManager:</strong></p>
  
  <pre><code>alarm_manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
  Intent intent = new Intent(this, AlarmReceiver.class);
  PendingIntent pending = PendingIntent.getBroadcast(this, 0, intent, 
                          PendingIntent.FLAG_UPDATE_CURRENT);
  alarm_manager.setRepeating(AlarmManager.RTC_WAKEUP, 
                          System.currentTimeMillis(), 15000, pending);
  </code></pre>
  
  <p><strong>AlarmReceiver:</strong></p>
  
  <pre><code>public class AlarmReceiver extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          Log.d("MYTAG", "RECEIVED getMobileDataEnabled: " + getMobileDataEnabled(context));  
          if (!isOnline(context)) {
              Log.d("MYTAG", "NO INET");
              if (turnOnInet(context)) {
                  Log.d("MYTAG", "INET IS ON");
              }
          }
  
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost("http://xxx.xxx.xxx.xxx/ping/pong/moto/");
              try {
                  List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(1);
                  nameValuePairs.add(new BasicNameValuePair("short_code", "ROFL"));
                  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                  httpclient.execute(httppost);
                  Log.d("MYTAG", "POST FINISHED");
              }
              catch (Exception e) {
                  Log.e("MYTAG", "MYTAG", e);
              }
      }
  
      public boolean isOnline(Context context) {
          ConnectivityManager cm = (ConnectivityManager)context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo netInfo = cm.getActiveNetworkInfo();
          if (netInfo != null){
              Log.d("MYTAG", "isAvailable: "+netInfo.isAvailable());
          }
          if (netInfo != null &amp;&amp; netInfo.isConnectedOrConnecting()) {
              return true;
          }
          return false;
      }
  
      public boolean turnOnInet(Context context) {
          ConnectivityManager mgr = (ConnectivityManager)context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
          if (mgr == null) {
              Log.d("MYTAG", "ConnectivityManager == NULL");
              return false;
          }
          try {
              Method setMobileDataEnabledMethod = mgr.getClass().getDeclaredMethod("setMobileDataEnabled", boolean.class);
              if (null == setMobileDataEnabledMethod) {
                  Log.d("MYTAG", "setMobileDataEnabledMethod == null");
                  return false;
              }    
              setMobileDataEnabledMethod.invoke(mgr, true);
          }
          catch(Exception e) {
              Log.e("MYTAG", "MYTAG", e);
              return false;
          }
          return true;
      }   
  
  
      private boolean getMobileDataEnabled(Context context) {
          ConnectivityManager mgr = (ConnectivityManager)context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
          if (mgr == null) {
              Log.d("MYTAG", "getMobileDataEnabled ConnectivityManager == null");
              return false;
          }
          try {
              Method method = mgr.getClass().getMethod("getMobileDataEnabled");
              return (Boolean) method.invoke(mgr);
          } catch (Exception e) {
              Log.e("MYTAG", "MYTAG", e);
              return false;
          }
      }
  }
  </code></pre>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /&gt;
  </code></pre>