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

  • SEARCH
  • Home
  • 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 8857217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:32:16+00:00 2026-06-14T14:32:16+00:00

I’ve got an application that needs to show a notification of certain events in

  • 0

I’ve got an application that needs to show a notification of certain events in a day, depending on the event.

I know how to do it, if I need a service to stay running in the background, or it will run in the background getting these events on certain days.

private void Notificar(String descricaoEvento){
    NotificationManager notifier = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, "Novo evento no The Point Club", System.currentTimeMillis()); 
    notification.flags |= Notification.DEFAULT_SOUND;

    PendingIntent pIntent = PendingIntent.getActivity(this, 0, null, 0); 
    notification.setLatestEventInfo(this, "Title", descricaoEvento.toString(), pIntent);        
    notifier.notify(0x007, notification);       
}

using this method to call when the right date!


Thanks for listening, I’ll try to explain what I’m doing right: I’m making an application for a club party, and the application will show the next event of the celebrations when I open the application, but what I wish is that a notification is created in android notification bar, on which there is a party, serving as a reminder.

I use this code when I open the activity to list events:

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());
    int anoAtual= c.get(Calendar.YEAR);
    long time = c.getTimeInMillis();
    Intent it = new Intent("EXECUTAR_ALARME") 
    p = PendingIntent.getBroadcast(Eventos.this, 0, it, 0);
    AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarme.set(AlarmManager.RTC_WAKEUP, time, p);

the line

    Intent it = new Intent("EXECUTAR_ALARME") 

will call the class:

    public class Alarm extends BroadcastReceiver{   @Override
       public void onReceive(Context contexto, Intent intent) {
           NotificationManager notifier =(NotificationManager) contexto.getSystemService(Context.NOTIFICATION_SERVICE);
           Notification notification = new Notification(R.drawable.ic_launcher, "New event in Club", System.currentTimeMillis()); 
           notification.flags |= Notification.DEFAULT_SOUND;
           //Intent i = new Intent(this, Eventos.class);
           PendingIntent pIntent = PendingIntent.getActivity(contexto, 0, null, 0); 
           notification.setLatestEventInfo(contexto, "Club", "Teste", pIntent);     
           notifier.notify(0x007, notification);  


       }

}

Now I want to know two things:
1 – How do I leave it running in the background without being open with the application.
2 This code only creates only an alarm, and I need to create multiple.

Thanks for the help.

  • 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-14T14:32:18+00:00Added an answer on June 14, 2026 at 2:32 pm

    Use AlarmManager to run a PendingIntent at your specific time. At that time, the intent that it stored in the PendingIntent should likely be a service if you’re planning on creating a Notification to display to the user. If you could form this as a question, I may be able to help more, but it’s unclear what you’re trying to accomplish, and what you’ve managed to accomplish.


    Additional Details

    In your code, it looks like you’re scheduling the alarm for right now, instead of at some time in the future. I’d suggest the following to create the alarm:

        Calendar c = Calendar.getInstance();
        // don't do this:
        // c.setTimeInMillis(System.currentTimeMillis()); // This just sets the alarm time to "now"
    
        // set the calendar to 8:00 pm:
        c.set(Calendar.HOUR_OF_DAY, 20);
        c.set(Calendar.MINUTE, 0);
    
        long alarmTime = c.getTimeInMillis();
        Intent it = new Intent(ServiceClass.getClass());
        // Even though the docs say that the request code doesn't matter making it unique tells the alarmManager that it's a different PendingIntent
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, it, 0);
    
        AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmMgr.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent); // set the first alarm for 8:00
    
        c.set(Calendar.MINUTE, 30);
        Intent it2 = new Intnt(ServiceClass.getClass());
        PendingIntent pi2 = PendingIntent.getService(this, 0, it2, 0);
        alarmMgr.set(AlarmManager.RTC_WAKEUP, alarmTime, pi2); // set the second alarm for 8:30
    

    the line

    Intent it = new Intent(ServiceClass.getClass());
    

    will start the Class ServiceClass:

    class ServiceClass extends Service {
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO TRAVIS Auto-generated method stub
            return null;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) { // onStartCommand is run when the service is started by the AlarmManager
            // build and set the notification here
            NotificationManager notifMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            // build notification
            Notification notification = new Notification.Builder(getApplicationContext())
                    .setContentTitle("New mail from " + sender.toString())
                    .setContentText(subject)
                    .setSmallIcon(R.drawable.new_mail)
                    .setLargeIcon(aBitmap)
                    .build();
    
            int ID = 0;
    
            notifMgr.notify(ID++, notification);   // use a unique id every time if you want multiple notifications (sounds like what you want, but not a best practice)
    
            notification = new Notification.Builder(getApplicationContext())
                    .setContentTitle("New event from " + sender.toString())
                    .setContentText(subject)
                    .setSmallIcon(R.drawable.new_mail)
                    .setLargeIcon(aBitmap)
                    .build();
    
            notifMgr.notify(ID++, notification);  // add the second notification to the notification bar            
            return super.onStartCommand(intent, flags, startId);
        }
    }
    

    Answers to your questions:

    1 – By setting the alarm for sometime in the future, you’re asking the OS to start your PendingIntent (in your case, send a Broadcast; in my exmaple above, a Service) at the time specified (that’s why you need to set the time with the calendar to sometime in the future!)

    2 – Creating multiple alarms is a matter of calling the alarm manager with different PendingIntent s scheduled at the appropriate time (for when you want them to fire). The same is true for Notification s, create multiple ones (with unique ids).

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I

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.