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