0

我正在使用 AlarmManager 设置警报,我想将它的 id 保存在我的 sqlite 数据库中,每当我设置警报时,它会立即触发,我搜索了解决方案,但没有根据我的要求找到,可能是我弄错了我的日期时间选择器小部件中的日期和时间,
我将日期和时间选择器用作:

final DatePicker datePicker = (DatePicker)dialog.findViewById(R.id.datePicker);
final TimePicker timePicker = (TimePicker)dialog.findViewById(R.id.timePicker);

然后出现一个对话框,我更改日期和时间,然后将其获取为:

Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 
timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
timeSetForAlert = calendar.getTimeInMillis()/1000;

然后我将它传递给服务处理程序,例如,

Intent myIntent = new Intent(thisContext, MyReceiver.class);
myIntent.putExtra("timeSetForAlert", timeSetForAlert);
pendingIntent = PendingIntent.getBroadcast(thisContext, 0, myIntent,0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, timeSetForAlert, pendingIntent);

我在这里跳过广播接收器,它接收意图并启动警报服务,而警报服务如下:

public void onStart(Intent intent, int startId)
{
       super.onStart(intent, startId);

   long timeSetForAlert = intent.getLongExtra("timeSetForAlert", 0);
   mManager = (NotificationManager) thisContext.getSystemService(thisContext.NOTIFICATION_SERVICE);

   Intent intent1 = new Intent(thisContext,MainActivity.class);

   Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", timeSetForAlert);

   intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

   PendingIntent pendingNotificationIntent = PendingIntent.getActivity( thisContext,0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
   notification.flags |= Notification.FLAG_AUTO_CANCEL;
   notification.setLatestEventInfo(thisContext, "Notification..", "This is a test message!", pendingNotificationIntent);

   mManager.notify(startId, notification);
   Toast.makeText(thisContext, startId+"", Toast.LENGTH_SHORT).show();
}

请建议:
1)-为什么警报会立即触发
2)-我应该将它保存在我的数据库中的哪个位置

4

2 回答 2

1

尝试使用这个

 Calendar calendar = Calendar.getInstance();
 calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 
 timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
 timeSetForAlert = calendar.getTimeInMillis();//try without dividing with 1000
于 2013-11-13T10:09:30.940 回答
0

是的,可以这样解决。接受两个答案..代码:而不是这个

        pendingIntent = PendingIntent.getBroadcast(thisContext, 0, myIntent,0);

        PendingIntent pi = PendingIntent.getBroadcast(
                getApplicationContext(), uniqueValue, intent, 0);

instead of "0" use unique value as second parameter to get multipleBroadcast.

于 2013-11-13T10:26:50.333 回答