Search code examples
androidtimedata-connections

Enable/Disable mobile data


I am trying to enable/disable mobile data at particular time of the day. After googling through all the stuff this is what I have:

 public class MainActivity extends Activity {
 ....
 onCreate{ blah blah
 }

 public void onToggleClicked(View v) {
          Calendar objCal = Calendar.getInstance();

      objCal.set(Calendar.HOUR_OF_DAY, 23); 
              objCal.set(Calendar.MINUTE, 0);
      objCal.set(Calendar.SECOND, 0);  
              PendingIntent pi = PendingIntent.getService(context, 0, new Intent( context,
      MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); 
              AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      am.setRepeating(AlarmManager.RTC_WAKEUP, objCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);

     //if condition here

      try { 

     Network objNetwork = new Network();
     objNetwork.setMobileDataEnabled(getBaseContext(), true); // set to false at x:00 pm

      } catch (Exception e) { // TODO Auto-generated catch block
      e.printStackTrace(); }

}

Current Error: context cannot be resolved to a variable.

I am new to android dev and am not really sure if all this is correct... I havent created an intent specifically...


Solution

  • MainActivity is instance of Context. Use this instead of context