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 8275707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:09:45+00:00 2026-06-08T08:09:45+00:00

I’m trying to trigger 3 alarms at different times using alarm manager. Here’s my

  • 0

I’m trying to trigger 3 alarms at different times using alarm manager. Here’s my code (Note that alarm1, alarm2, alarm3 are three calender objects set earlier in my code):

AlarmNum=1;
new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            // TODO Auto-generated method stub
            Intent myIntent = new Intent(MainActivity.this,
                    MyAlarmService.class);
            pendingIntent = PendingIntent.getService(MainActivity.this, 0,
                    myIntent, 0);

            if (AlarmNum == 1)
                alarmManager.set(AlarmManager.RTC_WAKEUP,
                        alarm1.getTimeInMillis(), pendingIntent);
            else if (AlarmNum == 2)
                alarmManager.set(AlarmManager.RTC_WAKEUP,
                        alarm2.getTimeInMillis(), pendingIntent);
            else
                alarmManager.set(AlarmManager.RTC_WAKEUP,
                        alarm3.getTimeInMillis(), pendingIntent);

            Toast.makeText(MainActivity.this, "Start Alarm",
                    Toast.LENGTH_LONG).show();
        }
    };

In the above code I start an intent which provokes MyAlarmService class given below:

public class MyAlarmService extends Service {
MainActivity instance;
MediaPlayer mp;

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG)
            .show();
    mp = MediaPlayer.create(this, R.raw.alarmtone);
    instance = new MainActivity();
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG)
            .show();
    return null;
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG)
            .show();
}

@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG)
            .show();
    mp.start();
    instance.setAlarmNum(instance.getAlarmNum() + 1);
}

@Override
public boolean onUnbind(Intent intent) {
    // TODO Auto-generated method stub
    mp.release();
    mp.reset();
    Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG)
            .show();
    return super.onUnbind(intent);
}

}
I assume there is a problem here because the toasts never show up and neither does the alarm.

  • 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-08T08:09:46+00:00Added an answer on June 8, 2026 at 8:09 am

    What I understood from your code and comments is that you want first alarm to trigger the second and second alarm trigger the third, etc.

    Several problems I notice with your code.

    1) This is a wrong way to start an activity, it won’t work:

    instance = new MainActivity();
    

    Instead, you should do this:

    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
    

    Regardless of above issue, you do not have to start your MainActivity everytime your alarm is triggered. You can just trigger the new alarm inside your service. Here, I am assuming that 2nd alarm trigger time is later than the first, and third later than the second, etc. Otherwise, your algorithm won’t work.

    First alarm will be set in your activity:

    Intent myIntent = new Intent(MainActivity.this, MyAlarmService.class);
    myIntent.addExtra("AlarmNum",1);
    PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0,
                        myIntent, 0);
    
    alarmManager.set(AlarmManager.RTC_WAKEUP,alarm1.getTimeInMillis(), pendingIntent);
    

    Note that I added an extra integer to your intent so that your service will be able to understand which alarm was set previously.

    Back in your service onBind function, you should read this extra from your Intent.

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
    ...
    int AlarmNum = intent.getIntExtra("AlarmNum",0);
    ...
        return null;
    }
    

    Then, check the value of AlarmNum and set the next alarm the same way we did, depending on the value (if AlarmNum == 1, set the second alarm, incerementing your intent’s extra by 1, etc.). Since you have 3 alarms to set, if it reads 3, then you will do nothing but complete your service.

    As a side notice, it is a better practice to show toast messages in your Service using a Handler.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.