我阅读了很多教程但没有解决方案。我想使用AlarmManager进行同步。我希望当我在系统中选择“after15Minutes”复选框警报设置当前时间时(如何在数据库中保存该特定时间。是否在数据库中节省时间?)和刷新列表每15分钟一次。我不知道如何将checkBoxes值与警报绑定以及如何使其成为可能。完全卡住了。
我的代码:
private final class syncOkClickListener implements DialogInterface.OnClickListener
{
public void onClick(DialogInterface dialog, int which)
{ //Implement search method here
if(after15mint.isChecked())
{
// Some code!!! or Alarm Manager !!!
// Toast.makeText(getApplicationContext(), "15 Minute checked", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Default", Toast.LENGTH_LONG).show();
}
}
BroadcastReceiver活动:
public class BroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
}
public void SetAlarm(Context context)
{
AlarmManager alarmManager = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
Intent syncIntent = new Intent(context, BroadCastReceiver.class);
syncIntent.putExtra("sync", 0);
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, syncIntent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000*20, pIntent);
}
public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, BroadCastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
任何建议都会有所帮助。谢谢。
答案 0 :(得分:0)
您可以在onClick代码中设置闹钟:
public void onClick(DialogInterface dialog, int which)
{ //Implement search method here
if(after15mint.isChecked())
{
// Create alarm intent.
Intent intent = new Intent(this, BroadCastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
// Schedule alarm.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 15);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
}
else
{
Toast.makeText(getApplicationContext(), "Default", Toast.LENGTH_LONG).show();
}
}
您无需在自己的数据库中保存闹钟时间。
关于您阅读的许多教程没有帮助,一个好的起点是Android SDK的ApiDemos,您可以在ApiDemos / App / Alarm / AlarmController中找到实现一次性警报以及重复警报的工作代码。