我正在做计时器应用,但我不明白程序错误的原因。这是我的代码
public class SampleBootReceiver extends BroadcastReceiver {
private PendingIntent pendingIntent;
AlarmManager alarmManager;
@Override
public void onReceive(Context context, Intent intent) {
DatabaseHandler db ;
db=new DatabaseHandler(context);
Contact cn= db.getContact(1);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(cn.getHour()));
calendar.set(Calendar.MINUTE, Integer.parseInt(cn.getMinutes()));
calendar.set(Calendar.DATE, Integer.parseInt(cn.getDay()));
Intent myIntent = new Intent(context,AlarmReceiver.class);
Log.w("log", "HOURS" +Integer.parseInt(cn.getHour()));
Log.w("log", "MINUTE" +Integer.parseInt(cn.getMinutes()));
Log.w("log", "DAY" +Integer.parseInt(cn.getDay()));
Log.w("log", "id" + cn.getId());
Log.w("log", "phone" + cn.getPhoneNumber());
Log.w("log", "sms" + cn.getSmsBody());
pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, pendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 20000, pendingIntent);
}
}
这是alarmManager的行alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 20000, pendingIntent);
答案 0 :(得分:1)
到目前为止,我在您的代码中看到,您从未初始化alarmManager
,因此当您致电setRepeating
时它为空。
在调用此方法之前,您应该有类似这样的内容
alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);