Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8484781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:28:06+00:00 2026-06-10T20:28:06+00:00

I’m using the Lunch List example from CommonsWare to set and reset alarms. I

  • 0

I’m using the Lunch List example from CommonsWare to set and reset alarms.

I set alarm at specific times and then if it’s a recurring alarm, I try to reset the alarm to a new date/time.

In OnAlarmReceiver I try to use the original three lines of code when first setting an alarm in my application’s context (the activity). The three lines are:

ComponentName component=new ComponentName(context, OnBootReceiver.class);
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);   
OnBootReceiver.setAlarm(context, itemId, mDateDue);

However this doesn’t seem to work. What I tried then was adding this line:

OnBootReceiver.cancelAlarm(context, itemId);

But that also made no difference. I don’t have a proper understanding of how this all ties together, but I suspect either:

  1. My context is wrong.
  2. I have to do something with the broadcast, like cancelling it as well.
  3. Perhaps there is a flag that needs to be changed?

The idea is that every time a recurring alarm happens, it’s reset by code. I know I can use repeating alarm but at this stage of my application I prefer to do this manually.

Here is OnAlarmReceiver:

public class OnAlarmReceiver extends BroadcastReceiver {
private static final int NOTIFY_ME_ID=1337;

private static final String TAG = "OnAlarmReceiver";

private DbAdapter mDbHelper;

private String mDateDue;
private String mFrequency;  

@Override
public void onReceive(Context context, Intent intent) {     
    mDbHelper = new DbAdapter(context);
    mDbHelper.open();

    AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

    Bundle bundle = intent.getExtras();
    long itemId = bundle.getLong("itemId");

    Cursor c  = mDbHelper.getItem(itemId);      
    String itemTitle = c.getString(c.getColumnIndex(Db.KEY_ITEMS_TITLE));
    int priority = c.getInt(c.getColumnIndex(Db.KEY_ITEMS_PRIORITY));
    long listId = c.getLong(c.getColumnIndex(Db.KEY_ITEMS_LIST_ID));
    String listTitle = mDbHelper.getListTitle(listId);

    mDateDue = c.getString(c.getColumnIndex(Db.KEY_ITEMS_DATE_DUE));
    mFrequency = c.getString(c.getColumnIndex(Db.KEY_ITEMS_FREQUENCY));

    Toast.makeText(context, "Due: " + listTitle + "->" + itemTitle, Toast.LENGTH_LONG).show();

    SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);
    boolean useNotification=prefs.getBoolean("use_notification", true);

    // Check if the alarm must be reset to a new future date based on frequency
    checkResetAlarm(context, itemId);

    if (useNotification) {
        NotificationManager mgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);                                  
        Notification notification = new Notification();         
        if (priority == 1) { // Display red icon
            notification=new Notification(R.drawable.nuvola_apps_kwrite, itemTitle, System.currentTimeMillis());    
        } else { // Display blue icon
            notification=new Notification(R.drawable.nuvola_apps_package_editors, itemTitle, System.currentTimeMillis());               
        }           
        Intent itemEditor = new Intent(context, ActivityEditItem.class);
        long lAlarmId = (long) (int) itemId;
        itemEditor.putExtra(DbAdapter.KEY_ITEMS_ITEM_ID, lAlarmId);
        itemEditor.putExtra("listId", listId);
        itemEditor.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // For flags, also see http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

        PendingIntent i=PendingIntent.getActivity(context, 0, itemEditor, PendingIntent.FLAG_UPDATE_CURRENT);           
        notification.setLatestEventInfo(context, listTitle, itemTitle, i);          
        String notifyPreference = prefs.getString("notification_sound", "DEFAULT_RINGTONE_URI");                        
        notification.sound = Uri.parse(notifyPreference);

        int oldVolume = audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);

        if (priority == 1) {
            Log.d(TAG, "A high priority item is due");
            //notification.defaults |= Notification.DEFAULT_VIBRATE;
            Vibrator v;
            v=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(3000);
            int streamVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
            audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, streamVolume, 0);
        }
        mgr.notify((int) (long) itemId + NOTIFY_ME_ID, notification);
        audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, oldVolume, 0);
    }
    else {
        Intent i=new Intent(context, AlarmActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

private void checkResetAlarm(Context context, long itemId) {        
    if (!mFrequency.equals("")) {           
        String newDateDue = Item.addMinutesToDate(mDateDue, mFrequency); 
        Log.d(TAG, "Due date " + mDateDue + " reset with frequency of " + mFrequency + ", new due date: " + newDateDue);
        mDbHelper.updateItemDueDate(itemId, newDateDue);
        Toast.makeText(context, "Resetting alarm...", Toast.LENGTH_LONG).show();
        ComponentName component=new ComponentName(context, OnBootReceiver.class);
        context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);   
        OnBootReceiver.cancelAlarm(context, itemId);
        OnBootReceiver.setAlarm(context, itemId, mDateDue);
    }       
}

}

Here is OnBootReceiver:

public class OnBootReceiver extends BroadcastReceiver {

public static void setAlarm(Context context, long itemId, String dateDue) {

    AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Calendar cal=Calendar.getInstance();                

    String[] pieces=dateDue.split("/");
    String day_of_month = dateDue.substring(8,10);      
    String hour = dateDue.substring(11,13);     
    String minute = dateDue.substring(14,16);
    String second = dateDue.substring(17,19);

    cal.set(Calendar.YEAR, Integer.parseInt(pieces[0]));                
    cal.set(Calendar.MONTH, Integer.parseInt(pieces[1])-1);     
    cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day_of_month));
    cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
    cal.set(Calendar.MINUTE, Integer.parseInt(minute));
    cal.set(Calendar.SECOND, Integer.parseInt(second));     
    cal.set(Calendar.MILLISECOND, 0);

    if (cal.getTimeInMillis()<System.currentTimeMillis()) {
        cal.add(Calendar.DAY_OF_YEAR, 1);
    }

    mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            getPendingIntent(context, itemId));

}

/**
 * Cancel Alarm
 *  
 * @param ctxt
 * @param itemId
 */
public static void cancelAlarm(Context ctxt, long itemId) {
    AlarmManager mgr=(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
    mgr.cancel(getPendingIntent(ctxt, itemId));
}

private static PendingIntent getPendingIntent(Context ctxt, long itemId) {
    //Intent i = new Intent(OnAlarmReceiver.ACTION, Uri.parse("timer:"+alarmId));
    Intent i=new Intent(ctxt, OnAlarmReceiver.class);
    i.putExtra("itemId", itemId);
    return(PendingIntent.getBroadcast(ctxt, (int) (long) itemId, i, 0));
}

// When the phone restarts all alarms must be reset by this method
@Override
public void onReceive(Context ctxt, Intent intent) {
    // To be added
    //setAlarm(ctxt);
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:28:08+00:00Added an answer on June 10, 2026 at 8:28 pm

    Stupid coding error, in OnAlarmReceiver I had OnBootReceiver.setAlarm(context, itemId, mDateDue); instead of OnBootReceiver.setAlarm(context, itemId, newDateDue);.

    This post also helped me: Android AlarmManager in a Broadcastreceiver

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.