0
public static void createAlarms() {
    totaltimetaken = totaltimetaken + timetaken;
    totalcost = totalcost + costone; 
    cal = Calendar.getInstance();
    //cal.add(Calendar.HOUR, alarmintervalint);
    cal.add(Calendar.SECOND, alarmintervalint);
    calintent = new Intent(this, AlarmBroadcastReceiver.class);
    calpendingintent = PendingIntent.getBroadcast(this.getApplicationContext(), 12345, calintent, 0);
    am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, alarmintervalint, calpendingintent);
}

I have made this method static so I can call it an another class of the same project. I am getting an error on these lines:

calintent = new Intent(this, AlarmBroadcastReceiver.class);
    calpendingintent = PendingIntent.getBroadcast(this.getApplicationContext(), 12345, calintent, 0);
    am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);

1) Cannot use 'this' in a static context
2) Cannot make a static reference to the non-static method getSystemService(String) from the type

How would I solve these errors? Thanks a lot!

4 Answers4

2

Read Why can't we use 'this' keyword in a static method.

So solution is to use parameterise method and send Context into param and use that context at place of this

as

public static void yourMethod(Context mContext) {

calintent = new Intent(mContext, AlarmBroadcastReceiver.class);
    calpendingintent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 12345, calintent, 0);
    am = (AlarmManager)mContext.getSystemService(Activity.ALARM_SERVICE);
}

So your method should be

public static void createAlarms(Context mContext) {
    totaltimetaken = totaltimetaken + timetaken;
    totalcost = totalcost + costone; 
    cal = Calendar.getInstance();
    //cal.add(Calendar.HOUR, alarmintervalint);
    cal.add(Calendar.SECOND, alarmintervalint);
    calintent = new Intent(mContext, AlarmBroadcastReceiver.class);
    calpendingintent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 12345, calintent, 0);
    am = (AlarmManager)mContext.getSystemService(Activity.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, alarmintervalint, calpendingintent);
}
Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • That makes sense, thanks a lot! But the actual function of the method won't change right? –  Jul 18 '13 at 06:30
  • but suppose i would call this class somewhere, how would I? Sorry, I'm not that familiar with calling methods. I mean if I just say "createAlarm();" it is giving me an error. –  Jul 18 '13 at 06:32
  • From where you are calling this method? Activity/ Services/ Receiver? Each component having some methods to get Context object... – Pankaj Kumar Jul 18 '13 at 06:41
  • In Activity you can call `ActivityName.this` or `getApplicationContext()` and same as Service. But in Receiver you will get context from onReceive – Pankaj Kumar Jul 18 '13 at 06:42
  • Activity. Like a different method of the same activity. And also after a different class as a whole. Like the post below, I just used "createAlarms(this);" and it worked. Thanks! –  Jul 18 '13 at 06:43
  • if you are using `this` better is to use `createAlarms(ActivityName.this);` because you can call this method from threads too... – Pankaj Kumar Jul 18 '13 at 06:45
  • I am also calling it from a class extended to Broadcast receiver, but its not working the same way.? –  Jul 18 '13 at 06:48
  • See at `onReceive(Context context, Intent intent)` of Receiver.. here you should use `context`. So call method as `createAlarms(context);` – Pankaj Kumar Jul 18 '13 at 06:50
0

Simply said, static class has no this pointer. You should pass it as parameter instead:

public static void createAlarms(final Context context) {
    ...
    calintent = new Intent(context, AlarmBroadcastReceiver.class);
}

Then you call it from activity:

createAlarms(this);

or from fragment;

YourActivity.createAlarms(getActivity());
Neoh
  • 15,906
  • 14
  • 66
  • 78
0

As you can only access static members from a static method,and getSystemService is a non static method,so you cant do that.

Option 1: You can either remove static keyword , and call your method from object

Or

Option 2: You can move the code of getSystemService to other non static method .and you can call that from other non static method

saum22
  • 884
  • 12
  • 28
0

This Reference is used when we want to refer the current object of the class, static methods and variables are loaded before the main and object of the class is the made,, either avoid the use of the this word , or make your method non static

Deepak Odedara
  • 481
  • 1
  • 3
  • 12