Reputation: 186
I am working on an app, I store schedules on everyday basis, eg there will be 6 schedule each day but timming will be different for each day, I have stored Scheduled and there timings in sqlite in a table, Now i want to make alarm for these schedules, How should i do so. The alarms be like eg, for sunday 9:30 am, 10:40 am, 11:00 am , 12 pm, 4 pm, 5pm and for monday 7 am , 8am etc. I am using this code to set alarm on specific time
calSet.set(Calendar.DAY_OF_WEEK, week);
calSet.set(Calendar.HOUR_OF_DAY, hour);
calSet.set(Calendar.MINUTE, minuts);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calSet.getTimeInMillis(), 1 * 60 * 60 * 1000, pendingIntent);
Upvotes: -1
Views: 3064
Reputation: 4021
If you don't use SQlite then it's very easy, you can use PendingIntent with different request code.
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), requestcode, intent, 0);
requestcode+=1;
I have posted 3 classes using these classes you can get your task.
SetAlarms.java class actually set alarm
public class SetAlarms {
Context c;
String SaveStateVal;
public SetAlarms(Context c) {
this.c = c;
}
public void AlarmSet(int i, Calendar targetCal) {
Intent intent = new Intent(c, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(c, i, intent,
0);
AlarmManager alarmManager = (AlarmManager) c
.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
pendingIntent);
}
}
This is AlarmReciever.java class when alarm is comes then this will be call
public class AlarmReciever extends BroadcastReceiver {
MediaPlayer mP;
@Override
public void onReceive(Context context, Intent intent) {
// /** Play sound */
mP = MediaPlayer.create(context, R.raw.tn);
mP.start();
// Show the toast like in above screen shot
Toast.makeText(context, "Congratulation Alarm is Triggered",
Toast.LENGTH_LONG).show();
}
}
Mainactivity.java class you can provide value(time) to the alarm class simply, using Timepacker you can set alarm.
public class MainActivity extends Activity implements OnClickListener {
TimePickerDialog tPickerDialog;
SharedPreferences StatPref;
TextView tvtime, sttime;
Button setTim, getRec;
SetAlarms ar;
String str;
int Count = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StatPref = getSharedPreferences("mfile", Context.MODE_PRIVATE);
reference();
tvtime.setText(getStr());
setTim.setOnClickListener(this);
sttime.setOnClickListener(this);
getRec.setOnClickListener(this);
}
private void reference() {
ar = new SetAlarms(this);
sttime = (TextView) findViewById(R.id.sttime);
getRec = (Button) findViewById(R.id.updtTime);
tvtime = (TextView) findViewById(R.id.tim);
tvtime.setText(getStr());
setTim = (Button) findViewById(R.id.sctime);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sctime:
openTimePickerDialog(false);
tvtime.setText(getStr());
break;
case R.id.sttime:
openTimePickerDialog(false);
break;
case R.id.updtTime:
tvtime.setText(getStr());
break;
}
}
private void openTimePickerDialog(boolean is24r) {
Calendar calendar = Calendar.getInstance();
tPickerDialog = new TimePickerDialog(MainActivity.this,
onTimeSetListener, calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), is24r);
tPickerDialog.setTitle("Set Alarm Time");
tPickerDialog.show();
}
OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if (calSet.compareTo(calNow) <= 0) {
// Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
// setAlarm(calSet);
if (Count <= 6) {
ar.AlarmSet(Count, calSet);
str = "\n" + "Alarm " + Count + " Set " + calSet.getTime();
Editor editrr = StatPref.edit();
editrr.putString("ref" + Count, str);
editrr.commit();
Count++;
} else {
Toast.makeText(
MainActivity.this,
"Set 6 Alarms at Same Time Restart Your App",
Toast.LENGTH_SHORT).show();
}
}
};
private String getStr() {
str = StatPref.getString("ref1", "");
str = str + "\n" + StatPref.getString("ref2", "");
str = str + "\n" + StatPref.getString("ref3", "");
str = str + "\n" + StatPref.getString("ref4", "");
str = str + "\n" + StatPref.getString("ref5", "");
str = str + "\n" + StatPref.getString("ref6", "");
return str;
}
}
main.xml for MainActivity.java class , use this xml as a layout
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Alarm Manager"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/sttime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:gravity="center"
android:text="Set Time"
android:textSize="20sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="220sp"
android:layout_gravity="center"
android:gravity="center" >
<TextView
android:id="@+id/tim"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/sctime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="scheduleAlarm"
android:text="Schedule Alarm" />
<Button
android:id="@+id/updtTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Updated View" />
</LinearLayout>
</LinearLayout>
Upvotes: 1
Reputation: 855
Save alarm time in database. Don't set repeating alarm but a single one. When your first alarm is triggered (when you receive the alarm event through broadcastreceiver) query the database, and set next alarm.
So the idea is simple - when you execute an alarm set the next alarm then. You have to implement the logic how to find the next alarm time.
Upvotes: 0