Broadcastreceiver not receiving triggered Alarm - android

I have the following problem. I have 2 classes. 1 is called AlarmService and the other is called TimeAlarm which extends BroadcastReceiver.
The App should do the following thing: It should generate a new Alarm to a time specified in the preferences (Which it already does...) also in Logcat i can see how the Alarm gets triggered. But the problem is, that the Notification which should be shown does not show up in the StatusBar.
Here is all the code i have for this:
AndroidManifest.xml:
<receiver android:name="com.ikalma.alarmmanager.TimeAlarm">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
AlarmService.java:
private Context context;
private PendingIntent mAlarmSender;
public AlarmService(Context context) {
this.context = context;
Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
notifyIntent.setClass(context, myActivity.class);
mAlarmSender = PendingIntent.getBroadcast(context, 0, notifyIntent, 0);
}
public void startAlarm(int stunde, int minute) {
Calendar updateTime = Calendar.getInstance();
updateTime.set(Calendar.HOUR_OF_DAY, stunde);
updateTime.set(Calendar.MINUTE, minute);
updateTime.set(Calendar.SECOND, 00);
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, mAlarmSender);
}
TimeAlarm.java:
#Override
public void onReceive(Context context, Intent intent) {
Log.e("TEST", "onReceive() called...");
}
The Receiver in the Manifest is inside of the Tag so that should not be a problem.
The problem is, that if i restart my device, it gets called. But not if an Alarm gets triggered.
But the onReceive() method should also be called if an alarm gets triggered, shouldn't it?
Thanks for your help!

your intent filter is only listening to boot complete intents and not your own alarm broadcast intent action. update your intent filter so that your broadcast intent is also received (that means for your special case add the action of Intent.ACTION_MAIN to your intent filter)
<receiver android:name="com.ikalma.alarmmanager.TimeAlarm">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</receiver>

Related

AlarmManager is not triggered after complete bootup

I've used an alarmManager to send daily notification. Since alarmManager stops after restarting phone so I've created a BroadcastReceiver to trigger on BOOT_COMPLETE, still no success.
Even not getting toast.
BroadcastRreceiver class
package com.aman.dailynoti;
import...
public class BReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ((intent.getAction()).equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "broadcast", Toast.LENGTH_SHORT).show();
SharedPreferences mpreferences=context.getSharedPreferences("myPreferences",MODE_PRIVATE);
int h=mpreferences.getInt("hour",14);
int m=mpreferences.getInt("minute", 30);
Calendar calendar= Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,h);
calendar.set(Calendar.MINUTE,m);
calendar.set(Calendar.SECOND,00);
Intent notiIntent = new Intent(context, Notification_Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, notiIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application...>
<receiver
android:name="com.aman.dailynoti.BReceiver"
android:enabled="true"
android:label="breceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
Dont quote me on this but I believe android only gives a small window of time for your applications to do what they need to do on boot. If you want the user to receive notifications when not using the app look into push notifications.
you should use also intent filter android.intent.action.QUICKBOOT_POWERON to recieve after restart

Alarm manager trigger every time app running

I want to create a repeating alarm from AlarmManager which is triggered at 21:00 every day to show a notification. So i create a service and declare that in manifest, in the service class i wrote a method for schedule repeating alarms.
public static void setRecurringAlarm(Context context) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, NotificationService.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
am.cancel(pi);
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getDefault());
updateTime.set(Calendar.HOUR_OF_DAY, 21);
updateTime.set(Calendar.MINUTE, 00);
updateTime.set(Calendar.SECOND, 00);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP,
updateTime.getTimeInMillis(),
AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
}
(For test i set the delay INTERVAL_FIFTEEN_MINUTES).
I call this method at onResume() of my splash screen activity and in a BroadcastReceiver that is set up to receive BOOT_COMPLETED.
#Override
protected void onResume() {
super.onResume();
NotificationService.setRecurringAlarm(this);
}
this is my BroadcastReceicer :
public class NotificationBootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
NotificationService.setRecurringAlarm(context);
}
}
and this is my manifest :
<receiver android:name=".notification.NotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".notification.NotificationService" />
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
the problem is every time i open the app and splashscreen running the alarm triggered and notification will be shown.

My broadcast receiver is not receiving

I have used broadcast receiver with alarm manager to hit webservices for every 60 seconds,seems its working fine when i have used my broadcast receiver in different class but its not calling when i declare a receiver with in my activity
my code is below
public static void startAlarm(Context context) {
Intent locationAlarm = new Intent(context, GetLocation.class);
AlarmManager alarms = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Calendar updateTime = Calendar.getInstance();
PendingIntent recurringAlarm = PendingIntent.getBroadcast(context, 0,
locationAlarm, PendingIntent.FLAG_CANCEL_CURRENT);
alarms.setRepeating(AlarmManager.RTC_WAKEUP,
updateTime.getTimeInMillis() + 500, UPDATE_INTERVAL,
recurringAlarm);
}
Broadcast receiver :
public class GetLocation extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
LogUtil.d("Started Service");
}
}
My Manifest file :
<receiver android:name="com.sample.sample.listeners$GetLocation" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
kindly correct my mistake. my Started service method never called
You have a typo in your manifest file. You have put $GetLocation instead of .GetLocation
<receiver android:name="com.sample.sample.listeners.GetLocation" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

BroadcastReceiver not working when app is not running

In my manifest file I have declared the receiver. (as follows)
<receiver android:name=".OnAlarmReceive" />
however, once I shut down my application, I am not able to get the alarms and the notifications. Apparently, a call to the OnReceive in my Broadcast receiver is never made.
public class OnAlarmReceive extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent arg1)
{
//various stuff
}
}
Inside the MainActivity, my alarm manager class is as the follows.
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent("MY_ALARM_NOTIFICATION");
intent.setClass(this, OnAlarmReceive.class);
intent.putExtra("message", message);
PendingIntent pendingIntent = PendingIntent
.getBroadcast(MainActivity.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar timeCal = Calendar.getInstance();
timeCal.set(Calendar.HOUR_OF_DAY, hour);
timeCal.set(Calendar.MINUTE, minutes);
alarmManager.set(AlarmManager.RTC_WAKEUP, timeCal.getTimeInMillis(), pendingIntent);
and my manifest as is follows :
<receiver android:name=".OnAlarmReceive">
<intent-filter android:priority="1">
<action android:name="MY_ALARM_NOTIFICATION"/>
</intent-filter>
</receiver>
What should I do in order to receive the notifications/alarms even if I have shut off my app. Background service ?
you should add intent-filter in manifest,as
receiver android:name=".SmsBroadCastReceiver">
<intent-filter android:priority="20">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
As Gong Cong says, you need to declare which events your receiver should listen.
For example :
<receiver android:name=".OnAlarmReceive">
<intent-filter>
<action android:name="MY_ALARM_NOTIFICATION"/>
</intent-filter> </receiver>
and then when your set your alarm, use an intent with your action :
Intent intent = new Intent("MY_ALARM_NOTIFICATION");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pi = PendingIntent.getBroadcast( this, 0, intent, 0 );
Your code is working fine!
All you have to do is to change this line:
alarmManager.set(AlarmManager.RTC_WAKEUP, timeCal.getTimeInMillis(),
pendingIntent);
With this line:
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 5000, pendingIntent);
And the code in the "onReceive" will run after 5000ms (5sec) even when app is not running
In My understanding, In some cased depending on the way of implementations, OS has authority to adjust the alarm set time. So try to use AlarmManager.set(...), AlarmManager.setexact(...) etc accordingly. In Some cases, depending on the manufacturer(Custom Android OS), there is a possibility that the OS is blocking fire alarm.
Adding android:exported="true" for receiver in manifest file helped me to receive alarms (and thus, wake the application) even when application was shut-down (intentionally by me, removing app from task list).
1.Declare the receiver in the Manifest-file:
<receiver android:name="your.package.name.TestAlarmReceiver"></receiver>
Always remember that the whole Android-System is case sensitive. So check your spelling is correct in the AndroidMainfest.xml.
2.If you create a PendingIntent for your Receiver, please add an requestCode - even it is a random number! Without your onReceive code never get called!
The function which start AlarmManager should look like below:
public static void scheduleTestAlarmReceiver(Context context) {
Intent receiverIntent = new Intent(context, TestAlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 123456789, receiverIntent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+startDelay, someDelay, sender);
}
BroadcastReceiver class:
package your.package.name;
public class TestAlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
// your code here!
}
}
The original article: Why my BroadcastReceiver does not get called?

broadcast receiver not getting called with alarm

I'm trying to call a broadcast receiver at a certain time by setting an alarm. It doesn't appear to be getting called at all. I'm trying to debug with logcat.
My set up for the alarm is something like this:
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar notifyAlarm = Calendar.getInstance();
notifyAlarm.set(Calendar.HOUR, 17);
notifyAlarm.set(Calendar.MINUTE, 00);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 2378530, intent, 0);
alarm.set(AlarmManager.RTC_WAKEUP, notifyAlarm.getTimeInMillis(), pendingIntent);
My receiver is as follows:
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG ="test receiver";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.v(TAG, "receiver called");
}
}
also in the manifest I have:
</activity>
<receiver android:name="AlarmReceiver"></receiver>
</application>
Any help appreciated.
Thinking about it, I'm not sure you can send a broadcast to a BroadcastReceiver using an explicit Intent as you are doing it...
Intent intent = new Intent(this, AlarmReceiver.class);
Try giving the <receiver section an <intent-filter> example...
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="com.mypackage.ACTION_DO_SOMETHING" />
</intent-filter>
</receiver>
Then when you create your Intent, do it as follows...
Intent intent = new Intent("com.mypackage.ACTION_DO_SOMETHING");

Categories

Resources