Pol Hallen
Pol Hallen

Reputation: 1872

alarmmanager selected time and date

Using this code, I try to set alarmmanager to stars Notify.class at 25/12/2012 - 15.15 but when I use this code no Notify.class starts. Where's the problem?

Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());

    cal.set(Calendar.DATE,25); 
    cal.set(Calendar.MONTH,Calendar.DECEMBER);
    cal.set(Calendar.YEAR,2012);
    cal.set(Calendar.HOUR_OF_DAY, 15);
    cal.set(Calendar.MINUTE, 15);     
    cal.set(Calendar.SECOND, 00);     

Intent intent3 = new Intent(context, Notify.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,intent3, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

Manifest:

<activity android:name="Notify"></activity>
<receiver android:name="AlarmReceiver" >
            <intent-filter>
                <action android:name="com.example.AlarmReceiver" />
            </intent-filter>

Upvotes: 1

Views: 804

Answers (2)

logray
logray

Reputation: 2332

If you are just trying to start the Activity Notify.class, then just change this line:

PendingIntent.getBroadcast

to

PendingIntent.getActivity

Also you should change this line in your manifest:

from:

<activity android:name="Notify"></activity>

to

<activity android:name=".Notify"></activity>

You should also provide a android:name and android:label for this activity.

If you are trying to use getBroadcast instead, you need to change this line so your receiver gets the broadcast instead of starting an activity with the pending intent. For that you would use

Intent intent3 = new Intent(context, AlarmReceiver.class);

Instead of

Intent intent3 = new Intent(context, Notify.class);

You would also want to add an action to your intent3, example:

intent3.setAction("com.example.AlarmReceiver");

That way in your AlarmReceiver you can filter for that intent and perform an action, such as launching an activity Notify.class, using a new intent.

Upvotes: 1

drulabs
drulabs

Reputation: 3121

you should write this

<receiver android:name="com.example.AlarmReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="AlarmReceiver"/>
        </intent-filter>
</receiver>

You have switched android:name for receiver and it's intent filter. You may replace com.example.AlarmReceiver with .AlarmReceiver

after you have made this change in your manifest, whatever code you have written inside onReceive() method of your broadcast receiver com.example.AlarmReceiver will get executed.

What your code will do is it will set alarm manager for the specified date-time. At that moment it will fire an intent with action name AlarmReceiver. Your Broadcast receiver is in Manifest so it will catch the broadcast and execute it's onReceive() Method.

If you want to start an activity write context.startActivity(...) inside onReceive().

Edit: also change your intent to this:

Intent intent3 = new Intent("AlarmReceiver");

Upvotes: 1

Related Questions