views:

536

answers:

1

Hi,

I want to use an alarm to run some code at a certain time. I have successfully implemented an alarm with the broadcast receiver registered in the manifest but the way i understand it, this method uses a separate class for the broadcast receiver.

I can use this method to start another activity but I cant use it to run a method in my main activity?

(http://stackoverflow.com/questions/3122564/how-can-i-notify-a-running-activity-from-a-broadcast-receiver)

So I have been trying to register my broadcast receiver in my main activity as explained in the answer above.

private BroadcastReceiver receiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "hello", Toast.LENGTH_SHORT).show();
        uploadDB();         
    }
};    

public void onResume() {
    super.onResume();

    IntentFilter filter = new IntentFilter();
    filter.addAction(null);

    this.registerReceiver(this.receiver, filter);
}

public void onPause() {
    super.onPause();

    this.unregisterReceiver(this.receiver);
}

However I have been unable to get this to work with alarm manager, I am unsure as to how i should link the alarm intent to the broadcast receiver. Could anyone point me to an example of registering an alarm manager broadcast receiver dynamically in the activity? Or explain how i would do this?

Thanks for any help

A: 

Why is the action of intent filter null? With this code which intents do you think your receiver will listen to.

Tushar Tarkas
Hi Tushar,I know the action shouldnt be null, I'm just not sure what it should be? When i was using a separate class for my broadcast receiver I could specify that class when declaring my alarm. But when I have my broadcast receiver in the same activity I'm not sure how to link the alarm intent to the broadcast receiver?Thanks for your reply
Shane