1

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...

who-aditya-nawandar
  • 1,334
  • 9
  • 39
  • 89

2 Answers2

1

Add

import android.content.Context

to the top of your activity sourcecode

SathMK
  • 1,171
  • 1
  • 8
  • 18
0

MainActivity is instance of Context. Use this instead of context

Tishka17
  • 305
  • 2
  • 12