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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:30:19+00:00 2026-05-27T10:30:19+00:00

I have the following code to trigger an alarm, which when it fires is

  • 0

I have the following code to trigger an alarm, which when it fires is meant to launch another activity that is basically a nap alarm. After much searching through Google and SO, I am at a loss. I believe I have the correct setup for the AlarmManager, but the Activity that it is supposed to launch never gets launched.

By the way, eventually I want to use the napTime variable to set the time at which the AlarmManager will fire, but for testing purposes I am just trying to get it to fire right way.

Here is the code to set the nap. The Log.e("Nap time", "" + napTime); line always shows the correct number that was selected for the napTime, so I know that the method is executing.

private void setNap(int napTime) {
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);       
        Intent i = new Intent(getApplicationContext(), NapAlarm.class);
//      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);     
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);

        Log.e("Nap time", "" + napTime);

        TextView nap = (TextView) findViewById(R.id.nap);
        nap.setText("nap set");
    }

And the code for the activity that is launched:

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;

public class NapAlarm extends Activity implements OnClickListener {

    MediaPlayer mp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alarm_layout);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

        Button dButton = (Button) findViewById(R.id.dismissButton);
        dButton.setOnClickListener(this);

        Button sButton = (Button) findViewById(R.id.snoozeButton);
        sButton.setOnClickListener(this);

        new Thread(new Runnable() {

            public void run() {
                try {

                    Log.e("Got this far", "In the alarm");

                    mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
                    mp.start();
                    mp.setLooping(true);

                } catch (Exception e) {
                }
            }
        }).start();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.dismissButton:
            mp.stop();
            this.finish();
            break;

        case R.id.snoozeButton:
            Button sButton = (Button) findViewById(R.id.snoozeButton);
            sButton.setText("I'm sorry, this app currently does not support snoozing, you must wake up.");
            break;
        }
    }
}

If I run the second piece of code by itself, it works and the sound plays, etc. But if I run it from within the larger project from which the AlarmMananger is supposed to create an alarm to trigger it, it never happens.

Is there something that I am overlooking?

  • 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-05-27T10:30:19+00:00Added an answer on May 27, 2026 at 10:30 am

    Your NapTime activity should be extending a BroadCast Receiver like this..

    public class NapAlarm extends BroadcastReceiver {
    
    @Override
     public void onReceive(Context context, Intent intent) {
    //Here do your naptime code.
    
    //YOu could actually have your NapTime class launch from this reciever if you want.
    }
    
    }
    

    Make sure you register your reciever like this..

    <receiver  android:process=":remote" android:name="NapAlarm"></receiver>
    

    Refer here for more info

    Alarm Manager tutorial

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

Sidebar

Related Questions

I have the following code on my website, which basically checks to see if
I have the following code which toggles the visibility of a div when another
I have following Code Block Which I tried to optimize in the Optimized section
I have following code snippet that i use to compile class at the run
I have the following code: jQuery('#hexVal').change(function() { var hexvarval = jQuery('#hexVal').val(); jQuery('#thumbnail_hover_color').val(hexvarval); }); Basically,
Lets say I have the following code that returns number of anchor elements on
I have the following Javascript code which controls an accordion type set of divs.
I have the following code: <TabItem Name=tabItemGeneral> <TabItem.Triggers> <Trigger Property=IsSelected Value=True> <Setter TargetName=tabGeneralImg Property=Source
I have the following code, the only problem is that when the code gets
I have the following code which supposed to display a TreeView (the original code

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.