我正在开发一个应用程序,我每天存储时间表,例如每天会有6个时间表,但每天的调整会有所不同,我已经存储了Scheduled和sqlite中的时间表,现在我想要要对这些时间表发出警报,我应该怎么做。 警报类似于例如星期日上午9:30,上午10点40分,上午11点,下午12点,下午4点,下午5点以及星期一早上7点,早上8点等。 我正在使用此代码在特定时间设置闹钟
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);
答案 0 :(得分:1)
如果您不使用SQlite,那么它非常简单, 您可以将PendingIntent与不同的请求代码一起使用。
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), requestcode, intent, 0);
requestcode+=1;
我已经使用这些课程发布了3个课程,您可以完成任务。
SetAlarms.java 类实际上设置了警报
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);
}
}
这是 AlarmReciever.java 类,当闹钟响起时,这将被调用
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 类可以简单地为报警类提供值(时间),使用Timepacker可以设置报警。
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;
}
}
MainActivity.java 类的main.xml ,使用此xml作为布局
<?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>
答案 1 :(得分:0)
在数据库中保存闹钟时间。 不要设置重复闹钟而是单个闹钟。 当您的第一个警报被触发时(当您通过broadcastreceiver收到警报事件时)查询数据库,然后设置下一个警报。
所以这个想法很简单 - 当你执行一个闹钟时,设置下一个闹钟。你必须实现如何找到下一个闹钟时间的逻辑。