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.
Related
I have an old application in which the service was invoked using the AlarmManager's setExact every minute, but in the latest Android O, it is not allowing me to run the service in the same way. I couldn't use foreground service because I don't want to show any notification when my service is running.
I want help on tweaking my application architecture in a way so that my code should run in the Android O with minimum changes.
When I run my application in the Android, it is crash:(.Please find the stack trace of the crash below.
java.lang.RuntimeException: Unable to create application com.***.***.***ControllerApplication: java.lang.IllegalStateException: Not allowed to start service Intent
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.***.***.qa/com.***.***.services.AlarmService
Edit:
The task in my application is time critical therefore I used setExact in AlarmManager but none of the currently supported APIs is scheduling jobs at an exact time. With that said, If I use JobScheduler/ WorkManager then my application will malfunction.
You can use the Alarm manager which will be called after every 10 mins
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.setClass(this,AlarmReceiver_.class);
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(YourClass.this, r5, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 300000L,
600000L, broadcast);
ManiFest receiver Code where you will get a receiver response
<receiver android:name="com.yourpackage.AlarmReceiver_"
>
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
You will have to create Receiver where you will receive data as an above-specified name of AlarmReceiver_.class
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 BroadcastReceiver that will run on the background regardless of my application lifecycle. This BroadcastReceiver is initiated on the manifest and launched by a PendingIntent.
I need to stop this BroadcastReceiver at some point, and i must do it inside the BroadcastReceiver it self (inside onReceive perhaps?).
I tried to call abortBroadcast() but no luck, the BroadcastReceiver is still getting called again and again.
Please kindly help me, Thanks a lot for your help
UPDATE
This is how i launch the BroadcastReceiver :
Intent intent = new Intent("mypackage.ACTION_RECEIVE_GEOFENCE");
intent.putExtra("toStation", Constants.toStation);
PendingIntent geofencePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
getGeofencingRequest(),
geofencePendingIntent
).setResultCallback(this);
And this is on my manifest :
<receiver android:name="mypackage.GeofenceUpdateReceiver"
android:exported="false">
<intent-filter >
<action android:name="mypackage.ACTION_RECEIVE_GEOFENCE"/>
</intent-filter>
</receiver>
I use Google's Geofence API
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.
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