Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Android alarmmanager is your answer. use it with a broadcast receiver which also resets the alarms on phone wake.</p> <p>Now with code example: Setting alarm inside a method:</p> <pre><code>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); </code></pre> <p>Receiver for your interval:</p> <pre><code>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. } </code></pre> <p>Receiver for resetting alarms whenever phone has been shut down.</p> <pre><code>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&lt;String, ?&gt; scheduleData = preferences.getAll(); // set the schedule time if(scheduleData.containsKey("fromHour") &amp;&amp; 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 } } } </code></pre> <p>Manifest very important, this is added under application:</p> <pre><code> &lt;receiver android:name="AlarmReceiver"&gt; &lt;intent-filter&gt; &lt;action android:name="packagename.ACTION"/&gt; &lt;action android:name="packagename.ACTION2"/&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;receiver android:name="AlarmSetter" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; </code></pre> <p>Also in order for this to work you need to add permission to receive the boot Broadcast in the manifest with following line:</p> <pre><code>&lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt; </code></pre> <p>Hope this cleared things up, if any errors plz tell.</p> <p><strong>Edit</strong> (added alarmsetter example):</p> <pre><code>public class AlarmSetter extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Do your stuff } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload