Set an alarm at a selected time and date

I am developing a taskmanager on Android 2.1. I want to set an alarm for a task given by datepicker date and time from timer. Help me with the code.

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final EditText next = (EditText) findViewById(R.id.editText1); final Button sub = (Button) findViewById(R.id.button1); final Button res = (Button) findViewById(R.id.button2); final DatePicker dp = (DatePicker) findViewById(R.id.datePicker1); final TimePicker tp = (TimePicker) findViewById(R.id.timePicker1); sub.setOnClickListener(new View.OnClickListener() { public void onClick(final View view) { int y=dp.getYear(); int mo=dp.getMonth(); int day=dp.getDayOfMonth(); Time t = new Time(); int h=t.getCurrentHour(); int m=t.getCurrentMinutes(); } private AlarmManager getSystemService(String alarmService) { // TODO Auto-generated method stub return null; } }); 
+4
source share
5 answers

Hey, how to set the alarm on Android AlarmManager for an urgent date (android alarmmanager set the alarm for the date) I was looking for all this. pay attention to the value of the month!

 Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault()); //cal.add(Calendar.SECOND, 10); cal.set(Calendar.DATE,19); //1-31 cal.set(Calendar.MONTH,Calendar.DECEMBER); //first month is 0!!! January is zero!!! cal.set(Calendar.YEAR,2012);//year... cal.set(Calendar.HOUR_OF_DAY, 16); //HOUR cal.set(Calendar.MINUTE, 39); //MIN cal.set(Calendar.SECOND, 10); //SEC // Create a new PendingIntent and add it to the AlarmManager Intent intent = new Intent(MainActivity.this, alarmAct.class); PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0,intent, 0); //or if you start an Activity //PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,intent, 0); AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent); 
+6
source

Here's how to set an alarm.

 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); //when does this go off? Long mHowLongFromNowInMilliseconds = 10000 //(10 seconds from now) calendar.add(Calendar.MILLISECOND, mHowLongFromNowInMilliseconds); alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

A calendar is not required ... But it can be useful;)

 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, 10000, pendingIntent); 

!!! Remember: alarms are canceled when the device is completely turned off.

http://developer.android.com/reference/android/app/AlarmManager.html

+2
source

Android does not play an alarm for you; the Alarm Manager allows you to plan to launch the application at some point in the future. So, just add the time and pending intentions to the AlarmManager, and when that intent is called up, play the music.

Visit: http://developer.android.com/reference/android/app/AlarmManager.html

+1
source

see the link for an example complelete.

0
source
 public class AlarmService extends Activity { private PendingIntent mAlarmSender; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create an IntentSender that will launch our service, to be scheduled // with the alarm manager. mAlarmSender = PendingIntent.getService(AlarmService.this, 0, new Intent(AlarmService.this, AlarmService_Service.class), 0); setContentView(R.layout.alarm_service); // Watch for button clicks. Button button = (Button)findViewById(R.id.start_alarm); button.setOnClickListener(mStartAlarmListener); button = (Button)findViewById(R.id.stop_alarm); button.setOnClickListener(mStopAlarmListener); } private OnClickListener mStartAlarmListener = new OnClickListener() { public void onClick(View v) { // We want the alarm to go off 30 seconds from now. long firstTime = SystemClock.elapsedRealtime(); // Schedule the alarm! AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 30*1000, mAlarmSender); // Tell the user about what we did. Toast.makeText(AlarmService.this, R.string.repeating_scheduled, Toast.LENGTH_LONG).show(); } }; private OnClickListener mStopAlarmListener = new OnClickListener() { public void onClick(View v) { // And cancel the alarm. AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.cancel(mAlarmSender); // Tell the user about what we did. Toast.makeText(AlarmService.this, R.string.repeating_unscheduled, Toast.LENGTH_LONG).show(); } }; } 
0
source

Source: https://habr.com/ru/post/1382997/


All Articles