日期时间选取器警报管理器的值错误



我正在使用 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) - 我应该将其保存在数据库中的什么

位置

尝试使用它

 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

可以这样解决。接受这两个答案..法典:取而代之的是这个

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

而不是"0"使用唯一值作为第二个参数来获取多个广播。

最新更新