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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:39:54+00:00 2026-05-22T21:39:54+00:00

In my alarm application, I take time from time picker and set it in

  • 0

In my alarm application, I take time from time picker and set it in alarm manager. But the time which I set in the time picker was not set properly in alarm manager. So the alarm is not activated at the specified time. I have included my code below. Could anybody help me out?

layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
    android:id="@+id/btn"
    android:text="Alerm"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
    private Button btn = null;
    private AlarmManager alarmManager = null;
    Calendar cal = Calendar.getInstance();
    static final int DIALOG_TIME = 0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {         
            @Override
            public void onClick(View v) {
                showDialog(DIALOG_TIME);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;
        switch(id){
        case DIALOG_TIME:
            dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    Calendar c = Calendar.getInstance();
                    c.setTimeInMillis(System.currentTimeMillis());
                    c.clear();
                    //c.set(Calendar.YEAR, 2011);
                    //c.set(Calendar.MONTH, 6);
                    //c.set(Calendar.DAY_OF_MONTH, 4);
                    c.set(Calendar.HOUR, hourOfDay);
                    c.set(Calendar.MINUTE, minute);
                    c.set(Calendar.SECOND, 0);
                    //c.set(Calendar.MILLISECOND, 0);
                    Intent intent = new Intent(MainActivity.this,AlarmReceiver.class);
                    PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
                    alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
                    //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
                    Toast.makeText(MainActivity.this, "Alarm has been set..", Toast.LENGTH_LONG).show();
                }
            },cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),false);
            break;
        }
        return dialog;
    }
}

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, AlarmActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

AlarmActivity.java

public class AlarmActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new AlertDialog.Builder(AlarmActivity.this).setTitle("Task").setMessage("Time to wake up").setPositiveButton("ok", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                AlarmActivity.this.finish();
            }
        }).create().show();
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="android.phone"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AlarmActivity"></activity>
        <receiver android:name=".AlarmReceiver" android:process=":remote"></receiver>
    </application>
</manifest>

  • 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-22T21:39:55+00:00Added an answer on May 22, 2026 at 9:39 pm

    I was able to replicate your problem and find why it was failing. Basically, say you were testing at “12:25” like i was, then using Calendar.HOUR of 12 and doing c.get(Calendar.HOUR_OF_DAY) was returning 0.

    To change, simply use:

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);
    

    i.e. remove the clear, and set HOUR_OF_DAY instead of HOUR

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

Sidebar

Related Questions

I am having a reminder application in which i have an alarm manager like
I write application which will use alarm manager. First I set up alarm manager
I am working on Alarm application in which I want to set Large Sound
I have a Homework Planner application which I want to create an Alarm for
I am testing simple alarm application. In firstactivity I set alarm with ringtone which
I have an application in which i have made a alarm clock. In my
I'm not really writing an alarm clock application, but it will help to illustrate
I have an idea for a unique alarm application on the iPhone. But at
I have created one alarm clock application. In which, when user clicks on the
I have an application, which sets an alarm using AlarmManager, which starts another Activity

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.