-3

I am a Beginner in Android Development

I am using a time picker widget and alarmmanager service to set alarm at a time. but if the user sets time that is past in that day. alarm is getting triggered immediately... Please help me How to prevent that ie alarm should trigger next day if the time is past Just like System alarm app does.

Is there another method rather than Alarm Manager to achieve the same result.?

Here is my main activity//

1    package com.example.friends.myalarm; 
2     
3    import android.annotation.SuppressLint; 
4    import android.app.PendingIntent; 
5    import android.content.Context; 
6    import android.content.Intent; 
7    import android.support.v7.app.AppCompatActivity; 
8    import android.os.Bundle; 
9    import android.app.AlarmManager; 
10   import android.text.format.Time; 
11   import android.view.View; 
12   import android.widget.Button; 
13   import android.widget.EditText; 
14   import android.widget.TextView; 
15   import android.widget.TimePicker; 
16   import android.widget.Toast; 
17    
18   import java.util.Calendar; 
19    
20    
21   public class MainActivity extends AppCompatActivity { 
22       AlarmManager al; 
23       private PendingIntent pt; 
24   Calendar c; 
25       TimePicker tp; 
26    
27       TextView tv; 
28    
29       @Override 
30       protected void onCreate(Bundle savedInstanceState) { 
31           super.onCreate(savedInstanceState); 
32           setContentView(R.layout.activity_main); 
33            tp = (TimePicker)findViewById(R.id.timePicker); 
34           Button bt; 
35    
36    
37    
38   bt = (Button)findViewById(R.id.button); 
39    
40    
41           bt.setOnClickListener(new View.OnClickListener() { 
42    
43                
44    
45               @Override 
46               public void onClick(View v) { 
47                   al = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
48                   c = Calendar.getInstance(); 
49                   c.set(Calendar.HOUR_OF_DAY, tp.getHour()); 
50                   c.set(Calendar.MINUTE, tp.getMinute()); 
51                   c.set(Calendar.SECOND, 0); 
52    
53                   if (tp.getHour() < 12) { 
54                       c.set(Calendar.AM_PM, Calendar.AM); 
55                   } else { 
56                       c.set(Calendar.AM_PM, Calendar.PM); 
57                   } 
58    
59                   Intent myt = new Intent(MainActivity.this, Myreciever.class); 
60                   PendingIntent pint = PendingIntent.getBroadcast(MainActivity.this, 0, myt, 0); 
61                   al.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pint); 
62                   String toaster = "scheduled at" + Integer.toString(tp.getHour()) + ":" + Integer.toString(tp.getMinute()); 
63                   Toast.makeText(MainActivity.this, toaster 
64                           , Toast.LENGTH_SHORT).show(); 
65    
66               } 
67           }); 
68    
69    
70    
71       } 
72   } 
73

..thanks in advance

Vihaari Varma
  • 155
  • 11
  • 1
    can you post your code? It'll be easier to see how we can help. – C0D3LIC1OU5 Jun 22 '16 at 14:30
  • i have added my mainactivity please check now – Vihaari Varma Jun 22 '16 at 17:28
  • Possible duplicate of [android prevent immediate trigger of alarm service if alarm time has passed for the day](http://stackoverflow.com/questions/36535575/android-prevent-immediate-trigger-of-alarm-service-if-alarm-time-has-passed-for) – Mike M. Jun 22 '16 at 20:38

1 Answers1

0

The Android alarm service will trigger every alarm that his programmed time is less than the current time (i.e. it will trigger immediately the alarms that are late). The method "getInstance" of the Calendar class returns you a calendar instance that has the current time. If you are only setting the hours, minutes and seconds of the calendar instance, then it will use the current day, so the alarm will be trigger immediately because the time has just passed. If you want to program for tomorrow an alarm if the selected time is less than the current, then you will need to verify if the selected hour is less than the current and conditionally add one day to the calendar (or sum to the millis result "24*60*60*1000" which is a day in milliseconds). Hope this helps.

josemgu91
  • 719
  • 4
  • 8