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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:36:13+00:00 2026-06-19T00:36:13+00:00

I had created a program that will create alarm for different date set manually

  • 0

I had created a program that will create alarm for different date set manually from the date picker.
The code is working properly.but if reboot it losing the data and alarm is not working how can i overcome that

The code I used is

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

    OnClickListener setClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            /** This intent invokes the activity DemoActivity, which in turn opens the AlertDialog window */
            Intent i = new Intent("in.com.example.demoactivity");

            /** Creating a Pending Intent */
            PendingIntent operation = PendingIntent.getActivity(getBaseContext(), count++, i, Intent.FLAG_ACTIVITY_NEW_TASK);

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

            /** Getting a reference to DatePicker object available in the MainActivity */
            DatePicker dpDate = (DatePicker) findViewById(R.id.dp_date);

            /** Getting a reference to TimePicker object available in the MainActivity */
            TimePicker tpTime = (TimePicker) findViewById(R.id.tp_time);

            int year = dpDate.getYear();
            int month = dpDate.getMonth();
            int day = dpDate.getDayOfMonth();
            int hour = tpTime.getCurrentHour();
            int minute = tpTime.getCurrentMinute();


            GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);

            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();

        }
    };      

    OnClickListener quitClickListener = new OnClickListener() {         
        @Override
        public void onClick(View v) {
            finish();
        }
    };

    Button btnSetAlarm = ( Button ) findViewById(R.id.btn_set_alarm);
    btnSetAlarm.setOnClickListener(setClickListener);

    Button btnQuitAlarm = ( Button ) findViewById(R.id.btn_quit_alarm);
    btnQuitAlarm.setOnClickListener(quitClickListener);

}

From that to an activity fragment

    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");       
}

}

from here to an activity which create an alertbox

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();
}

I want thealarm to be invoken even if I reboot the device,Somebody please help me to sort it out

  • 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-19T00:36:14+00:00Added an answer on June 19, 2026 at 12:36 am

    You have to Use BroadcastReceiver in which you have to check Intent.ACTION_BOOT_COMPLETED and reset your alarm actions within Receiver. For example:

    public class MyBootReceiver extends BroadcastReceiver{
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
                //reset your alarm here
            }
        }
    
    }
    

    Updated:

    Use share-preference to store you data, or you can use database too. I did same using share-preference, check below code:

    public class MyBootReceiver extends BroadcastReceiver{
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
                SharedPreferences mPreferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
                String datetime = mPreferences.getString("date", null);
                if(!TextUtils.isEmpty(datetime)) {
                    Utility.setNotification(context);//set your alarm here.
    
                }
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a program that will open a text file with
I created a program that will use a dynamic array by using pointer to
I'm trying to create a program that will display bar graphs with * the
I had created a zip file (together with directory) under Windows as follow (Code
Question : Beginner Level Code: Pure Javascript I had created a web page in
I have a smallish utility library I made that I had created in TFS
I am creating a program that will run an experiment on a user. It
I'm a java beginner and I tried making a basic program that will delete
I have created a program in Java using eclipse that contains a couple of
In my program I'm trying to set up column headers for a file that

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.