Mick
Mick

Reputation: 7937

Making an alarm program - not working

I am attempting to make alarm program. So far I have written an activity in which the user can select the time he wishes the alarm to go off. This is working fine. Now I need to use the alarm manger to tell the OS to call some of my code at a certain point in the future. Just to test this in a crude way I added the following code that gets executed when I press a test button in my main activity:

Intent intent = new Intent(getApplicationContext(), to_call_when_alarm_goes_off.class);
PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0);
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarms.cancel(pIntent);

alarms.setRepeating(
        AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis()+1000, 
        AlarmManager.INTERVAL_DAY, 
        pIntent);

This should mean that some code called to_call_when_alarm_goes_off will get executed one second after I press the button.... Now this is where I'm a little confused. I'm not sure quite how/where to set up "to_call_when_alarm_goes_off". What I did was simply add a new class to my project as follows:

package com.mycompany.alarmprogram;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class to_call_when_alarm_goes_off extends BroadcastReceiver
{

    @Override
    public void onReceive(Context arg0, Intent arg1) 
    {
        // TODO Auto-generated method stub
        Log.i("ALARM","TIME TO WAKE UP!!!");

    }

}

All the code compiles, and when I press the button all the code in the first code snippet gets executed without crashing - but one second later the broadcast receiver code is not executed. Clearly I am misunderstanding something.

Upvotes: 0

Views: 330

Answers (2)

AAnkit
AAnkit

Reputation: 27549

I assume you are missing registering your receiver in Manifest file, With appropriate action string. as given below.

         <receiver android:name=".to_call_when_alarm_goes_off" > 
               <intent-filter>
                  <action android:name="com.android.whatever.WHAT_EVER_NAM_YOU_WANNA_GIVE" />
                </intent-filter>// can change name/action string as par ur requirement.
        </receiver>

you need to set same action string in your intent, Remember Action string must be same in Manifest and here intent.setAction("com.android.whatever.WHAT_EVER_NAM_YOU_WANNA_GIVE"); in java also. then only it will tringger receiver.

Your code can be changed like given below.

Intent intent = new Intent(getApplicationContext(), to_call_when_alarm_goes_off.class);
intent.setAction("com.android.whatever.WHAT_EVER_NAM_YOU_WANNA_GIVE");// added line

PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0);

AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

alarms.cancel(pIntent);


alarms.setRepeating(
        AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis()+1000, 
        AlarmManager.INTERVAL_DAY, 
        pIntent);

Upvotes: 2

vArDo
vArDo

Reputation: 4408

You need to register to_call_when_alarm_goes_off class in AndroidManifest.xml as receiver:

<receiver android:name=".to_call_when_alarm_goes_off"></receiver>

See details in API docs.

Upvotes: 0

Related Questions