Intent in2 = new Intent(context, ReminderService.class);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, in2, 0);
AlarmManager alarmManager2 = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), 0, pendingIntent2);
and the reminderservice.java include the following code
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Context context = getApplicationContext();
Intent in = new Intent(this, RemindBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, in, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), 1000, pendingIntent);
}
First of all I suggest you to just use only RemindBroadcastReceiver. There isn't any need of ReminderService
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), 1000, pendingIntent);
Here you have set repeated alarm which triggered after every 1 second. You need to use alarmManager.set instead of alarmManager.setRepeating.
I think bellow code will be helpful for you:
/------------------- CALLING PART
Intent in2 = new Intent("MY_ALARM_ACTION");
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 0, in2, 0);
AlarmManager alarmManager2 = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
//alarmManager2.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), pendingIntent2);
alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (5 * 1000), AlarmManager.INTERVAL_DAY, pendingIntent2);
}
/-------------------
/-------------------------- RemindBroadcastReceiver ----------------
public class RemindBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction() == "MY_ALARM_ACTION")
{
// TODO Enter your code here
}
}
}
You didn't explain what you want this code to do so we can't really know, but these are some errors I see anyway:
I assume that ReminderService is a subclass of Service, therefore you should use PendingIntent.getService rather than PendingIntent.getBroadcast.
In the first piece of code you call setRepeatig with the parameter intervalMillis = 0, and never calling AlarmManager.cancel, which I guess is an error because Android won't trigger your alarm forever, non stop.
Why are you starting a service just so you can set another alarm to trigger something else inside it?
Please explain the purpose of your code.
Related
I have used alarm manager in Android for showing some notifications, but it is not working properly.
My code in activity is:
Intent i = new Intent(this, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);
AlarmManager alarmManager = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (int) (10*60 * 1000),
pendingIntent);
And the code for receiving a notification after 15 minutes is:
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getService(context, 0, intent, 0);
am.cancel(pi);
am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
(int) ( 5*60 * 1000), pi);
use this type of AlarmManager when you use service than you need to change getService insted of getBroadCast. sometime receiver not call and also creat issue in some devices when app in background
Intent intent = new Intent(myContext, SetAlarmService.class);
intent.putExtra("salah",i);
intent.putExtra("repeat",HowManyTimeRepeatAlarm);
PendingIntent pendingIntent = PendingIntent.getService(
myContext, i, intent, 0);
AlarmManager alarmManager = (AlarmManager) myContext.getSystemService(ALARM_SERVICE);
// alarmManager.cancel(pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, reset_cal.getTimeInMillis(),60000 * 2 ,
pendingIntent);
I want to perform a database operation at specific moments in time and use an alarm manager and a pending intent in conjunction with a broadcast receiver.
This works, but when there are multiple intents that the same broadcast receiver has to receive within a short timeframe (f.ex. within a minute), onreceive() only gets called once.
Intent intent2 = new Intent(this, CustomBroadcastReceiver.class);
intent2.putExtra("taskId", currentTask.getTaskId());
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() +
((long) dataSource.fetchTask(task.getTaskId()).getTaskTime() * 60 * 1000), pendingIntent1);
and the onreceive method of the broadcastreceiver:
#Override
public void onReceive(Context context, Intent intent) {
dataSource = new DataSourceClass(context);
dataSource.open();
int taskId = intent.getIntExtra("taskId", 0);
dataSource.setTaskStatus(3, taskId);
dataSource.setCatInActive(dataSource.fetchTask(taskId).getTaskCategory())
}
Any suggestions on what I'm doing wrong?
Instead of set() you can use setExact() after KITKAT:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC, cal.getTimeInMillis(), pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC, cal.getTimeInMillis(), pendingIntent);
}
The second parameter in getBroadcast() is the requestCode, which works as if it was an ID, so, each PendingIntent should have a unique requestCode (aka ID). All your pendingIntent1's have the same requestCode (0), I'm not sure if that's what's causing the issue but it could be, you can set a diffrent requestCode for each pendingIntent like this:
int i = sharedPrefs.getInt("i", 0);
Intent intent2 = new Intent(this, CustomBroadcastReceiver.class);
intent2.putExtra("taskId", currentTask.getTaskId());
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, i++, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() +
((long) dataSource.fetchTask(task.getTaskId()).getTaskTime() * 60 * 1000), pendingIntent1);
sharedPrefs.edit.putInt("i", i).apply();
I know this is very simple and can do anyone but i m stuck in this plz help where i did mistake
one think very important i start from one.class and stop in second.class and i can do stop after 3rd or 4th day of start.
i start alarm like this
//set Alarm method
private void SetAutoCleanningService()
{
long when= System.currentTimeMillis() + 1000*20;
long repeattime =Constant.NOTIFICATION_TimeDiff;
if(Constant.IsDebug)
System.out.println("time to trigger is ="+getDate(when, "dd/MM/yyyy hh:mm:ss.SSS"));
Intent intent = new Intent(this, AutoCleaningBroadCast.class);
intent.setAction("systweak.AutoCleanningBroadCast");
PendingIntent pendingIntent = PendingIntent.getBroadcast(ApplicationSetting.this,1234567, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when, repeattime,pendingIntent);
}
And stop like this
//close Alarm method
private void StopEarlierAlarm() {
// TODO Auto-generated method stub
AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getBaseContext(), AutoCleaningBroadCast.class);
PendingIntent pIntent = PendingIntent.getBroadcast(ApplicationSetting.this, 1234567, intent,0);
aManager.cancel(pIntent);
}
i use same request code but cant able to stop and on every alarm i show one notification that will be repeat in every alarm calling
You can cancel the alarm like this:
Intent intent = new Intent(this, Mote.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1352, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Also, you should remove Intent.FILL_IN_DATA from your call to getBroadcast() in the code where you set the alarm.
Cancel your PendingIntent too.
pIntent.cancel();
Make sure context used in creating intent is same.
If not possible to use same Context, then create intent as a global Static variable and use later.
Check this, it may help you
Step 1:initialize the alarm manager.
AlarmManager alarmManager1;
Step 2:
// for Notification Process
Intent myIntent1 = new Intent(Settings.this,MyNotificationService.class);
pendingintent2 = PendingIntent.getService(SettingsPage.this, 2, myIntent1, 2);
alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar1Notify = Calendar.getInstance();
calendar1Notify.setTimeInMillis(System.currentTimeMillis());
calendar1Notify.set(Calendar.HOUR_OF_DAY, 8);
calendar1Notify.set(Calendar.MINUTE, 15);
alarmManager1.set(AlarmManager.RTC_WAKEUP,calendar1Notify.getTimeInMillis(), pendingintent2);
long time24h = 24 * 60 * 60 * 1000;
alarmManager1.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar1Notify.getTimeInMillis(), time24h,pendingintent2);
Step 3-Stop alarm manager:
alarmManager1.cancel(pendingintent2);
I'm testing AlarmManager to use in my app, and it is firing my Broadcast Receiver immediately when I want it to fire after 1 minute. The code is below:
public class SetMealTimersActivity extends Activity {
PendingIntent pi;
BroadcastReceiver br;
AlarmManager am;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_meal_timers);
br = new BroadcastReceiver() {
#Override
public void onReceive(Context c, Intent i) {
Toast.makeText(c, "Ready to Go!", Toast.LENGTH_LONG).show();
}
};
registerReceiver(br, new IntentFilter("com.ian.mealtimer"));
pi = PendingIntent.getBroadcast(this, 0, new Intent(
"com.ian.mealtimer"), 0);
am = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
am.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() +
60 * 1000, pi );
}
try :
am.set(AlarmManager.RTC_WAKEUP,
Calendar.getInstance().getTimeInMillis()+60*1000, pendingIntent);
it is working for me.
If using an exact alarm, make sure it's time is in the future. Otherwise it will fire immediately.
Try changing SystemClock.elapsedRealtime() to System.currentTimeMillis() and AlarmManager.ELAPSED_REALTIME_WAKEUP to AlarmManager.RTC_WAKEUP.
Try to use AlarmManager.setExact(int, long, PendingIntent) if you use Android API > 18 or compile with API < 19, because the time management for this methods changed with API 19. Maybe that helps. Read the documentation for more information.
make id for pendingIntent as this
pendingIntent = PendingIntent.getActivity(this, 999123266,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
All example
public void setAlarm_sat(int dayOfWeek1) {
cal1.set(Calendar.DAY_OF_WEEK, dayOfWeek);
Intent intent = new Intent(this, RemmemberActivity.class);
pendingIntent = PendingIntent.getActivity(this, 999123266,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Long alarmTime = cal1.getTimeInMillis();
AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,
alarmTime,7*24 * 60 * 60 * 1000,
pendingIntent);
// am.set(AlarmManager.RTC, cal1.getTimeInMillis(), pendingIntent);
}
I set alarm from one class using this code
Intent myIntent = new Intent(ClassOne.this, AlarmService.class);
pendingIntent = PendingIntent.getService(ClassOne.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(currentDate.getTime());
long when = calendar.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when, (7 * 24 * 60 * 60) * 1000, pendingIntent);
Now I need to cancel the pending alarms from another class. Can I just use this code?
Intent myIntent = new Intent(ClassTwo.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(ClassTwo.this, 0, myIntent, 0);
AlarmManager alarmManagerCancel = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManagerCancel.cancel(pendingIntent);
Or there is a better/proper way to cancel pending alarms?
As long as the PendingIntent are equivalent as the one you used to register the alarm, it doesn't matter where you call cancel() from. Two PendingIntents are equivalent (equals() returns true) if the underlying intents and request codes are the equivalent.