我在android中制作一个警报应用程序。我已经对所有类型的警报(星期一,星期二等)使用了不同的待定意图,重复警报或单个警报。现在,在我的应用程序中,用户可以为我使用SQlite数据库设置任意数量的警报。 我的问题是如何将我的警报细节放在SQlite数据库中,或者如何将所有待处理的意图放在数据库中?
这是我的警报代码:
案例R.id.buttonSetAlarm:
if(onealarm == true)
{
onealarm();
}
else if(repeatingalarm == true)
{
if(all == true||(monday==true && tuesday==true && wednesday==true && thursday==true && friday==true && saturday==true && sunday==true))
{ alldaysalarm();
monday=false;
tuesday=false;
wednesday=false;
thursday=false;
friday=false;
saturday=false;
sunday=false;
}
else if(monday==false && tuesday==false && wednesday==false && thursday==false && friday==false && saturday==false && sunday==false && all==false)
{Toast.makeText(Alarm.this, "Alarm Cannot Set", Toast.LENGTH_SHORT).show();}
else
{
if(monday == true)
{mondayalarm();}
if(tuesday == true)
{tuesdayalarm();}
if(wednesday == true)
{wednesdayalarm();}
if(thursday == true)
{thursdayalarm();}
if(friday == true)
{fridayalarm();}
if(saturday == true)
{saturdayalarm();}
if(sunday == true)
{sundayalarm();}
}
}
if(radioButtonOneAlarm.isChecked()||radioButtonRepeatingAlarm.isChecked())
{
finish();
}
else
{
Toast.makeText(Alarm.this,"Please Set One Type Of Alarm \n Or Press Back To Go To Previous Menu" , Toast.LENGTH_LONG).show();
}
finish();
break;
}
}
public void alldaysalarm()
{ alarmmanageralarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
calendar.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,timePicker.getCurrentMinute());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
if (calendar.compareTo(calendar1) <= 0)
{
calendar.add(Calendar.DATE, 1);
}
Intent intent = new Intent("com.example.crazyyalarm.MyServiceAlarm");
PendingIntent pendingintentalarm = PendingIntent.getActivity(getBaseContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
alarmmanageralarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),86400000 ,pendingintentalarm);
Toast.makeText(Alarm.this, "Alarm Set For All Days", Toast.LENGTH_SHORT).show();
String pi = pendingintentalarm.toString();
}
public void mondayalarm()
{ calendar.set(Calendar.DAY_OF_WEEK,2);
repeating();
PendingIntent pendingintentalarm2 = PendingIntent.getActivity(getBaseContext(), 2, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
alarmmanageralarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),604800000,pendingintentalarm2);
}
public void tuesdayalarm()
{ calendar.set(Calendar.DAY_OF_WEEK, 3);
repeating();
PendingIntent pendingintentalarm3 = PendingIntent.getActivity(getBaseContext(), 3, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
alarmmanageralarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),604800000 ,pendingintentalarm3);
}
public void wednesdayalarm()
{ calendar.set(Calendar.DAY_OF_WEEK, 4);
repeating();
PendingIntent pendingintentalarm4 = PendingIntent.getActivity(getBaseContext(), 4, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
alarmmanageralarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),604800000 ,pendingintentalarm4);
}
public void thursdayalarm()
{ calendar.set(Calendar.DAY_OF_WEEK, 5);
repeating();
PendingIntent pendingintentalarm5 = PendingIntent.getActivity(getBaseContext(), 5, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
alarmmanageralarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),604800000 ,pendingintentalarm5);
}
public void fridayalarm()
{ calendar.set(Calendar.DAY_OF_WEEK, 6);
repeating();
PendingIntent pendingintentalarm6 = PendingIntent.getActivity(getBaseContext(), 6, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
alarmmanageralarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),604800000 ,pendingintentalarm6);
}
public void saturdayalarm()
{ calendar.set(Calendar.DAY_OF_WEEK, 7);
repeating();
PendingIntent pendingintentalarm7 = PendingIntent.getActivity(getBaseContext(), 7, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
alarmmanageralarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),604800000 ,pendingintentalarm7);
}
public void sundayalarm()
{ calendar.set(Calendar.DAY_OF_WEEK, 1);
repeating();
PendingIntent pendingintentalarm1 = PendingIntent.getActivity(getBaseContext(), 1, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
alarmmanageralarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),604800000 ,pendingintentalarm1);
}
public void onealarm()
{
Calendar calendar1 = Calendar.getInstance();
Calendar calendar = (Calendar) calendar1.clone();
alarmmanageralarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
calendar.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,timePicker.getCurrentMinute());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
//calendar.set(Calendar.DAY_OF_WEEK, 1);
if (calendar.compareTo(calendar1) <= 0) {
// Today Set time passed, count to tomorrow
calendar.add(Calendar.DATE, 1);
}
Intent intent = new Intent("com.example.crazyyalarm.MyServiceAlarm");
PendingIntent pendingintentalarm10 = PendingIntent.getActivity(getBaseContext(), 10, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
alarmmanageralarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingintentalarm10);
Toast.makeText(Alarm.this, "Alarm Set", Toast.LENGTH_SHORT).show();
String pi = pendingintentalarm10.toString();
}
public void repeating()
{
Calendar calendar1 = Calendar.getInstance();
Calendar calendar = (Calendar) calendar1.clone();
alarmmanageralarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
calendar.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,timePicker.getCurrentMinute());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
if(calendar1.get(Calendar.DAY_OF_WEEK)>calendar.get(Calendar.DAY_OF_WEEK))
{
calendar.add(Calendar.DATE, 7);
}
if (calendar.compareTo(calendar1) <= 0) {
calendar.add(Calendar.DATE, 7);
}
Intent intent = new Intent("com.example.crazyyalarm.MyServiceAlarm");
}
答案 0 :(得分:0)
PendingIntent使用id和参数,你可以创建一个生成唯一id的类并按意图分配它,在databasse中存储你需要存储id和参数,如果你想要历史,你可以存储intent是否有效否则您可能想要从数据库中删除意图记录。所以你的简单数据库看起来像这样。
---------------------------
Intent
---------------------------
id (unique)
is_active
parameter_1
parameter_2
...
---------------------------
答案 1 :(得分:0)
我建议您创建Alarm表并维护所有字段信息 因为你需要和eg.alarm重复mon,tue等。
为每个警报创建记录并将警报记录ID作为 pendingIntent请求代码,我建议你创建报警给定 下面并交叉验证alarmReciever(BroadcastReciever)那个闹钟 记录在一周中的特定日期推送警报活动屏幕
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(getApplicationContext(), AlarmReciever.class);
alarmIntent.setAction(IConstants.ACTION_SET_CLOCK_ALARM);//put your action
alarmIntent.putExtra(IConstants.ALARM_REQUEST_CODE,alarm.getAlarmId());// pass alarm id
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,alarm.getAlarmId(), alarmIntent, 0);
mgr.cancel(pendingIntent);
mgr.setRepeating(AlarmManager.RTC_WAKEUP,time in millis,AlarmManager.INTERVAL_DAY, pendingIntent);
//alarmReciever
public class AlarmReciever extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (/*alarm in this week day*/) {
//push your alarm screen
}
}