Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set android alarm at specific time

i set the alarm at specific time but every time i open the application it will turn on this is the code i used :

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0010000,intent,0);

Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR, 5);
time.set(Calendar.MINUTE, 59);
time.set(Calendar.SECOND, 0);

alarmManager.set(AlarmManager.RTC,time.getTimeInMillis(),pendingIntent);
like image 784
israa Avatar asked Nov 15 '11 16:11

israa


People also ask

How do you set an alarm for a certain time on Android?

In the main activity, AndroidTimeActivity, the main code to start Alarm is in setAlarm(Calendar targetCal) method. Retrieve AlarmManager by through getSystemService(Context. ALARM_SERVICE). And set our alarm with alarm type(RTC_WAKEUP), trigger time, and pendingIntent.

Can schedule exact alarms?

"Alarms & reminders" special app access page in system settings, where users can allow your app to set exact alarms. If necessary, you can send users to the Alarms & reminders screen in system settings, as shown in Figure 1.

What is Android alarm Manager?

android.app.AlarmManager. This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future.


1 Answers

Alright, you need to set the alarm to ring the next time it is 5:59:00. You do this by getting the current time, if its before 5:59:00, set the alarm, if its after 5:59:00 then add a day and set the alarm. Do it like so:

import java.util.Calendar;
import java.util.Date;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(new Panel(this));

    Date dat  = new Date();//initializes to now
    Calendar cal_alarm = Calendar.getInstance();
    Calendar cal_now = Calendar.getInstance();
    cal_now.setTime(dat);
    cal_alarm.setTime(dat);
    cal_alarm.set(Calendar.HOUR_OF_DAY,5);//set the alarm time
    cal_alarm.set(Calendar.MINUTE, 59);
    cal_alarm.set(Calendar.SECOND,0);
    if(cal_alarm.before(cal_now)){//if its in the past increment
        cal_alarm.add(Calendar.DATE,1);
    }
    //SET YOUR AlarmManager here

}

I wanted to give you a buildable example, but i don't fully understand alarmmanager yet, so this is what you have. Built on eclipse 3.5.2 with ADK 15

like image 133
Chris Avatar answered Oct 03 '22 07:10

Chris