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

时间:2013-11-13 09:42:22

标签: android alarmmanager

我正在使用AlarmManager来设置闹钟,我想将其id保存在我的sqlite数据库中,每当我设置闹钟时,它立即触发,我用Google搜索解决方案,但根据我的要求找不到,可能是我从我的约会时间选择器小部件得到错误的日期和时间,
我使用日期和时间选择器:

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) - 我应该把它保存在我的数据库中

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

你可以这样解决。 同时接受答案.. 码: 而不是这个

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

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

而不是“0”使用唯一值作为第二个参数来获得multipleBroadcast。

相关问题