How to set one time alarm in android

Joy Helina picture Joy Helina · Mar 28, 2013 · Viewed 9.6k times · Source

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

Answer

Pratik picture Pratik · Mar 28, 2013

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

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