【发布时间】:2016-07-05 12:43:27
【问题描述】:
我正在尝试创建一种清除数据库中某些数据的方法的测试,取消一个现有的 AlarmManager(清除相同的 PendingIntent)并删除存储中的文件等。
测试基本上包括三个步骤: - 启动创建警报管理器并将数据放入数据库的方法 - 启动清除一切的方法 - 所有检查
当 Robolectric 尝试使用 NullPointerException 创建挂起的 Intent 时,测试失败。
@Test
public void testClearExpiredVideoDownloadedContent() {
mDatabase.post_action().addVideo(mVideoSample, StaticDbConfig.TableNames.DOWNLOADED_CONTENT);
//Run the method to create the timer for downloaded content into the database
int numberAlarmCreated = SuperBase.setUpContextExpireAlarm(mContext);
assertEquals("Alarm has not been created", 1, numberAlarmCreated);
//Run method to clear the expired content
mDatabase.delete_action().clearExpiredVideoContent(mContext, mVideoSample);
//ALL THE CHECKS
}
当我使用挂起的意图时,方法 setUpContextExpireAlarm 正在崩溃:
Intent intent = new Intent(context, ExpiredContentReceiver.class);
intent.putExtra(ExpiredContentReceiver.EXTRA_VIDEO_INFO, video);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, video.getId(), intent, PendingIntent.FLAG_NO_CREATE);
SimpleDateFormat format = new SimpleDateFormat(StaticAppConfig.MatDateFormat);
Date d = format.parse(video.getExpiryDate());
alarmMgr.setRepeating( //CRASHING HERE BECAUSE alarmIntent is null
AlarmManager.RTC_WAKEUP,
d.getTime(),
AlarmManager.INTERVAL_DAY,
alarmIntent);
编辑
这是创建警报管理器和待处理意图的方法:
public static int setUpContextExpireAlarm(Context context){
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
ArrayList<VideosV0> videosV0s = new DatabaseHandler(context, null).get_action().getNotExpiredDownloadedContent();
for(VideosV0 video : videosV0s){
try {
Intent intent = new Intent(context, ExpiredContentReceiver.class);
intent.putExtra(ExpiredContentReceiver.EXTRA_VIDEO_INFO, video);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, video.getId(), intent, PendingIntent.FLAG_NO_CREATE);
SimpleDateFormat format = new SimpleDateFormat(StaticAppConfig.MatDateFormat);
Date d = format.parse(video.getExpiryDate());
alarmMgr.setRepeating(
AlarmManager.RTC_WAKEUP,
d.getTime(),
AlarmManager.INTERVAL_DAY,
alarmIntent);
Logging.i("Setting up the alarm for content ["+video.getTitle()+" ("+video.getId()+")], the alarm will be triggered on ["+video.getExpiryDate()+"("+d.getTime()+")] ");
} catch (ParseException e) {
e.printStackTrace();
}
}
return videosV0s.size();
}
【问题讨论】:
-
如果
alarmIntent是null,这意味着不存在PendingIntent。请首先发布您用于创建警报的代码。 -
我添加了 setUpContextExpireAlarm 方法来设置所有警报。我认为这是 Robolectric 的问题,因为在设备上正常运行时很好。
-
此调用:
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, video.getId(), intent, PendingIntent.FLAG_NO_CREATE);不会创建PendingIntent。如果您指定PendingIntent.FLAG_NO_CREATE,则不会创建PendingIntent,如果那里已经有一个,它只会返回一个。第一次创建PendingIntent的代码在哪里? -
是那个...这就是为什么不工作的原因。所以,问题是我每次都需要一个不同的待处理意图,因为我必须为每个意图传递不同的额外内容,我不想覆盖它。你建议使用什么?
-
您需要确保
PendingIntent是唯一的。删除标志PendingIntent.FLAG_NO_CREATE并确保在每个Intent上使用唯一的操作或在对PendingIntent.getBroadcast()的调用中使用唯一的requestCode。这将确保它每次都创建一个新的。
标签: android alarmmanager android-pendingintent robolectric