AlarmManager set method not working when called from boot receiver - android

I have looked everywhere for this solution most of the replies ask to set alarm in boot receiver.
i have implemented a boot receiver and started a service and set a alarm using set method.
Service is started fine but alarm is not setting.
please help i am stuck on that.
i can post some also if you want but boot receiver is working fine as i am also starting service in that
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPref = context.getSharedPreferences(
Util.SHARED_PREF_NAME, Context.MODE_PRIVATE);
long curTime = System.currentTimeMillis();
long endTime = sharedPref.getLong(Util.END_TIME, -1);
long startTime = sharedPref.getLong(Util.START_TIME, -1);
if (curTime < endTime && startTime >= curTime) {
Intent intent1 = new Intent(context, HUD.class);
PendingIntent pintent = PendingIntent.getService(context, 1987,
intent1, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarm = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, endTime, 100, pintent);
Log.e("alaram set", endTime + " " + curTime);
}
Intent service = new Intent(context, HUD.class);
context.startService(service);
}
}
MyReceiver.java
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Manifest.xml
Service started successfully after boot
but alarm.setRepeating(AlarmManager.RTC_WAKEUP, endTime, 100, pintent);
alarm is not working.
I think i clear my question.
Plz help

Intent intent1 = new Intent(context, HUD.class);
PendingIntent pintent = PendingIntent.getService(context, 1987,
intent1, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarm = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 30);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), 100, pending);
Try your setInexactRepeating instead of setRepeating that is more performance optimized method.

Related

Android: My broadcast receiver for alarm manager isn't working

I want my alarm manager to be set for a certain time to change the phone background, but the broadcast receiver isn't doing anything. Can you help tell me why?
Scheduling alarm:
public void scheduleAlarm(Context context){
Intent intent = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 34);
am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent );
Toast.makeText(context, "Alarm set", Toast.LENGTH_LONG).show();
}
Broadcast Receiver:
#Override
public void onReceive(Context context, Intent intent){
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
GrilledCheeseLookup.getGrilledCheeseJSON(grilledCheeseUrls, context);
Toast.makeText(context, "Alarm worked", Toast.LENGTH_LONG).show();
}
Enabling in manifest:
<receiver android:name=".AlarmReceiver" android:enabled="true" />
Try creating your Intent while also specifying the packageContext, using the Intent/4 constructor (you can set the uri to null).

Alarm Manager not firing on screen is off

I set alarm manager for time control.My purpose continue to time controlling on screen off. While screen is on everything is ok but when i locked to device(screen of) my broadcast receiver not receiver anything
i tried lots of different solution (in Manifest export:false or process:":remote") about similar problem but problem not solved. I need to help. I hope someone solved that problem before.
For set alarm manager
AlarmManager alarmManager = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, TimeService.class);
intent.setAction(SETUP_TIMER_FOR_NOTIFICATION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 112, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long now = System.currentTimeMillis();
long minute = 1000 * 60;
alarmManager.cancel(pendingIntent);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() , minute, pendingIntent);
My Receiver
public class TimeService extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("TimeService", "onReceive");
Intent timeIntent = new Intent(context, NotificationService.class);
timeIntent.setAction(SETUP_TIMER_FOR_NOTIFICATION);
startWakefulService(context, timeIntent);
}
}
Manifest.xml
<receiver
android:name="service.TimeService"
android:enabled="true"
android:exported="false"
>
<intent-filter>
<action android:name="alarm_timer_for_notification" />
</intent-filter>
</receiver>
setAndAllowWhileIdle this is worked when screen is off and phone in idle mode
In marshmallow introduce the doze mode for battery saving. So the alarm not fired correctly in repeat mode.
So we use
https://developer.android.com/reference/android/app/AlarmManager.html#setAndAllowWhileIdle(int,%20long,%20android.app.PendingIntent)
From marshmallow and up, alarm waked up only 10 min interval,setAndAllowWhileIdle also fire service or notification 10 minutes once when phone is idle (screen is off).
In normal mode time interval working fine (I have checked 3 min's interval)
public class TimeService extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("TimeService", "onReceive");
Intent timeIntent = new Intent(context, NotificationService.class);
timeIntent.setAction(SETUP_TIMER_FOR_NOTIFICATION);
startWakefulService(context, timeIntent);
setAlarm(context);
}
public void setAlarm(Context context)
{
Timber.v(TAG +"set Alarm");
PreferencesHelper preferencesHelper =new PreferencesHelper(context);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, TimeService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
int intervalInMinutes = preferencesHelper.getInt(context.getResources().getString(R.string.sha_loc_intervals), 1)* 60000;
if (android.os.Build.VERSION.SDK_INT >= 23) {
alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()+intervalInMinutes , pendingIntent);
} else if (android.os.Build.VERSION.SDK_INT >= 19
&& android.os.Build.VERSION.SDK_INT < 23) {
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+intervalInMinutes , pendingIntent);
} else {
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+intervalInMinutes , pendingIntent);
}
}
public void cancelAlarm(Context context)
{
Timber.v(TAG +"cancel Alarm");
Intent intent = new Intent(context, TimeService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}}
<receiver android:name=".TimeService"
android:enabled="true"
android:exported="true"
android:process=":remote">
</receiver>
In MainActivity.class
TimeService timeService =new TimeService();
timeService.setAlarm(MainActivity.this);
You want to take a permission in manifest file.
uses-permission android:name="android.permission.WAKE_LOCK"
WakeLock is a mechanism to keep the device on

Service is started via BroadcastReceiver "randomly"

I want to have an Notification on an specified Time each day.
For that I implemented an Broadcast receiver and an AlarmManager.
My doubt is that the receiver is reacting to every broadcast of the system.
Cause if I setup the alarmManager it is working fine for a while but than I got an notification nearly every hour (approximately).
And an other strange behaviour, if i go in airplane mode, the notifications doesn't show up.
public class MyReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
in the main activity
long REPEATE_TIME_MS=86400000 /*24h*/;
/* notification handling */
String syncConnPref = this.sharedPref.getString("notificationTime", "12:00");
String[] pieces=syncConnPref.split(":");
/* setup the start time for the alarm */
Calendar calendar = Calendar.getInstance();
long nowInMillisecs = calendar.getTimeInMillis();
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(pieces[0], 10));
calendar.set(Calendar.MINUTE, Integer.parseInt(pieces[1], 10));
long alarmStartTimeUnMillisecs = calendar.getTimeInMillis();
if(alarmStartTimeUnMillisecs<=nowInMillisecs)
{
calendar.add(Calendar.DAY_OF_YEAR, 1);
alarmStartTimeUnMillisecs = calendar.getTimeInMillis();
}
Intent myIntent = new Intent(this.notificationContext, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.notificationContext, 0, myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)this.notificationContext.getSystemService(Service.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), REPEATE_TIME_MS, pendingIntent);
Log.d(TAG, "Alarm setup with :"+ calendar.get(Calendar.HOUR_OF_DAY)+":"+ calendar.get(Calendar.MINUTE));
manifest.xml
<receiver android:name="com.example.whoseturn.MyReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

alarm manager stop when change system clock

I have a service that runs in background. I am using alarm manager to start this service. It works fine but when i change the system clock in my device or in the simulator the alarm manager stops.
public void startAzanService() {
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, CheckAzan.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//repeat the action every 5 secionde
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5000, pintent);
}
You can overcome this problem by Listening to the time changing Broadcast. Do the following:-
1)Create BroadcastReceiver in you Manifest file:-
<receiver android:name=".TimeChangeReceiver">
<intent-filter >
<action android:name="android.intent.action.TIME_SET"/>
</intent-filter>
</receiver>
2)Create the receiver class:-
public class TimeChangeReceiver extends BroadcastReceiver{
#Override
public void onReceive(final Context arg0, Intent arg1) {
//Call your Alarm setting code
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, CheckAzan.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//repeat the action every 5 secionde
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5000, pintent);
}
}
*Assuming that your don`t have any problem with your code :)
This link is also a good Tutorial of the BroadcastReceiver

Why does my Alarm not get called?

I am calling a Alarm every 30 seconds. It is not being called. What am I doing wrong.
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, OnAlarmReceiver.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent
.getBroadcast(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 30);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, cal.getTimeInMillis(), 1000 * 30, pi);
My OnAlarmReceiver
public class OnAlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// PullPendingRequests.acquireStaticLock(context);
Toast.makeText(context, "Don't panik but your time is up!!!!.", Toast.LENGTH_LONG).show();
Log.d("Taxeeta:PullPendingRequets", "CallService Location");
context.startService(new Intent(context, DriverService.class));
}
}
My Manifest contents for Alarm and Service
<service
android:name="com.taxeeta.DriverService"
android:enabled="true"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Light.NoTitleBar" />
<receiver
android:name="com.taxeeta.support.OnAlarmReceiver"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NOTIFY" />
</intent-filter>
</receiver>
Fixed it by changing the code to, thanks to
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 5);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent notifyintent = new Intent(this, OnAlarmReceiver.class);
notifyintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notifyintent.setAction("android.intent.action.NOTIFY");
PendingIntent notifysender = PendingIntent.getBroadcast(this, 0, notifyintent,
PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 20 * 1000,
notifysender);

Categories

Resources