0

I am implement a simple alarm function, which is used to trigger some function at the specific date time.

The problem is I have set the time already, but the receiver seems never called.

Here is how I implement:......

1) in manifest :

 <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

and

<receiver android:name=".Listener.AlarmReceiver" />

2) in the main activity (I would like to trigger on 4th June 2014, 02:06 p.m.)

profilePic.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
             GregorianCalendar date = new GregorianCalendar(2014,6,4,14,6);
             long dateTime = date.getTimeInMillis();
             Log.d("test1",date.toString());
             AlarmManager alarmManager = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
             Intent intentAlarm = new Intent(ctx, AlarmReceiver.class);
             alarmManager.set(AlarmManager.RTC_WAKEUP, dateTime, PendingIntent.getBroadcast(ctx, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
        }
    });

And I have also log the time

java.util.GregorianCalendar[time=1404453960000,areFieldsSet=true,lenient=true,zone=Asia/Hong_Kong,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=6,WEEK_OF_YEAR=27,WEEK_OF_MONTH=1,DAY_OF_MONTH=4,DAY_OF_YEAR=185,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=6,SECOND=0,MILLISECOND=0,ZONE_OFFSET=28800000,DST_OFFSET=0]

3) Receiver

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("test1", "alarm");
        Toast.makeText(context, "Alarm Triggered", Toast.LENGTH_LONG).show();
    }
}

I waited until the 02:06p.m. but nothing happened, and receiver is not called. How to fix the problem? Also, is it possible to set more than one alarm, is it all I need to do is to create another datetime and fire the alarmManager.set() again, will it overwrite the old timer? Thanks for helping.

Updated

For the AlarmReceiver in the mainifest ,

I changed to

<receiver android:name="com.example.antismoke.Listener.AlarmReceiver" />

And the class is

package com.example.antismoke.Listener;

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

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("test1", "alarm");
        Toast.makeText(context, "Alarm Triggered", Toast.LENGTH_LONG).show();
    }
}

so I think package name is not the root cause? Thanks for helping

user782104
  • 13,233
  • 55
  • 172
  • 312

5 Answers5

2

Instead of using GregorianCalendar , try to use Calendar.
Try the following, it should work:

Intent i = new Intent(getApplicationContext(), AlarmReceiver.class); 
PendingIntent sender = PendingIntent.getBroadcast(AlarmSample.this, 0, i, 0);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour); // set hour
calendar.set(Calendar.MINUTE, minuite); // set minute
calendar.set(Calendar.SECOND, 0); // set seconds     
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); 
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
1

you should use package name correctly, if you have different package ,

receiver android:name="XXX.XXXXXXXXXXXXXXX.AlarmReceiver" />

check this out

Sree
  • 3,136
  • 2
  • 31
  • 39
1

1) You don't need this persmission (com.android.alarm.permission.SET_ALARM) to receive alarm.

2) Change this line:

<receiver android:name=".Listener.AlarmReceiver" />

to

<receiver android:name=".AlarmReceiver" />
Denis
  • 161
  • 2
  • 5
  • 1) Is your Receiver declared inside Application section? – Denis Jun 04 '14 at 06:40
  • Try changing you dateTime to: long dateTime = SystemClock.elapsedRealtime() + 5 * 1000; Alarm should fire in 5 seconds. If it's true, problem is in you date. Possibly Mobile phone clock is set not like you are expecting – Denis Jun 04 '14 at 06:46
0

replace alarmManager.set with alarmManager.setRepeating

or try to use this:

public static AlarmManager am = null;
public static PendingIntent sender;
Intent intent1 = new Intent(ctx, Reciver.class);
sender = PendingIntent.getBroadcast(ctx, 1, intent1, 0);
am = (AlarmManager) getSystemService(ALARM_SERVICE);
long l = new Date().getTime();
am.setRepeating(AlarmManager.RTC_WAKEUP, l, 1500, sender);
Mohammad Rababah
  • 1,730
  • 4
  • 17
  • 32
0

Ok , finally I figure out it is because the month start at 0 in GregorianCalendar. So June should use 5 instead of 6

I should have noticed the problem. Thanks for all you guys

user782104
  • 13,233
  • 55
  • 172
  • 312