我的应用是短信发件人。我写了一个BootCompleteReceiver类,在重置手机后,从数据库中读取短信的时间并重置警报。但问题是大部分时间操作都没有正确完成,也就是说,当手机重启时,会设置警报并立即运行。 (发送短信)。谢谢你的帮助。
public class BootCompleteReceiver extends BroadcastReceiver
{
SQLiteDatabase bootDatabase;
String DIR_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
String DIR_DATABASE = DIR_SDCARD + "/db-sms_Manager_and_Scheduler/";
ArrayList<setAlarmNode> setAlarmNodes = new ArrayList<setAlarmNode>();
@Override
public void onReceive(final Context context, Intent intent) {
//Thread
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
bootDatabase = context.openOrCreateDatabase(DIR_DATABASE + "/database.sqlite", 0, null);
//Read From DB
String name;
String number;
String miladiDate;
String time;
String message;
int requestCode;
String repeat;
String schedulerId;
Boolean isFromSmsGroup = false;
Boolean smsGroup;
Cursor cursor = bootDatabase.rawQuery("SELECT * FROM sms_scheduler where nextOccurrence != '----' ", null);
cursor.moveToFirst();
while ( !cursor.isAfterLast()) {
isFromSmsGroup = false;
schedulerId = cursor.getString(cursor.getColumnIndex("schedulerId"));
name = cursor.getString(cursor.getColumnIndex("contactName"));
number = cursor.getString(cursor.getColumnIndex("contactPhone"));
miladiDate = cursor.getString(cursor.getColumnIndex("nextOccurrence")); /// Notic!
message = cursor.getString(cursor.getColumnIndex("smsText"));
requestCode = cursor.getInt(cursor.getColumnIndex("requestCode"));
repeat = cursor.getString(cursor.getColumnIndex("frequency"));
smsGroup = cursor.getInt(cursor.getColumnIndex("smsGroup")) > 0;
//calculate Date from next Occurrence
String nextOccur = getDate(Long.valueOf(miladiDate), "dd/MM/yyyy hh:mm a"); /////a ?
Log.i("TAG", "we are in BootCompleteReceiver and next occur is : " + nextOccur);
String[] temp = nextOccur.split(" ");
String nextOccurDate = temp[0];
String nextOccurTime = temp[1];
setAlarmNode node = new setAlarmNode();
node.name = name;
node.number = number;
node.nextOccurDate = nextOccurDate;
node.nextOccurTime = nextOccurTime;
node.message = message;
node.requestCode = requestCode;
node.repeat = repeat;
node.SchedulerId = schedulerId;
node.isFromSmsGroup = isFromSmsGroup;
setAlarmNodes.add(node);
///////////////////////////////////setAlarm
// setAlarm(name, number, nextOccurDate, nextOccurTime, message, requestCode, repeat, SchedulerId, isFromSmsGroup);
if (smsGroup) {
isFromSmsGroup = true;
Cursor cursor3 = bootDatabase.rawQuery("SELECT GcontactName,GcontactPhone,requestCode FROM sms_group where GschedulerId = '" + schedulerId + "'", null);
cursor3.moveToFirst();
while ( !cursor3.isAfterLast()) {
name = cursor3.getString(cursor3.getColumnIndex("GcontactName"));
number = cursor3.getString(cursor3.getColumnIndex("GcontactPhone"));
int rCode = cursor3.getInt(cursor3.getColumnIndex("requestCode"));
setAlarmNode node2 = new setAlarmNode();
node2.name = name;
node2.number = number;
node2.nextOccurDate = nextOccurDate;
node2.nextOccurTime = nextOccurTime;
node2.message = message;
node2.requestCode = rCode;
node2.repeat = repeat;
node2.SchedulerId = schedulerId;
node2.isFromSmsGroup = isFromSmsGroup;
setAlarmNodes.add(node2);
///////////////////////////////////setAlarm
//setAlarm(name, number, nextOccurDate, nextOccurTime, message, rCode, repeat, SchedulerId, isFromSmsGroup);
cursor3.moveToNext();
}
cursor3.close();
}
cursor.moveToNext();
}
cursor.close();
for (int x = 0; x < setAlarmNodes.size(); x++)
{
boolean isFromBoot = true;
setAlarmNode temp = new setAlarmNode();
temp = setAlarmNodes.get(x);
ActivityNewScheduler.setAlarm(temp.name, temp.number, temp.nextOccurDate, temp.nextOccurTime, temp.message, temp.requestCode, temp.repeat, temp.SchedulerId, temp.isFromSmsGroup, isFromBoot);
}
}
});
thread.start();
}
setAlarm:
private void setAlarm(final String name, final String number, final String miladiDate, final String time, final String message, final int requestCode, final String repeat, final String SchedulerId, final Boolean isFromSmsGroup) {
String[] nextOccurArrayDate = miladiDate.split("/");
String[] nextOccurArrayTime = time.split(":");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.clear();
calendar.set(Calendar.YEAR, Integer.valueOf(nextOccurArrayDate[2]));
calendar.set(Calendar.MONTH, (Integer.valueOf(nextOccurArrayDate[1]) - 1));
calendar.set(Calendar.DAY_OF_MONTH, Integer.valueOf(nextOccurArrayDate[0]));
calendar.set(Calendar.HOUR, Integer.valueOf(nextOccurArrayTime[0]));
calendar.set(Calendar.MINUTE, Integer.valueOf(nextOccurArrayTime[1]));
Intent intentAlarm = new Intent(G.context, smsActivityBroadCast.class);
intentAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentAlarm.putExtra("NAME", name);
intentAlarm.putExtra("NUMBER", number);
intentAlarm.putExtra("MESSAGE", message);
intentAlarm.putExtra("FREQUENCY", repeat);
intentAlarm.putExtra("SID", SchedulerId);
intentAlarm.putExtra("ISFROMSMSGROUP", isFromSmsGroup);
// PendingIntent pendingIntent = PendingIntent.getBroadcast(G.context, requestCode, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(G.context, requestCode, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
if (repeat.equals("Once")) {
G.alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
;
}
if (repeat.equals("every HALF_HOUR")) {
G.alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
}
if (repeat.equals("HOUR")) {
G.alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HOUR, pendingIntent);
}
if (repeat.equals("2HOUR")) {
G.alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HOUR * 2, pendingIntent);
}
if (repeat.equals("8HOUR")) {
G.alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HOUR * 8, pendingIntent);
}
if (repeat.equals("DAY")) {
G.alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
if (repeat.equals("Week")) {
G.alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);
}
if (repeat.equals("Month")) {
G.alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 30, pendingIntent);
}
if (repeat.equals("Year")) {
G.alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 365, pendingIntent);
}
}