我正在做一个闹钟定时器,虽然没有闹钟时间,但是当我重新启动时它被调用了。我使用BroadcastReceiver重新启动并加载数据备份DataBase。这是我的代码:
public class SampleBootReceiver extends BroadcastReceiver {
private PendingIntent pendingIntent;
AlarmManager alarmManager;
int ID;
String phone,sms,day,hour,min,rule,pending;
@Override
public void onReceive(Context context, Intent intent) {
DatabaseHandler db ;
db=new DatabaseHandler(context);
List<Contact> contactsList = db.getAllContacts();
for (Contact cn : contactsList) {
ID =cn.getId();
phone = cn.getPhoneNumber();
sms = cn.getSmsBody();
day= cn.getDay();
hour =cn.getHour();
min =cn.getMinutes();
rule =cn.getRule();
if (rule.equalsIgnoreCase("everyday")){
SetEveryday(context,hour,min,day,ID);
}
// Writing Contacts to log
}
}
public void SetEveryday(Context context,String hour ,String minute,String date, int idAlarm){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
calendar.set(Calendar.MINUTE, Integer.parseInt(minute));
calendar.set(Calendar.DATE, Integer.parseInt(date));
Intent myIntent = new Intent(context,AlarmReceiver.class);
myIntent.putExtra("ID",idAlarm);
alarmManager = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(context, idAlarm, myIntent, pendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 240000, pendingIntent);
Toast.makeText(context,"livsfgsg"+idAlarm,Toast.LENGTH_SHORT).show();
}
}
非常感谢