Using Broadcast receiver, how can i make the alarmmanager repeat evrey given day/days/time ?
public void startAlert(String time, String title) {
long timeInMillis = Long.decode(time);
intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("myTitle", title);
alarmmanager = (AlarmManager) getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(this, (int) Long.parseLong(time), intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmmanager.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
}
Use setRepeating instead of set.
Here is the proper syntax -
alarmmanager.setRepeating(AlarmManager.RTC_WAKEUP,
startUpTime, AlarmManager.INTERVAL_DAY, pendingIntent);
I think you can use setRepeating with customized Date like this another question.
alarmmanager.setRepeating(AlarmManager.RTC_WAKEUP,
startUpTime, AlarmManager.INTERVAL_DAY, pendingIntent);
Related
I have been working on setting alarms using AlarmManager.
Set:
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), reqCode, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);
cancel:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), reqCode, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
in my alarm receiver I want to be able to receive a notification which has a button that allows me to call cancelAlarm(). however I don't see the best approach to this and looking for the best and most reliable way of doing this.
I get the requestCodes so I am able tell which alarm to cancel
I am writing code to call the push notifications twice. But it only works once.
As you can see I am calling it at different times. What should I do to allow both notifications to work?
Intent intent = new Intent(MainActivity.this,ReminderBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,intent,0);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this,0,intent,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);
long timeAtButtonClick = System.currentTimeMillis();
long tenSecondsInMillis = 1000 * 10;
long nextInMillis = 1000 * 20;
alarmManager.set(AlarmManager.RTC_WAKEUP,timeAtButtonClick + tenSecondsInMillis, pendingIntent);
alarmManager2.set(AlarmManager.RTC_WAKEUP,timeAtButtonClick + nextInMillis, pendingIntent2);
You should declare different PendingIntent requestCode try this
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this,1,intent,0);
I have created a simple alarm clock.
It's working in above and below oreo+ but the issue is when I close the app in above oreo+ the alarm is not working.
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent nIntent = new Intent(getApplicationContext(),MyAlarm.class);
nIntent.setFlags(FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 1234, nIntent, 0);
//creating a pending intent using the intent
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, (int)alarmPeriodicTime, nIntent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.cancel(pendingIntent);
if (Build.VERSION.SDK_INT >= 23) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
}
Toast.makeText(this, "Alarm is set "+new Date(alarmPeriodicTime), Toast.LENGTH_SHORT).show();
Does anybody know why?
I'm trying to schedule a method to be called using AlarmManager, but it doesn't seem to be working. I've looked at other examples and theirs isn't working for me. So I'm thinking it's something from my code. Here's the AlarmManager code:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(SplashScreenActivity.this, KinectReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(SplashScreenActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 25);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
And my broadcast receiver:
public class KinectReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_notify)
.setContentTitle("Kinect")
.setColor(ContextCompat.getColor(context, R.color.colorAccent))
.setWhen(System.currentTimeMillis())
.setContentText("Your Kinects and Likes have been refilled. Now get to swiping")
.setAutoCancel(true)
.setSound(uri)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Your Kinects and Likes have been refilled. Now get to swiping"))
.setVibrate(new long[] { 100, 500, 100, 500, 100 });
Intent targetIntent = new Intent(context, MainActivity.class);
targetIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
targetIntent.putExtra("action", "main");
targetIntent.putExtra("id", "");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}
Neither the Toast nor the Notification show.
The AlarmManager code is in the first thing that runs in my launcher activity, in case that helps. Thanks
Please register ur receiver in manifeast file like this
In application tag
<receiver
android:name=".KinectReceiver">
Second question is how to cancel alarm
Here is ur answer
Intent myIntent = new Intent(MainActivity.this, AlarmActivity.class);
pendingIntent = PendingIntent.getActivity(CellManageAddShowActivity.this,
id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent.cancel();
alarmManager.cancel(pendingIntent);
The main thing that you will need is:
1).Create pending intent with the same id and appropriate intent FLAG.
2).Cancel that pending intent.
3).Cancel the alarm using alarm manager.
I've a alarm created in an OnBootReceiver like this:
public class OnBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
Intent i = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), savedIntervalAutomaticMilisInt, pendingIntent);
}
}
But to cancel it, I use this code in an Activity:
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
scManager.clearErrors();
So to set it up I use the context from the BroadcastReceiver's Context parameter, but to cancel it I use the this context from the Activity. My question: Will the alarm still be cancelled, even though the context is slightly different?
Will the alarm still be cancelled, even though the context is slightly different?
Context does not matter in this case -- it is merely a way to get to an AlarmManager.
Your choice of PendingIntent.FLAG_CANCEL_CURRENT in your cancel-the-alarm logic may cause a problem. If you run into difficulties (e.g., adb shell dumpsys alarm indicates your alarm survived the cancel), try replacing PendingIntent.FLAG_CANCEL_CURRENT with 0.
Give this a try for a global (application-wide) context object: android.content.ContextWrapper.getApplicationContext()