I'm using a BroadCastReceiver for triggering alarm which is working fine for like 2-3 days, after that it stops working, I am not receiving any alerts in my BroadcastReceiver.
Is there a lifeSpan for Pending Intent?
Actually I'm creating alarms for 30 days. I'm using PendingIntents. Here is the code for PendingIntent:
PendingIntent pendingIntentScheduler = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntentScheduler);
and the receiver at BroadcastReceiver:
#Override
public void onReceive(Context context, Intent intent) {
this.context = context;
Log.d("onReceive", "this is broadcast reciever");
}
Android Manifest Permission:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application>
<receiver android:name=".medicine.modal.AlarmReceiver" android:permission="android.permission.WAKE_LOCK"
android:enabled="true" android:exported="true" android:process=":remote">
<intent-filter>
<action android:name="com.healthsaverz.nimap.healthmobile.healthsaverz.mainscreen.controller.NotificationActivity" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
I found a solution, not legitimate but a bypass. May be this will help someone. What I did is put a repeating Alarm which repeats at every 24 hours, in which I'm recreating all the Pending Intents. Since the life of pending intent is 2-3 days (According to what i found in my app). This results in creating new Pending Intents everyday.
Related
I am using AlarmManager in order to set alarm.
I have a Service, and in that Service I have a Broadcast Receiver.
The problem is, when selected time and date comes, receiver cannot receive it.
onStart part of my Service class, and where is register my receiver:
public int onStartCommand(Intent intent, int flags, int startId) {
super.onCreate();
IntentFilter intentFilter = new IntentFilter();
registerReceiver(receiver, intentFilter);
Log.i("com.example.adama", "Service has started!");
return START_STICKY;
}
onReceive Method for receiver:
public static BroadcastReceiver receiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("com.example.adama", "Receiver has received!"); ...}
Part that I run service and set alarm with AlarmManager:
Intent serviceIntent = new Intent(this, BrService.class);
startService(serviceIntent);
Intent intent = new Intent(this, BrService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,alarmCode++, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.adama.wifiv2_spinner">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.wifi" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".BrService"/>
</application>
What am I missing?
I am trying this since I want receive it even user closes the app.
If the user force-close your app, there is nothing you can do to make your receiver run, other than the user restarting your app manually.
Other than that, please make sure that you added the BOOT_COMPLETED action to your intent filter, as well as the RECEIVE_BOOT_COMPLETED permission to your manifest, as described in this thread. This will let your receiver start up again after the phone has been rebooted (turned on and off again).
Android BOOT_COMPLETED not received when application is closed
Furthermore, on this thread it has been mentioned that in order for your receiver to work after the app has been closed (not force-closed), the receiver needs to be registered in the manifest:
Is there a way to keep a repeating alarm working after existing the app that uses a broadcast receiver?
Give it a try and let us know if it helped you :)
Long time = prefs.getLong("dailynoti", 0);
Intent intent = new Intent(cn, NotificationReceiver.class);
intent.putExtra("Desc", "Description...");
PendingIntent pendingIntent = PendingIntent.getBroadcast(cn, 1055, intent,0);
AlarmManager alarmManager = (AlarmManager) cn.getSystemService(cn.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,time, AlarmManager.INTERVAL_DAY, pendingIntent);
and my mainifest file
<receiver
android:name="com.mypackage.NotificationReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.REBOOT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
And this same code is use in same project but now in same project another module it will not work Broadcast Receiver isn't call at given time
my notification code is in Receiver file but Receiver class isn't call.
I checked all things like time is also get properly. so what is the problem . please help...
I used many solution like
PendingIntent pendingIntent = PendingIntent.getBroadcast(cn, 1055, intent, PendingIntent.FLAG_CANCEL_CURRENT
//FLAG_NO_CREATE
//FLAG_UPDATE_CURRENT
One approach that I think that will work is instead of alarmManager.setRepeating() use alarmManager.set() and when the alarm triggers then set the next alarm. This will give you almost accurate results. Why almost? Because due to Doze mode introduced in Android M. So this will give you between 0 to +5 min delay.
I have a alarm manager that call a particular service after every 1 minute which do some calculations and send a broadcast to broadcast receiver. I have done this using alarm manager setRepeating().
Here it my code:
Alarm manager:
Intent serviceIntent = new Intent(this, PrayerNotifyService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, serviceIntent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60000, pintent);
It works fine but after rebooting a mobile phone, alarm manager not calling the service.
I have searched on internet and found some solutions like below but they didn't worked for me.
i have also give Boot Complete permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
My Receiver in Manifest:
<receiver
android:name=".NotificationReceiver"
android:enabled="true"
android:exported="true" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</receiver>
Can any one tell me how to solve this issue?
Your broadCast should start service, not alarmManager.
1)Boot device
2)start service from bootReceiver
3)in service start alarmManager.
Hope it helps.
I have an application developed for jelly bean, where I schedule an event to be executed in the future using Alarm manager. The scheduled event executes as expected as long as the application runs in the foreground or in the background. But once I force close the application under taskmanager, I am no longer able to receive the broadcast from the alarm manager.
As suggested by various posts and blogs i tried using Intent.Flag_Include_Stopped_Packages. But it was of no use. Including this flag in the intent works only for sendBroadcast(intent). But in case of an alarm manager where pending intent is used,it does not work.
My code to schedule the alarm
Intent intent = new Intent("com.dummy.intent");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),dummyId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, scheduledAlarm.getTimeInMillis(), pi);
My mainfest
<receiver android:name="com.example.androidScheduling.alarmReceiver"
android:enabled="true"
android:exported="true"
android:process=":remote">
<intent-filter>
<action android:name="com.dummy.intent"></action>
</intent-filter>
</receiver>
Can someone please help me out?
I even tried including android:process = ":remote" for the receiver in manifest. But even that did not help.
I think you have not spelled correctly the intent's action name in manifest and programmatically .
In pro-grammatically
Intent intent = new Intent("com.dummy.intent");
Manifest file-
<intent-filter>
<action android:name="com.custom.intent"></action>
</intent-filter>
The action name to intent and declared in manifest must require same.
Hope this helpful to you
I use Broadcast Receiver, WidgetProvider and Configure Activity. Activity starts during the creation of a widget. Widgets are updated every 20 seconds.
private PendingIntent service = null;
Intent intent = new Intent(context, AlarmReceiver.class);
if (service == null) {
service = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
m.setRepeating(AlarmManager.RTC, TIME.getTime().getTime(), 1000 * 20,
service);
I whant to update widgets when I change time or timezone on my phone. Widgets are updated when I change the time ahead. But when I change the time back, nothing happens.
I've already added to the manifest
<receiver android:name=".AlarmReceiver" >
<intent-filter>
<action android:name="android.intent.ACTION_TIMEZONE_CHANGED" />
<action android:name="android.intent.ACTION_TIME" />
</intent-filter>
</receiver>
In the Broadcast Receiver I'm updating widgets.
Please refer to
http://developer.android.com/reference/android/content/Intent.html#ACTION_TIMEZONE_CHANGED
They should be
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.TIME_SET" />