How to set one time alarm in android
Asked Answered
S

3

5

I am doing alarm Project,

I want to set one time alarm.. But i am facing problem to set tat,

My code is;

public void loadCalender(String month) {

    try {

        Cursor cursor = null;

        Database db = new Database(getApplicationContext());

        cursor = db.getSelectedCalenderDetails(month);
        if (cursor.getCount() != 0) {
            if (cursor.moveToFirst()) {
                do {
                    String text = cursor.getString(cursor
                            .getColumnIndex("event"));
                    String title = "News/Events";
                    String dates = cursor.getString(cursor
                            .getColumnIndex("date"));

                    String yr = dates.substring(0, 4);
                    int year = Integer.parseInt(yr);
                    String mon = dates.substring(5);
                    String mo = mon.substring(0, 2);
                    int months = Integer.parseInt(mo);
                    String da = dates.substring(9);
                    int day = Integer.parseInt(da);

                    // Ask our service to set an alarm for that date,
                    // this
                    // activity talks to the client that talks to the
                    // service
                    set_alarm(year, months, day, title, text);
                    System.out.println(dates);

                } while (cursor.moveToNext());

            }

        }
        cursor.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // looping through All Contacts

}


public void set_alarm(int year, int month, int day, String title,
        String text) {
    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());

    // etc
    Calendar cal = Calendar.getInstance();
    int hour = prefs.getInt("PREF_HOUR", 0);
    int min = prefs.getInt("PREF_MIN", 0);
    if(hour == 0 && min == 0){


    cal.set(Calendar.MONTH, month - 1);
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.DAY_OF_MONTH, 7);

    cal.set(Calendar.HOUR_OF_DAY, 15);
    cal.set(Calendar.MINUTE, min);
    }else
    {

        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.DAY_OF_MONTH, hour);

        cal.set(Calendar.HOUR_OF_DAY, min);
        cal.set(Calendar.MINUTE, min);
    }
    Intent intent = new Intent(getApplicationContext(), AlarmActivity.class);
    intent.putExtra("title", title);
    intent.putExtra("text", text);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            getApplicationContext(), 1, intent,
            PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getApplicationContext()
            .getSystemService(getApplicationContext().ALARM_SERVICE);

    /*
     * alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
     * pendingIntent);
     */

    alarmManager.cancel(pendingIntent); // cancel any existing alarms

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY,

            pendingIntent);
}

Here the alarm push is enabled always, when the app is opened...

Wat mistake did i do in my code.. plz help me to set the one time alarm

Thanks in advance

Scuff answered 28/3, 2013 at 8:57 Comment(0)
S
10

You need to use setAlarm() method instead of setRepeating()

alarmManager.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent);
Senhauser answered 28/3, 2013 at 9:0 Comment(10)
Do I also need to handle boot of the device? Or will it still work even after the device was restarted ? What if it should have been called, yet the device was turned off (no battery for example), and now the device was turned on ?Concubinage
@androiddeveloper you dont have to worry about device boot. RTC method sets an alarm to a specific time. So if phone is not running in that time the alarm will be fired next time the phone boots.Irradiance
@Senhor I see. What about the case of time change (example " daylight saving") ? If I set it to alarm on X, yet before X, the country changes X to be X+1, before X arives. This means that an entire hour was skipped. What will it do to the alarm?Concubinage
@androiddeveloper I believe the alarm will fire at the same time. RTC is not based on the pased time. If you want to handle this you should use ELAPSED_TIME instead. But I am not sure, I have never used RTC in a situation you mentioned.Irradiance
@Senhor I wonder what happens on the Alarm clock app. It probably uses this API, so what would happen if I set it to wake me on this problematic hour...Concubinage
@androiddeveloper I checked the alarm app when you asked your question. Alarm app doesn't change the set time for alarm when timezone changed.Irradiance
@androiddeveloper I have tried it on AlarmManager, I have set an alarm to 3 hours later with RTC method and changed device to 4 hours later. And alarm got fired.Irradiance
@Senhor So even though the time of alarm was skipped, it got triggered?Concubinage
@androiddeveloper yes 👍Irradiance
@Senhor Good to know.Concubinage
A
6

How about that I'm using

alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

and I want to push notification in future by alarmMenager It works while app is still running, while I call allarm menager to set notify in futer and close app my notification didn't show. WHat i have to do ?

here u got my event sender:

Calendar cal = Calendar.getInstance();
 cal.set(Calendar.HOUR, HOUR_OF_DAY);
 cal.set(Calendar.MINUTE, MINUTE);
 //cal.add(Calendar.SECOND, SECOND_OF_DAY);
 Intent intent = new Intent(UnityPlayer.currentActivity, TimeAlarm.class);
 intent.putExtra("alarm_status", statusMessage);
 intent.putExtra("alarm_title", title);
 intent.putExtra("alarm_content", content);
 Log.i("SenderEvent ", "przygotowane dane");
 PendingIntent sender = PendingIntent.getBroadcast(UnityPlayer.currentActivity.getApplicationContext(), REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

And Reciever:

Bundle bundle = intent.getExtras();
        String statusMessage = bundle.getString("alarm_status");
        String title = bundle.getString("alarm_title");
        String content = bundle.getString("alarm_content");
        nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(), 0);
Notification notif =new Notification();//R.drawable.ic_launcher,statusMessage, System.currentTimeMillis());;
        //notif.largeIcon = bitmap;
        notif.icon =2130837504;
        notif.tickerText=statusMessage;
        notif.when= System.currentTimeMillis(); 
        /*
        new Notification(0,
                statusMessage, System.currentTimeMillis());*/
        notif.setLatestEventInfo(context, title, content, contentIntent);
        nm.notify(NOTIFY_ME_ID, notif);

Whats wrong with that to push notification in the future while app is close ?

Akiko answered 10/6, 2013 at 11:1 Comment(0)
C
4

Instead of using alarmManager.setRepeating use alarmManager.set to set one time alarm

Croatian answered 28/3, 2013 at 8:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.