Héctor Ortiz
Héctor Ortiz

Reputation: 267

which context do I need?

I've the class:

PendingIntent pendingIntent;
public class xxx{

public void updateObjects(){

deleteIntents(Context context, int x);
(...)
for(...){
    //Update each object ofdb4o with the new object value's.
    (...)
    doIntents(context,mil,obj);
    }
            (...)
    }

public void doIntents(Context context, long mil, ClassObjects obj){
(...)
pendingIntent = PendingIntent.getBroadcast(context, obj.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
(...)
}
}

public void deleteIntents(Context context, int x){
            intent = new Intent(context, OnAlarmReceiver.class);
            for(int i=1;i<x;i++){
                pendingIntent = PendingIntent.getBroadcast(context, i, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                alarmManager.cancel(pendingIntent);
            }
            Log.d(TAG,"intents removed");
        }

And work's Ok.. But I need use deleteIntents(Context context, int x) from main Activity.. With a button:

xxx.deleteIntents(getApplicationContext(),x);

But doesn't work.. I think that I've a problem with the context but I don't know why..

Can anybody help me please?

Upvotes: 0

Views: 460

Answers (2)

Ali Behzadian Nejad
Ali Behzadian Nejad

Reputation: 9044

Each Activity is a Context object.

So inside Activity class just use "this" pointer instead of getApplicationContext()

Upvotes: 1

H&#233;ctor Ortiz
H&#233;ctor Ortiz

Reputation: 267

Sorry,

I had forgotten to instantiate the AlarmManager before:

public void deleteIntents(Context context, int x){
            alarmManager = (AlarmManager) context.getSystemService (Service.ALARM_SERVICE);
            intent = new Intent(context, OnAlarmReceiver.class);
            for(int i=1;i<x;i++){
                pendingIntent = PendingIntent.getBroadcast(context, i, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                alarmManager.cancel(pendingIntent);
            }
            Log.d(TAG,"intents removed");
        }

Now Work's!! Thank you both!!!!!

Upvotes: 0

Related Questions