Why the alarm receiver is not trigger in android? - android

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

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);

you should use package name correctly, if you have different package ,
receiver android:name="XXX.XXXXXXXXXXXXXXX.AlarmReceiver" />
check this out

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" />

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);

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

Related

alarm not triggering when app is no running

I'm setting up a daily alarm. It works if the app is running at alarm time, but does not work if the app is not running.
This is how I declare the receiver in the Manifest:
<receiver android:name="com.myAppPackage.alarm.AlarmReceiver"
android:enabled="true"
android:exported="true"/>
Studio warns me: Exported receiver does not require permission.
True I have not added an android:permission nor and Intent to the receiver and the application section doesn't have any permission tags.
And this is the broadcastreceiver:
package com.myAppPackage.alarm;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
public class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver(){}
#Override
public void onReceive(Context context, Intent intent) {
final Intent syncIntent = new Intent(context, AlarmActivity.class);
syncIntent.addFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(syncIntent);
}
}
The alarm is configured in the following method (in this example configured to set-off daily inexact at 13:48):
public static void configureDailySync(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
final int hourOfDay = 13;
final int minuteOfHour = 48;
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minuteOfHour);
alarmManager.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
alarmPendingIntent);
}
Any help is very welcome! Thank you!!
news: getting closer... if I copy the alarm-setting code in the MainActivity onCreate() method it works! This is the code I copied:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
final int hourOfDay = 13;
final int minuteOfHour = 48;
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minuteOfHour);
alarmManager.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
alarmPendingIntent);
When the alarm is created as above in the onCreate() of the MainActivity then it DOES trigger even when the app is closed...
But when I call the call the method from the MainActivity like this:
MyAppAccount.configureDailySync(this);
it doesn't work!
MyAppAccount is an plain class not extending anything... I've tried to have MyAppAccount extend AppCompatActivity in case it mattered but nothing...
Oh well... it seems that the above try of executing the alarm-setting in the onCreate() method of the MainActivity is not always working... what is most puzzling!!! :-(
SOLVED: It had nothing to do with coding!! The problem was the way I was closing the application. When closing the application using the stop button of Android Studio the alarm is NOT set. When closing the application from the phone, using the back button for instance and / or removing the application from the list of applications (with the square button), then the alarm works!
Why? No idea...

Alarm Manager set Daily Alarm at a specific time?

I've been working on this application that is supposed to run at a given time daily, except on weekends. I've used an AlarmBroadCastReceiver to fire a certain block of code at a given time. I have this code in my AlarmBroadCastReceiver class:
public void SetAlarm(Context context) {
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 0, pi);
}
Basically I try and set a repeating alarm on that time.
Then at a press of a button, I have this in my MainActivity where I'm calling it from:
public void onStartAlarmClicked(View view){
Context context = this.getApplicationContext();
if(alarm != null){
Log.e(TAG, "starting alarm");
alarm.SetAlarm(context);
}else{
Log.e(TAG, "Alarm is null");
}
}
where alarm is an object of the AlarmBroadCastReceiver class.
The problem I have with it is that the code only fires once. Once it hits 2:30, it fires. However, when I set the time back to 2:29 and wait for 2:30, or set the date 1 day forward and then set the time to 2:20 and wait for 2:30, the code no longer fires.
I have a feeling I'm overlooking something rather simple with regards to setting the alarm time but I can't see it right now.
Found a better answer from this answer. Tweaked it a bit.
public void Set12nnAlarm(Context context) {
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
// every day at 9 am
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// if it's after or equal 9 am schedule for next day
if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 9) {
Log.e(TAG, "Alarm will schedule for next day!");
calendar.add(Calendar.DAY_OF_YEAR, 1); // add, not set!
}
else{
Log.e(TAG, "Alarm will schedule for today!");
}
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
}
You have to setup a BroadcastReceiver to listen to time and date changes, and then reset the alarm.
As you want the receiver to be triggered at all times, it is better to set in the Manifest.
Create class in own file
public class DateTimeChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_TIME_CHANGED) ||
action.equals(Intent.ACTION_TIMEZONE_CHANGED) ||
action.equals(Intent.ACTION_DATE_CHANGED))
{
resetAlarm();
}
}
}
And in the Manifest
<receiver android:name=".DateTimeChangeReceiver ">
<intent_filter>
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.DATE_CHANGED" />
</intent_filter>
</receiver>

Generating alarm at specific day each week at a specified time android

I am using the following code to trigger an alarm at 08:45 am on every monday.
Calendar cl1 = Calendar.getInstance();
cl1.set(Calendar.DAY_OF_WEEK, 2);
cl1.set(Calendar.HOUR, 8);
cl1.set(Calendar.MINUTE, 45);
cl1.set(Calendar.SECOND, 0);
long tme = cl1.getTimeInMillis();
Intent intent = newIntent(Mondayentry.this,Alarmreceiver.class);
PendingIntent pendingintent = PendingIntent.getBroadcast(Mondayentry.this,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager al = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
al.setRepeating(AlarmManager.RTC_WAKEUP, tme,7*1440*60000, pendingintent);
And I have used the following broadcast class
public class Alarmreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Alarm generated", Toast.LENGTH_SHORT).show();
}
}
I have also added in the manifest file
<receiver android:name=".Alarmreceiver"> </receiver>
There is no error but the alarm is not triggered at all.
Please help me.

AlarmManager inside BroadcastReceiver when BOOT_COMPLETED

I have a Service "GroupsTaskAlarmChecker" that is called every 20 seconds by AlarmManager in onCreate of Groups.class activity this way:
int seconds = 20;
Intent myIntent = new Intent(Groups.this, GroupsTaskAlarmChecker.class);
pendingIntent = PendingIntent.getService(Groups.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), seconds * 1000, pendingIntent);
This works perfectly. But I need to do that when device boot.
Then I know I have to make AndroidManifest like this:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".ReceiverBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
<category android:name="android.intent.category.HOME">
</category></action></intent-filter>
</receiver>
and then mi broadcastReceiver like this:
public class ReceiverBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int seconds = 20;
Intent myIntent = new Intent(context, GroupsTaskAlarmChecker.class);
pendingIntent = PendingIntent.getService(context, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), seconds * 1000, pendingIntent);
}
}
but inside this onReceive I dont know how can I do the same that I did before (with intent and alarmManager to start the service each 20 seconds).
Error in this line:
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Is possible that I can't make an AlarmManager inside BroadcastReceiver?
I thank you all, I am an Android begginer and I need your help. Sorry for my english ;)
To summarize the answers and comments above: the onReceive handler receives a context which can be used to access getSystemService and ALARM_SERVICE. Sample code:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Start periodic service.
Calendar cal = Calendar.getInstance();
Intent srvIntent = new Intent(context, MyService.class);
PendingIntent pIntent = PendingIntent.getService(context, 0, srvIntent, 0);
// Use context argument to access service
AlarmManager alarm =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
// Repeat every 5 seconds
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
5000, pIntent);
}
}
}
Create a new class with this code and of course change MyReceiver and MyService to the names in your implementation.
ALARM_SERVICE is neither defined in the class ReceiverBoot nor in BroadcastReceiver.
You should reference Context.ALARM_SERVICE as the argument for getSystemService(String).
Here is a little contribution, which I believe that can add a more complete vision about achieving the goal of this question.
First: configure a "receiver" inside of the AndroidManifest from your app.
<receiver
android:name=".AlarmBroadcastReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Second: with a Class that EXTENDS the Abstract Class BroadcastReceiver, you should determine if the Intent Action was "BOOT_COMPLETED". If the condition is satisfied, you can call a method from your class which has all the construction to your Alarm.
See the following snippet bellow.
public class AlarmBroadcastReceiver extends BroadcastReceiver {
String TAG = "ALARMS";
String CLASS = this.getClass().getSimpleName() + ": ";
Context alarmContext;
#Override
public void onReceive(final Context context, Intent intent) {
Log.d(TAG, CLASS + "[START] onReceive()... ");
alarmContext = context;
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Log.d(TAG, CLASS + "BOOT_COMPLETED action has been received.");
setAlarmOnBoot();
}
Log.d(TAG, CLASS + "[END] onReceive()... ");
}
public void setAlarmOnBoot() {
Log.d(TAG, CLASS + "[START] - setAlarmOnBoot()");
final long beginAt = SystemClock.elapsedRealtime() + 60 * 1000;
final long interval = 300000; // 5 minutes
try {
AlarmManager alarm = (AlarmManager) alarmContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(alarmContext, AlarmBroadcastReceiver.class);
PendingIntent pIntent = PendingIntent.getService(alarmContext, 0, intent, 0);
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, beginAt, interval, pIntent);
Log.d(TAG, CLASS + "the Alarm has been configured successfully (5 minutes) of interval.");
} catch (Exception e) {
Log.d(TAG, CLASS + "an exception has ocurred while setting the Alarm...");
e.printStackTrace();
}
Log.d(TAG, CLASS + "[END] - setAlarmOnBoot()");
}
}
in your onReceive:
if ("android.intent.action.BOOT_COMPLETED".equals (intent.getAction())){
//start it again here
}

BroadcastReceiver is not called

I have to following code to call the BroadcastReceiver:
public class WorkItemAlarmManager {
...
public void initAlarm(HelpMe helpMe, String delay,String period,WorkItem workItem){
//HelpMe = Activity
helpMe.registerReceiver(new WorkItemAlarmHandler() , new IntentFilter("WORK_ITEM_ALARM"));
Intent intent = new Intent("WORK_ITEM_ALARM");
intent.setClass(helpMe, WorkItemAlarmManager.class);
intent.putExtra("work_item", workItem);
PendingIntent mAlarmSender = PendingIntent.getService(helpMe, 0,intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//System.out.println("START"+System.currentTimeMillis());
calendar.add(Calendar.SECOND, calcDelayMillis(delay));
//System.out.println("END"+calendar.getTimeInMillis());
AlarmManager am = (AlarmManager) helpMe.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), mAlarmSender);
}
...
}
and the BroadcastReceiver:
public class WorkItemAlarmHandler extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
....
}
}
But the WorkItemAlarmHandler is never called.
Update:
I tried to register the BroadcastReceiver in the manifest at first. But that didn't work:
<receiver
android:name="de.helpme.alarm.WorkItemAlarmHandler"
android:enabled="true"
android:label="WorkItemAlarmHandler" >
<intent-filter>
<action android:name="WORK_ITEM_ALARM" />
</intent-filter>
</receiver>
Thanks for your answers :). Now it works - but i dont yet know which one is the correct because i implemented both answers at once. If a figure out which was the right one i mark the right as answer.
Update:
The cause of the problem as i see it was:
PendingIntent mAlarmSender = PendingIntent.getService(helpMe, 0,intent, 0);
With this code it works fine:
PendingIntent mAlarmSender = PendingIntent.getBroadcast(helpMe, 0, intent, 0);
Thanks again for all your help!
try this :
In WorkItemAlarmManager ;
public class WorkItemAlarmManager {
...
public void initAlarm(HelpMe helpMe, String delay,String period,WorkItem workItem){
Intent intent =new Intent(helpMe, WorkItemAlarmHandler.class);
intent.setAction("WORK_ITEM_ALARM");
intent.putExtra("work_item", workItem);
PendingIntent mAlarmSender=
PendingIntent.getBroadcast(helpMe, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//System.out.println("START"+System.currentTimeMillis());
calendar.add(Calendar.SECOND, calcDelayMillis(delay));
//System.out.println("END"+calendar.getTimeInMillis());
AlarmManager am = (AlarmManager) helpMe.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), mAlarmSender);
}
...
}
and in WorkItemAlarmHandler:
public class WorkItemAlarmHandler extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("WORK_ITEM_ALARM")){
Toast.makeText(context, "WORK_ITEM_ALARM", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context, "repeating alarm",
Toast.LENGTH_LONG).show();
}
}
}
Your code uses:
intent.setClass(helpMe, WorkItemAlarmManager.class);
but with the setClass(...) method you specify the class that should finally handle the intent.
Therefore you should write:
intent.setClass(helpMe, WorkItemAlarmHandler.class);
As far as I remember, if you specify the class or component manually (eg. with the setClass() method) this object has to be specified in the manifest, so make sure the receiver is mentioned there.

Categories

Resources