I've tried to search around, but seems like I'm the only one who experiences such an error, so maybe someone would be able to point out what I did wrong.
I create an AlarmManager via this function:
public void startTimer() {
Intent myIntent = new Intent(MainActivity.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(MainActivity.this, 0,
myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_FIFTEEN_MINUTES / 50, pendingIntent);
Toast.makeText(MainActivity.this, "Start Alarm", Toast.LENGTH_LONG)
.show();
}
It is created as expected: OnCreate() is called then OnStart() is called.
Then I stop the timer by calling the function:
private void stopTimer() {
Intent myIntent = new Intent(MainActivity.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(MainActivity.this, 0,
myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}
And it is indeed stopped after this call.
The problem occurs afterwards, when AlarmManager's OnCreate() and OnStart() methods are called long after the app was closed with no easily seen system in those calls. Though it seems that this happens when my phone lacks free memory.
Any ideas? Thanks in advance!
Your service needs to call stopSelf() when it is done, or you need to switch to using an IntentService, which will automatically call stopSelf() when it is done. Right now, you are leaking the service, and Android is destroying and recreating it after a while.
Related
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);
Am creating an application that fetch data from webservice. I have been while(true) and sleeping the loop at a specific milliseconds.. i had like to make the service action(fetching data from webservice) to always start at a specific time.. instead on being on always and pausing by Thread.sleep(milli)... Thanks
this is what have been using
while(true)
{
///pullDataFromWebservice();
Thread.sleep(600000);
}
Use the Alarm API. It is way safer than having a sleep & poll service.
Why?
Because Android is very likely to reclaim such a long running services when resources turn low, resulting
in your delayed logic never being called!
An example for activating a callback in 1 hour from now:
private PendingIntent pIntent;
AlarmManager manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyDelayedReceiver.class); <------- the receiver to be activated
pIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
60*60*1000, pIntent); <---------- activation interval
Try using AlarmManager - A bit battery eater though
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, YourClass.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar cal= Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 19);
cal.set(Calendar.MINUTE, 20);
cal.set(Calendar.SECOND, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pIntent);
//RTC_WAKEUP to enable this alarm even in switched off mode.
I have a service which uses PendinIntent and AlarmManager to launch another activity after a fixed period of time.
Here is the relevant code of the service:
Calendar cal = Calendar.getInstance(); //Create a calendar
cal.add(Calendar.MINUTE, 1); //Add the set minutes to the alarm
Intent dialogIntent = new Intent(this, alarmRingLayout.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1234, dialogIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
When the service starts, I also set up a notification which has the a button that can cancel the service incase the user does not want the activity to launch.
On click of the "cancel" button, stopService() is called:
stopService(new Intent(StopPowerNapAlarmService.this, PowerNapAlarmService.class));
onDestroy() has the following code which cancels the notification and calls stopSelf()
It also tries to cancel the PendingIntent and AlarmManager.
The problem is that the Activity opens up even after after onDestroy is called. I believe the PendingIntent and/or AlarmManager are not getting canceled.
Here is the code for onDestroy():
#Override
public void onDestroy() {
Toast.makeText(this, "Alarm Finished", Toast.LENGTH_SHORT).show();
CancelNotification(this, 0);
//Cancel the pending intent and AlarmManager
Intent myntent = new Intent(PowerNapAlarmService.this, PowerNapAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
1234, myntent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent.cancel();
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
stopSelf();
}
What is going wrong over here?
What is going wrong over here?
Your Intent objects are different, so your PendingIntent objects are different. This, in turn, means that you are working with different alarms. The first Intent points to an alarmRingLayout.class activity. The second Intent points to a BroadcastReceiver oddly named PowerNapAlarmService.
If you want to cancel() the alarmRingLayout alarm, create an activity PendingIntent for alarmRingLayout, and use that with cancel().
Also, please get rid of stopSelf() in onDestroy(), as that is not needed and could conceivably cause problems.
I am using AlarmManager for my Timer. I have 2 classes.
In the first class (MainActivity) I am starting my alarm with the next code:
public void startAlarm(long Seconds) {
Intent myIntent = new Intent(MainActivity.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(MainActivity.this, 13141337,
myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, (int) Seconds);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
}
In another class (AlarmSound), where the alarm goes off and the phone vibrates, I'm calling my cancel Alarm method.
public void stopAlarm(){
Intent intentstop = new Intent(AlarmSound.this, MyAlarmService.class);
PendingIntent senderstop = PendingIntent.getBroadcast(AlarmSound.this,
13141337, intentstop, 0);
AlarmManager myalarm = (AlarmManager) getSystemService(ALARM_SERVICE);
myalarm.cancel(senderstop);
Log.w("Karl","stopAlarm?");
}
Is this the correct way to do this? Because my alarm constantly goes off on android 4.1.
Thanks in advance!
Your Cancel wont work. The pending Intents have to match - since they have a different context (MainActivity and AlarmSound) this cancel wont work. You have to also use getService on both.
You can
a) try to recreate a matching pending Intent
b) get the pendingIntent that started the Service in the Service and use that to cancel
But usually every Alarm just goes off once if you dont use setRepeating().
Are you sure you are terminating your Service correctly with stopself() and are giving it the right flag in the onStartCommand() Method?
This is the code:
public void startAlarm(Context context) {
Intent intent = new Intent(context, SyncService.class);
PendingIntent sender = PendingIntent.getService(context, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
10 min, 20 min, sender);
}
This is my method where I start my alarm... I use it in my main activity in onCreate method... What if I change something in settings, and I want to change a time of Repeating? How to do that? I should kill that one and start new one?
If you read the docs you will notice the following:
If there is already an alarm scheduled
for the same IntentSender, it will
first be cancelled.