How to run a service every day at noon, and on every boot
Android alarmmanager is your answer. use it with a broadcast receiver which also resets the alarms on phone wake.
Now with code example:
Setting alarm inside a method:
Intent intent = new Intent(context, AlarmReceiver.class);
intent.setAction("packagename.ACTION");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pendingIntent);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Receiver for your interval:
public class AlarmReceiver extends BroadcastReceiver {
private final String SOMEACTION = "packagename.ACTION"; //packagename is com.whatever.www
@Override
public void onReceive(Context context, Intent intent) {
Time now = new Time();
now.setToNow();
String time = FileHandler.timeFormat(now);
String action = intent.getAction();
if(SOMEACTION.equals(action)) {
// here you call a service etc.
}
Receiver for resetting alarms whenever phone has been shut down.
public class AlarmSetter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// get preferences
SharedPreferences preferences = context.getSharedPreferences("name_of_your_pref", 0);
Map<String, ?> scheduleData = preferences.getAll();
// set the schedule time
if(scheduleData.containsKey("fromHour") && scheduleData.containsKey("toHour")) {
int fromHour = (Integer) scheduleData.get("fromHour");
int fromMinute = (Integer) scheduleData.get("fromMinute");
int toHour = (Integer) scheduleData.get("toHour");
int toMinute = (Integer) scheduleData.get("toMinute");
//Do some action
}
}
}
Manifest very important, this is added under application:
<receiver android:name="AlarmReceiver">
<intent-filter>
<action android:name="packagename.ACTION"/>
<action android:name="packagename.ACTION2"/>
</intent-filter>
</receiver>
<receiver android:name="AlarmSetter" >
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Also in order for this to work you need to add permission to receive the boot Broadcast in the manifest with following line:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Hope this cleared things up, if any errors plz tell.
Edit (added alarmsetter example):
public class AlarmSetter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Do your stuff
}
}
How to start service at the particular time?
Use android AlarmManager to schedule alarms.
you can register multiple alarms for your application.
hope this link helps you
Spring Scheduling - Cron expression for everyday at midnight not working?
I finally got it to work with this cron expression 0 0 0 * * *
but I had to set the time zone in the scheduler class like this.@Scheduled(cron = "0 0 0 * * *",zone = "Indian/Maldives")
Is it possible to run an application automatically on boot up as background service in J2me?
Yes, Srizan,
It is possible using the Application Settings. You need to set the Auto Start setting to true in Application Settings.
Related Topics
Android Support Library Setup with Maven
How to Order My SQLite Database in Descending Order, for an Android App
Adding Your Own SQLite Database to an Android Application
Tasksnapshot.Getdownloadurl() Is Deprecated
How to Change Color of Android Listview Separator Line
Handling Back Button in Android Navigation Component
Where/How to Getintent().Getextras() in an Android Fragment
Getting "Debuggable" Value of Androidmanifest from Code
Oauth 2.0 Authorization for Linkedin in Android
Why Getspeed() Always Return 0 on Android
How to Share Image + Text Together Using Action_Send in Android
How to Add Android Support V7 Libraries in Eclipse
Scan Android Sd Card for New Files
Why "This App Has Been Built with an Incorrect Configuration" Error Occured in Some Phones
What Is the Relationship Between Looper, Handler and Messagequeue in Android