i tried to write a simple service for an android device. it looks like
public void onReceive(Context context, Intent intent) {
// do something
// new alarm
setNextAlarm(context);
}
public static void setNextAlarm(Context context) {
Intent myIntent = new Intent(context, MainReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// trigger
Calendar cal = Calendar.getInstance();
// here is possible that trigger time will be different by each call
long triggerTime = computeNextTriggerTime();
long triggerAtTime = cal.getTimeInMillis() + triggerTime;
alarmManager.setInexactRepeating(AlarmManager.RTC, triggerAtTime, triggerTime, pendingIntent);
}
Well, I noticed that it works, but sometimes it crashes after days or hours of using and I'm quite sure, that it is not because of the code, that do the logic and that I didn't post here.
The trigger time is between 10 and 30 minutes.
Every help will be appreciated :)
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);
Some of my app users are complaining that my app is killing their batterys but im having a hard time figuring out the issue. Can anyone spot where I might be going wrong?
https://play.google.com/store/apps/details?id=com.walkingroutes.beta01
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent callingIntent) {
long interval = 86400000;//set interval 86400000 = 24 hours
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
long timeOrLengthofWait = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
Intent intent = new Intent(context, UpdaterService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, -1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(), interval, pendingIntent);
}
}
I want to accomplish to spawn a service every hour. This service should perform some SQL database operations. In order to do so, I've used AlarmManager like this:
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(Home.this, DeleteCaseService.class);
PendingIntent pintent = PendingIntent.getService(Home.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60*60*1000, pintent);
I haven't made the code for the service yet, but that's not the problem. My problem is kinda theoretical. If you imagine this app running for 8 hours constantly, will there will be started 8 different Service in 8 different threads? Or when will a service get killed? Is this the best approach? Feel free to suggest other solutions, but I want to keep the AlarmManager
Thanks.
Instead of using service use broadcast receiver for making background operation on database
to call the broadcast receiver use the bellow code
public class AlarmMgnr
{
private Intent intent;
Context context;
private static AlarmManager aMgnr;
private static PendingIntent sender;
public AlarmMgnr(Context context)
{
this.context = context;
}
public void registerAlarm()
{
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR);
cal.set(Calendar.HOUR, hour);
cal.set(Calendar.MINUTE, 00);
cal.set(Calendar.SECOND, 00);
System.out.println("AlarmMgnr.registerAlarm()");
intent = new Intent(context, <YourReciverCalssName>.class);
sender = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
aMgnr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// aMgnr.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, sender);
aMgnr.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 180000L, sender);
// aMgnr.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60000L, sender);
}
public void unregisterAlarm()
{
aMgnr.cancel(sender);
}
}
}
And start this alaram manage once only when application got installed .
I need help. Im new to android coding.
I have made task list, which I want to do specific things at time written in task.
Here is my task item
private long id;
private int mon;
private int tues;
private int wednes;
private int thurs;
private int fri;
private int satur;
private int sun;
private int profile;
Where I have days (monday,tuesday etc) which holds amount of minutes (for 10:00 its 600).
Following some tutorials I have alarm reciever
public class AlarmReciever extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Intent newIntent = new Intent(context, AlarmActivity.class);
newIntent.putExtra("profile", message);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
Its still unedited...
And then there is code which calls to make new tasks in alarm manager
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 5);
Intent intent = new Intent(ctx, AlarmReceiver.class);
intent.putExtra("alarm_message", "O'Doyle Rules!");
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
I dont understand how to specify in calendar, that I need new task repeating (for example) every monday at 10:00, and that when this happens, it calls new function, giving it "profile" variable to work with.
private void setProfile(Integer profile)
{
// Doing things with profile
}
take a look at the following code:
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent2 = new Intent(context, SampleAlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent2, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// Set the alarm's trigger time to item hour
calendar.set(Calendar.HOUR_OF_DAY, NuevoItemActivity.hora.getCurrentHour());
calendar.set(Calendar.MINUTE, NuevoItemActivity.hora.getCurrentMinute());
// Set the alarm to fire , according to the device's clock, and to repeat once a day.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, (PendingIntent) alarmIntent);
As you can see in last line, you are able to indicate AlarmManager.INTERVAL_DAY to repeat the PendingIntent.
I'm developing an application for mobile and tablet using android 2.3**
I want to do some operations namely Operation-A and Operation-B. Both perform some pocess.
I want to repeat the Operation-A and Operation-B is performed every 1 hour time interval
Operation-A is performed before the 10 minutes of Operation-B
Operation-B is performed when the time is 0.00,1.00,2.00,3.00,....,23.00 (I'm using railway time. So it is not confused for am or pm).
Operation-A is performed when the time is 0.50,1.50,2.50,3.50,....,23.50
The above scenarios is possible in android or not.
All are give your ideas for the above scenarios.
I'm planning to use AlarmManager. AlarmManager is android system service. It is used to notify the application every 1 hour.
I plan to use an AlarmManager for Operation-A and another AlarmManager for Operation-B.
My doubt is ,In android is it possible to use more than one AlarmManager with different repeat values in a single application?
All your ideas are welcome.
Yes this task will done with help of AlarmManager with reperating functionality. First you need to create two receiver class which will receive events . and then need to setup repeating alarm .
private void setAlarm1()
{
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.HOUR, 1);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60, pendingIntent);
}
private void setAlarm2()
{
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 50);
Intent intent = new Intent(this, AlarmReceiver_1.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*50, pendingIntent);
}
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
}
}
public class AlarmReceiver_1 extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
}
}
In menifest, you need to declare the classes as below.
<receiver android:name=".AlarmReceiver" />
<receiver android:name=".AlarmReceiver_1" />