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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:53:51+00:00 2026-06-18T14:53:51+00:00

I am creating alarm alert for appointment reminder for that I am using following

  • 0

I am creating alarm alert for appointment reminder for that I am using following code.Code is working very well it showing me alarm alert but only problem is that, it is not differentiate alarm between am and pm, suppose if I set alarm for 7am and currently 7pm in device then also my alert dialog shows. How can I manage that am and pm? I used this link for ref

http://wptrafficanalyzer.in/blog/setting-up-alarm-using-alarmmanager-and-waking-up-screen-and-unlocking-keypad-on-alarm-goes-off-in-android/

AlertDemo.class

 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.content.DialogInterface;
 import android.content.DialogInterface.OnClickListener;
 import android.os.Bundle;
 import android.support.v4.app.DialogFragment;
 import android.view.WindowManager.LayoutParams;


public class AlertDemo extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    /** Turn Screen On and Unlock the keypad when this alert dialog is     displayed */
    getActivity().getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON |   LayoutParams.FLAG_DISMISS_KEYGUARD);


    /** Creating a alert dialog builder */
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    /** Setting title for the alert dialog */
    builder.setTitle("Alarm");

    /** Setting the content for the alert dialog */
    builder.setMessage("An Alarm by AlarmManager");

    /** Defining an OK button event listener */
    builder.setPositiveButton("OK", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            /** Exit application on click OK */
            getActivity().finish();
        }                       
    });

    /** Creating the alert dialog window */
    return builder.create();
 }

 /** The application should be exit, if the user presses the back button */ 
 @Override
 public void onDestroy() {      
    super.onDestroy();
    getActivity().finish();
 }

}

Appointment.class

  import java.text.DateFormatSymbols;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  import java.util.GregorianCalendar;

  import android.app.Activity;
  import android.app.AlarmManager;
  import android.app.DatePickerDialog;
  import android.app.Dialog;
  import android.app.Notification;
  import android.app.NotificationManager;
  import android.app.PendingIntent;
  import android.app.TimePickerDialog;
  import android.app.TimePickerDialog.OnTimeSetListener;
  import android.content.Intent;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.DatePicker;
  import android.widget.TimePicker;
  import android.widget.Toast;

 public class Appointment extends Activity {

Button date, time, save;

private static final int DIALOG_DATE = 1;
private static final int DIALOG_TIME = 2;
private int year;
private int month;
private int day;
int i;
String strmonth, strday, stryear;

String months[] = { "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December" };

int intmonth, intday, intyear, inthour, intminutes;
Calendar c = Calendar.getInstance();
private SimpleDateFormat timeFormatter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.doctor_appointment);

    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);

    date = (Button) findViewById(R.id.btnsetdate);
    time = (Button) findViewById(R.id.btnsettime);
    save = (Button) findViewById(R.id.btnsave);
    timeFormatter = new SimpleDateFormat("hh:mm a");
    // c.set(Calendar.MONTH, 4);

    date.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDialog(DIALOG_DATE);
        }
    });
    time.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDialog(DIALOG_TIME);
        }
    });

    save.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent("com.example.healthmanager.DemoActivity");
            /** Creating a Pending Intent */
            PendingIntent operation = PendingIntent.getActivity(
                    getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);

            /** Getting a reference to the System Service ALARM_SERVICE */
            AlarmManager alarmManager = (AlarmManager) getBaseContext()
                    .getSystemService(ALARM_SERVICE);

            String strtime = time.getText().toString();

            Log.v("str btntime", strtime);

            String[] splitstrtime = strtime.split(":");

            Log.v("timestr1", splitstrtime[0]);
            Log.v("timestr2", splitstrtime[1]);

            int splithour = Integer.parseInt(splitstrtime[0]);
            String[] splitsecond = splitstrtime[1].split(" ");

            Log.v("split str second", splitsecond[0]);
            int splitmin = Integer.parseInt(splitsecond[0]);

            /**
             * Creating a calendar object corresponding to the date and time
             * set by the user
             */
            // GregorianCalendar calendar = new
            // GregorianCalendar(year,month,day, hour, minute);
            GregorianCalendar calendar = new GregorianCalendar(intyear,
                    intmonth, intday, splithour, splitmin);

            /**
             * Converting the date and time in to milliseconds elapsed since
             * epoch
             */
            long alarm_time = calendar.getTimeInMillis();

            /** Setting an alarm, which invokes the operation at alart_time */
            alarmManager
                    .set(AlarmManager.RTC_WAKEUP, alarm_time, operation);

            /** Alert is set successfully */
            Toast.makeText(getBaseContext(), "Alarm is set successfully",
                    Toast.LENGTH_SHORT).show();

        }
    });

}

// For date dialog
@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_DATE:
        return new DatePickerDialog(this, datePickerListener, year, month,
                day);
    case DIALOG_TIME:
        return new TimePickerDialog(this, new OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                c.set(Calendar.HOUR_OF_DAY, hourOfDay);
                c.set(Calendar.MINUTE, minute);
                time.setText(timeFormatter.format(c.getTime()));

            }
        }, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), false);
    }

    return null;
}

// For date
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year1, int monthOfYear,
            int dayOfMonth) {
        year = year1;
        month = monthOfYear;
        day = dayOfMonth;
        // date.setText(dateFormatter.format(dateTime.getTime()));
        updateDisplay();
    }
};

public String getMonthForInt(int m) {
    String month = "invalid";
    DateFormatSymbols dfs = new DateFormatSymbols();
    String[] months = dfs.getMonths();
    if (m >= 0 && m <= 11) {
        month = months[m];
    }
    return month;
}

private void updateDisplay() {
    // String strDOB = month + 1 + "/" + day + "/" + year;
    // Log.v("strDOB : ", strDOB);

    intmonth = month;
    intday = day;
    intyear = year;

    strmonth = Integer.toString(intmonth);
    strday = Integer.toString(intday);
    stryear = Integer.toString(intyear);
    Log.v("month value", strmonth);
    Log.v("day value", strday);
    Log.v("year value", stryear);
    // int one=7;
    // Log.v("string limit",one.length());
    for (i = 0; i < intmonth; i++) {
        String strone = Integer.toString(intmonth);
        strone = months[i];
        // String intmonth=Integer.toString(months);
    }
    Log.v("month value", months[i].toString());
    date.setText(months[i] + "  " + day + "," + year);
}

 }

DemoActivity.class

public class DemoActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /** Creating an Alert Dialog Window */
    AlertDemo alert = new AlertDemo();

    /** Opening the Alert Dialog Window */
    alert.show(getSupportFragmentManager(), "AlertDemo");
}
}
  • 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-18T14:53:52+00:00Added an answer on June 18, 2026 at 2:53 pm

    You are not handling the AM/PM. Just put these lines of code…

    int timeDifference=0; 
    String ampm=splitampmtime[1]; 
    if(ampm.matches("PM")){ 
    timeDifference=12; 
    } 
    
    int splithour = timeDifference+Integer.parseInt(splitstrtime[0]); 
    String[] splitsecond = splitstrtime[1].split(" ");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a repeating alarm. This works fine using the following code: Intent
I am creating reminder type demo app. that include at every morning 7 a.m.
I'm very stuck creating an alarm from a DatePicker and TimePicker... This is the
Hi I'm creating a app to display the count of alarm that comes from
I am creating a small alarm that will have to set the speaker volume
Creating a website using simple html(not html5) and css but got stock in mouseover
I'm creating a simple alarm clock app. The problem is that i get the
I have a custom validation method that ensures an alarm can only be set
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
Creating a simple RPG game, first time using XNA. Trying to get my character

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.