从sqlite重启重启警报

时间:2014-09-07 11:47:18

标签: android sqlite cursor alarmmanager

我重复设置并保存在sqlite数据库中的警报,我现在正尝试使用广播接收器在重启时应用这些相同的警报。没有任何事情发生,我没有看到我的logcat中的任何错误...有人可以检查下面,也许我会这样做错了吗?也许上下文错了?

我正在使用游标从我的表中获取数据,然后使用for循环设置这些警报在特定日期的时间的压缩时间。然后我将压延时间发送到报警方法

public class StartAlarmReceiver extends BroadcastReceiver {

private Context context; 

@Override
public void onReceive(Context c, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        // Set alarms after reboot
        context = c; 
        int[] daysOfWeek = new int[7]; 
        int[] daysOfWeekValue = new int[7]; 

        final DatabaseHandler db = new DatabaseHandler(context);
        Cursor cursor = db.getPersonalAlarms();
        while (cursor.moveToNext()) {

            final int sunday = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_SUNDAY_PERSONAL)));
            daysOfWeek[0] = sunday; 
            final int monday = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_MONDAY_PERSONAL)));
            daysOfWeek[1] = monday; 
            final int tuesday = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_TUESDAY_PERSONAL)));
            daysOfWeek[2] = tuesday; 
            final int wednesday = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_WEDNESDAY_PERSONAL)));
            daysOfWeek[3] = wednesday; 
            final int thursday = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_THURSDAY_PERSONAL)));
            daysOfWeek[4] = thursday; 
            final int friday = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_FRIDAY_PERSONAL)));
            daysOfWeek[5] = friday; 
            final int saturday  = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_SATURDAY_PERSONAL)));
            daysOfWeek[6] = saturday; 

            //for use with alarm for loop
            daysOfWeekValue[0]=1; 
            daysOfWeekValue[1]=2; 
            daysOfWeekValue[2]=3; 
            daysOfWeekValue[3]=4; 
            daysOfWeekValue[4]=5; 
            daysOfWeekValue[5]=6; 
            daysOfWeekValue[6]=7; 

            final int repeating = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_REPEAT_WEEKLY_PERSONAL)));

            int starthour = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_LOCK_START_HR_PERSONAL)));
            int startmin = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_LOCK_START_MIN_PERSONAL)));
            int stophour = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_LOCK_STOP_HR_PERSONAL)));
            int stopmin = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_LOCK_STOP_MIN_PERSONAL)));


            for (int i=0; i<daysOfWeek.length; i++){
                if(daysOfWeek[i]==1){
                    final int request_id = daysOfWeekValue[i]; 
            final Calendar calNow = Calendar.getInstance();
            final Calendar calSet = (Calendar) calNow.clone();

            calSet.set(Calendar.DAY_OF_WEEK, request_id);// 1 for sunday, 2 for monday etc
            calSet.set(Calendar.HOUR_OF_DAY, starthour);
            calSet.set(Calendar.MINUTE, startmin);
            calSet.set(Calendar.SECOND, 0);
            calSet.set(Calendar.MILLISECOND, 0);

            if (calSet.compareTo(calNow) <= 0)
                //Today Set time passed, count to tomorrow
                calSet.add(Calendar.DATE, 1);

            final Calendar calNow2 = Calendar.getInstance();
            final Calendar calSet2 = (Calendar) calNow2.clone();

            calSet2.set(Calendar.DAY_OF_WEEK, request_id);// 0 for sunday
            calSet2.set(Calendar.HOUR_OF_DAY, stophour);
            calSet2.set(Calendar.MINUTE, stopmin);
            calSet2.set(Calendar.SECOND, 0);
            calSet2.set(Calendar.MILLISECOND, 0);

            if (calSet2.compareTo(calNow2) <= 0)
                //Today Set time passed, count to tomorrow
                calSet2.add(Calendar.DATE, 1);

            gcmalarmset(calSet, request_id, repeating);

                }
            }
        }

    }
}

private void gcmalarmset(Calendar targetCal, String newpassword, int id, int repeating) {


    final String password = newpassword;
    //set alarm
    final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final Intent intent = new Intent(context, LockReceiver.class);
    final Bundle bundle = new Bundle();
    bundle.putString("password", password);
    intent.putExtras(bundle);
    final PendingIntent pendingIntent = PendingIntent.getBroadcast(
            context, id, intent, 0);

    if(repeating == 1){
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis()
            ,AlarmManager.INTERVAL_DAY * 7,pendingIntent);}
    else if(repeating == 0){
        alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis()
                ,pendingIntent);
    }

}

}
}

非常感谢

0 个答案:

没有答案