Android:没有在特定日期警报上调用警报接收器

时间:2012-12-11 08:50:10

标签: android alarmmanager android-alarms

我使用sqlite数据库实现了多个警报功能。当我测试我的虚拟应用程序时,每件事情都很好。但是,当我整合到主项目中时,警报仅在每日警报或一次警报时才起作用。警报接收器在我将其设置为特定日期(例如星期一)时从未调用,而同样的事情在虚拟应用程序中正常工作。甚至,我尝试为接收器定义操作并设置android:enabled="true"android:exported="false"。但是,它没有做出任何改变。最糟糕的是没有logcat细节,我不知道问题出在哪里。

为特定日期设置闹钟的方法

private void setDayWiseAlarm(int checkNo)
{
    Intent AlarmIntent = new Intent("com.dzo.HanumanChalisaWithAudioAndAlarm.RECEIVEALARM");
    int repeatId = checkNo + reqCode;

    Log.v("HCAlarmTimeDetail daywise alarm id", reqCode+" "+checkNo+" "+repeatId);
    AlarmManager AlmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

    Log.v("HCAlarmTimeDetail checkNo value: ", ""+checkNo);
    Calendar curCalendar = Calendar.getInstance();
    Calendar alarmCalendar = Calendar.getInstance();

    curCalendar.set(Calendar.SECOND, 0);
    curCalendar.set(Calendar.MILLISECOND, 0);           
    alarmCalendar.set(Calendar.SECOND, 0);
    alarmCalendar.set(Calendar.MILLISECOND, 0);

    RepeatAlarmTime repTime = formatTime();
    alarmCalendar.set(Calendar.DAY_OF_WEEK, checkNo);
    alarmCalendar.set(Calendar.HOUR, repTime.hour);
    alarmCalendar.set(Calendar.MINUTE, repTime.min);

    if(alarmCalendar.before(curCalendar))
    {
        alarmCalendar.add(Calendar.WEEK_OF_YEAR, 1);
        Log.v("HCAlarmTimeDetail in if loop", "alarm set for next week");
    }//if

    if(checkNo == 1)
    {
        String daysToRepeat = alarmTimeDaoToEdit.getDaysToRepeatAlarm();
        Log.v("HCAlarmTimeDetail", "1 daysToRepeat before edit "+daysToRepeat);

        if(!daysToRepeat.equals(null))
        {
            if(daysToRepeat.equals(""))
            {
                daysToRepeat = "Sunday";
                Log.v("HCAlarmTimeDetail daysToRepeat sunday if empty", daysToRepeat);
            }//if
            else if(daysToRepeat.equals("Daily") || daysToRepeat.equals("null"))
            {
                daysToRepeat = "Sunday";
                Log.v("HCAlarmTimeDetail daysToRepeat sunday else if daily or null", daysToRepeat);
            }//if
            else
            {
                daysToRepeat = daysToRepeat + " Sunday";
                Log.v("HCAlarmTimeDetail daysToRepeat sunday else not daily", daysToRepeat);
            }//else
            Log.v("AlarmTimeDetail daysToRepeat sunday check", "daysToRepeatNotNull");
        }//if
        alarmTimeDaoToEdit.setSundayRepeatId(Integer.toString(repeatId));
        alarmTimeDaoToEdit.setDaysToRepeatAlarm(daysToRepeat);
        Log.v("HCAlarmTimeDetail", "Alarm set for sunday");
        Log.v("HCAlarmTimeDetail", "1 daysToRepeat after edit "+daysToRepeat);
    }//if

    if(checkNo == 2)
    {
        String daysToRepeat = alarmTimeDaoToEdit.getDaysToRepeatAlarm();
        Log.v("HCAlarmTimeDetail", "2 daysToRepeat before edit "+daysToRepeat);

        if(!daysToRepeat.equals(null))
        {
            if(daysToRepeat.equals(""))
            {
                daysToRepeat = "Monday";
                Log.v("HCAlarmTimeDetail daysToRepeat monday if empty", daysToRepeat);
            }//if
            else if(daysToRepeat.equals("Daily") || daysToRepeat.equals("null"))
            {
                daysToRepeat = "Monday";
                Log.v("HCAlarmTimeDetail daysToRepeat monday else if daily or null", daysToRepeat);
            }//if
            else
            {
                daysToRepeat = daysToRepeat + " Monday";
                Log.v("HCAlarmTimeDetail daysToRepeat monday else not daily", daysToRepeat);
            }//else
            Log.v("AlarmTimeDetail daysToRepeat monday check", "daysToRepeatNotNull");
        }//if
        alarmTimeDaoToEdit.setMondayRepeatId(Integer.toString(repeatId));
        alarmTimeDaoToEdit.setDaysToRepeatAlarm(daysToRepeat);
        Log.v("HCAlarmTimeDetail", "Alarm set for monday");
        Log.v("HCAlarmTimeDetail", "2 daysToRepeat after edit "+daysToRepeat);
    }//if

    if(checkNo == 3)
    {
        String daysToRepeat = alarmTimeDaoToEdit.getDaysToRepeatAlarm();
        Log.v("HCAlarmTimeDetail", "3 daysToRepeat before edit "+daysToRepeat);

        if(!daysToRepeat.equals(null))
        {
            if(daysToRepeat.equals(""))
            {
                daysToRepeat = "Tuesday";
                Log.v("HCAlarmTimeDetail daysToRepeat tuesday if empty", daysToRepeat);
            }//if
            else if(daysToRepeat.equals("Daily") || daysToRepeat.equals("null"))
            {
                daysToRepeat = "Tuesday";
                Log.v("HCAlarmTimeDetail daysToRepeat tuesday else if daily or null", daysToRepeat);
            }//if
            else
            {
                daysToRepeat = daysToRepeat + " Tuesday";
                Log.v("HCAlarmTimeDetail daysToRepeat tuesday else not daily", daysToRepeat);
            }//else
            Log.v("AlarmTimeDetail daysToRepeat tuesday check", "daysToRepeatNotNull");
        }//if
        alarmTimeDaoToEdit.setTuesdayRepeatId(Integer.toString(repeatId));
        alarmTimeDaoToEdit.setDaysToRepeatAlarm(daysToRepeat);
        Log.v("HCAlarmTimeDetail", "Alarm set for tuesday");
        Log.v("HCAlarmTimeDetail", "3 daysToRepeat after edit "+daysToRepeat);
    }//if

    if(checkNo == 4)
    {
        String daysToRepeat = alarmTimeDaoToEdit.getDaysToRepeatAlarm();
        Log.v("HCAlarmTimeDetail", "2 daysToRepeat before edit "+daysToRepeat);

        if(!daysToRepeat.equals(null))
        {
            if(daysToRepeat.equals(""))
            {
                daysToRepeat = "Wednesday";
                Log.v("HCAlarmTimeDetail daysToRepeat wednesday if empty", daysToRepeat);
            }//if
            else if(daysToRepeat.equals("Daily") || daysToRepeat.equals("null"))
            {
                daysToRepeat = "Wednesday";
                Log.v("HCAlarmTimeDetail daysToRepeat wednesday else if daily or null", daysToRepeat);
            }//if
            else
            {
                daysToRepeat = daysToRepeat + " Wednesday";
                Log.v("HCAlarmTimeDetail daysToRepeat wednesday else not daily", daysToRepeat);
            }//else
            Log.v("AlarmTimeDetail daysToRepeat wednesday check", "daysToRepeatNotNull");
        }//if
        alarmTimeDaoToEdit.setWednesdayRepeatId(Integer.toString(repeatId));
        alarmTimeDaoToEdit.setDaysToRepeatAlarm(daysToRepeat);
        Log.v("HCAlarmTimeDetail", "Alarm set for wednesday");
        Log.v("HCAlarmTimeDetail", "4 daysToRepeat after edit "+daysToRepeat);
    }//if

    if(checkNo == 5)
    {
        String daysToRepeat = alarmTimeDaoToEdit.getDaysToRepeatAlarm();
        Log.v("HCAlarmTimeDetail", "5 daysToRepeat before edit "+daysToRepeat);

        if(!daysToRepeat.equals(null))
        {
            if(daysToRepeat.equals(""))
            {
                daysToRepeat = "Thursday";
                Log.v("HCAlarmTimeDetail daysToRepeat thursday if empty", daysToRepeat);
            }//if
            else if(daysToRepeat.equals("Daily") || daysToRepeat.equals("null"))
            {
                daysToRepeat = "Thursday";
                Log.v("HCAlarmTimeDetail daysToRepeat thursday else if daily or null", daysToRepeat);
            }//if
            else
            {
                daysToRepeat = daysToRepeat + " Thursday";
                Log.v("HCAlarmTimeDetail daysToRepeat thursday else not daily", daysToRepeat);
            }//else
            Log.v("AlarmTimeDetail daysToRepeat thursday check", "daysToRepeatNotNull");
        }//if
        alarmTimeDaoToEdit.setThursdayRepeatId(Integer.toString(repeatId));
        alarmTimeDaoToEdit.setDaysToRepeatAlarm(daysToRepeat);
        Log.v("HCAlarmTimeDetail", "Alarm set for thursday");
        Log.v("HCAlarmTimeDetail", "5 daysToRepeat after edit "+daysToRepeat);
    }//if

    if(checkNo == 6)
    {
        String daysToRepeat = alarmTimeDaoToEdit.getDaysToRepeatAlarm();
        Log.v("HCAlarmTimeDetail", "6 daysToRepeat before edit "+daysToRepeat);

        if(!daysToRepeat.equals(null))
        {
            if(daysToRepeat.equals(""))
            {
                daysToRepeat = "Friday";
                Log.v("HCAlarmTimeDetail daysToRepeat friday if empty", daysToRepeat);
            }//if
            else if(daysToRepeat.equals("Daily") || daysToRepeat.equals("null"))
            {
                daysToRepeat = "Friday";
                Log.v("HCAlarmTimeDetail daysToRepeat friday else if daily or null", daysToRepeat);
            }//if
            else
            {
                daysToRepeat = daysToRepeat + " Friday";
                Log.v("HCAlarmTimeDetail daysToRepeat friday else not daily", daysToRepeat);
            }//else
            Log.v("AlarmTimeDetail daysToRepeat friday check", "daysToRepeatNotNull");
        }//if
        alarmTimeDaoToEdit.setFridayRepeatId(Integer.toString(repeatId));
        alarmTimeDaoToEdit.setDaysToRepeatAlarm(daysToRepeat);
        Log.v("HCAlarmTimeDetail", "Alarm set for friday");
        Log.v("HCAlarmTimeDetail", "6 daysToRepeat after edit "+daysToRepeat);
    }//if

    if(checkNo == 7)
    {
        String daysToRepeat = alarmTimeDaoToEdit.getDaysToRepeatAlarm();
        Log.v("HCAlarmTimeDetail", "7 daysToRepeat before edit "+daysToRepeat);

        if(!daysToRepeat.equals(null))
        {
            if(daysToRepeat.equals(""))
            {
                daysToRepeat = "Saturday";
                Log.v("HCAlarmTimeDetail daysToRepeat saturday if empty", daysToRepeat);
            }//if
            else if(daysToRepeat.equals("Daily") || daysToRepeat.equals("null"))
            {
                daysToRepeat = "Saturday";
                Log.v("HCAlarmTimeDetail daysToRepeat saturday else if daily or null", daysToRepeat);
            }//if
            else
            {
                daysToRepeat = daysToRepeat + " Saturday";
                Log.v("HCAlarmTimeDetail daysToRepeat saturday else not daily", daysToRepeat);
            }//else
            Log.v("AlarmTimeDetail daysToRepeat saturday check", "daysToRepeatNotNull");
        }//if
        alarmTimeDaoToEdit.setSaturdayRepeatId(Integer.toString(repeatId));
        alarmTimeDaoToEdit.setDaysToRepeatAlarm(daysToRepeat);
        Log.v("HCAlarmTimeDetail", "Alarm set for saturday");
        Log.v("HCAlarmTimeDetail", "7 daysToRepeat after edit "+daysToRepeat);
    }//if

    if(alarmTimeDaoToEdit.getDaysToRepeatAlarm().equals("Sunday Monday Tuesday Wednesday " +
            "Thursday Friday Saturday"))
    {
        alarmTimeDaoToEdit.setDaysToRepeatAlarm("Daily");
        Log.v("HCAlarmTimeDetail in daywise alarm in if loop", "Alarm set for everyday");
        alarmEnabled = true;
        /*AlarmIntent.putExtra("ALARM_ENABLED", alarmEnabled);
        AlarmIntent.putExtra("REQUEST CODE", reqCode);
        PendingIntent Sender = PendingIntent.getBroadcast(AlarmTimeDetail.this, reqCode
                , AlarmIntent, 0);
        AlmMgr.setRepeating(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), 
                24 * 60 * 60 * 1000, Sender);*/
        dismissAlarm(1);
        dismissAlarm(2);
        dismissAlarm(3);
        dismissAlarm(4);
        dismissAlarm(5);
        dismissAlarm(6);
        dismissAlarm(7);
        setDailyAlarm();
    }//if
    else
    {
        alarmEnabled = true;
        AlarmIntent.putExtra("ALARM_ENABLED", alarmEnabled);
        AlarmIntent.putExtra("REQUEST CODE", repeatId);
        alarmDBHelper.updateAlarmTime(alarmTimeDaoToEdit);
        PendingIntent Sender = PendingIntent.getBroadcast(getApplicationContext(), repeatId
                , AlarmIntent, 0);
        AlmMgr.setRepeating(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), 
                AlarmManager.INTERVAL_DAY * 7, Sender);
        Log.v("HCAlarmTimeDetail in daywise alarm in else loop", "Alarm set daywise");
    }//else
    //Build the Strings for displaying the alarm time through Toast
    String CalendarHourStr;
    if (repTime.hour > 12) 
    {
        CalendarHourStr = Integer.toString(repTime.hour - 12);
    }//if 
    else 
    {
        CalendarHourStr = Integer.toString(repTime.hour);
    }//else

    String CalendarMinStr = Integer.toString(repTime.min);
    if (repTime.min < 10) 
    {
        CalendarMinStr = "0" + CalendarMinStr;
    }//if

    if (repTime.hour < 12) 
    {
        strAmPm = "AM";
    }//if
    else 
    {
        strAmPm = "PM";
    }//else

    String alarmTime = CalendarHourStr + ":" + CalendarMinStr + " " + strAmPm;
    //alarmEnabled = true;
    Log.v("HCAlarmTimeDetail: ", alarmTime+" on day "+checkNo);
}//setDayAlarm

警报接收器

public class AlarmReceiver extends BroadcastReceiver
{
AlarmDBHelper alarmHelper;
List<AlarmTimeDAO> mAlarmList;
int mAlarmHour = 0, mAlarmMin = 0;
boolean alarmEnabled;

@Override
public void onReceive(Context context, Intent intent) 
{ 
    alarmHelper = new AlarmDBHelper(context);
    mAlarmList = alarmHelper.getAllAlarmTimeResults();
    alarmEnabled = intent.getBooleanExtra("ALARM_ENABLED", false);
    //snoozed = intent.getBooleanExtra("ALARM_SNOOZED", false);
    //Log.v("MultipleAlarmReceiver snoozed value", ""+snoozed);
    if((mAlarmList == null) || (mAlarmList.size() == 0))
    {
        Log.v("AlarmReceiver onReceiver", "no data found");
    }//if
    else
    {
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        String today = null;
        if (day == 2) 
        {
            today = "Monday";
        }//if
        else if (day == 3) 
        {
            today = "Tuesday";
        }//else if
        else if (day == 4) 
        {
            today = "Wednesday";
        }//else if
        else if (day == 5) 
        {
            today = "Thursday";
        }//else if
        else if (day == 6) 
        {
            today = "Friday";
        }//else if
        else if (day == 7) 
        {
            today = "Saturday";
        }//else if
        else if (day == 1) 
        {
            today = "Sunday";
        }//else if
        Log.v("Alarmreceiver", "Today is:- " + today);

        int system_hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        int system_minute = Calendar.getInstance().get(Calendar.MINUTE);
        Log.v("Current Time: ", system_hour+":"+system_minute);
        for(int i = 0; i < mAlarmList.size(); i++)
        {
            AlarmTimeDAO alarmDAO = mAlarmList.get(i);
            String delimiter = ":";
            String alarmTime = alarmDAO.getAlarmTime();
            String daysToRepeatAlarm = alarmDAO.getDaysToRepeatAlarm();
            String[] editTimeArray = alarmTime.split(delimiter);

            for(int j=0; j < editTimeArray.length; j++)
            {
                String timeData = editTimeArray[j];
                Log.v("AlarmReceiver alarm time", timeData);
            }//for

            SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
            Date date = null;
            try 
            {
                date = sdf.parse(alarmTime);
            }//try 
            catch (ParseException e) 
            {
                e.printStackTrace();
            }//catch

            final Calendar mCalendar = Calendar.getInstance();
            mCalendar.setTime(date);

            if(alarmTime.contains("AM"))
            {
                mAlarmHour = mCalendar.get(Calendar.HOUR);
                mAlarmMin = mCalendar.get(Calendar.MINUTE);
                Log.v("AlarmReceiver if AM", "time contains AM");
            }//if
            else if(alarmTime.contains("PM"))
            {
                mAlarmHour = mCalendar.get(Calendar.HOUR)+12;
                mAlarmMin = mCalendar.get(Calendar.MINUTE);
                Log.v("AlarmReceiver else PM", "time contains PM");
            }//else

            String daoAlarmTime = mAlarmHour+":"+mAlarmMin;
            Log.v("AlarmReceiver",daoAlarmTime);
            if(daoAlarmTime.equals(system_hour+":"+system_minute) 
                    && (daysToRepeatAlarm.equals("") || daysToRepeatAlarm.equals("null")))
            {
                Log.v("HCAlarmReceiver matched", "one day alarm");
                Intent in = new Intent(Intent.ACTION_MAIN);
                in.setClass(context, SnoozeActivity.class);
                int reqCode = intent.getIntExtra("REQUEST CODE", 0);
                Log.v("HCAlarmReceiver RequestCode", ""+reqCode);
                in.putExtra("REQUEST CODE", intent.getIntExtra("REQUEST CODE", 0));
                in.putExtra("ALARM_ENABLED", alarmEnabled);
                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                context.startActivity(in);
            }//if
            else if(daoAlarmTime.equals(system_hour+":"+system_minute) 
                    && daysToRepeatAlarm.equals("Daily"))
            {
                Log.v("HCAlarmReceiver matched", "daily alarm");
                Intent in = new Intent(Intent.ACTION_MAIN);
                in.setClass(context, SnoozeActivity.class);
                int reqCode = intent.getIntExtra("REQUEST CODE", 0);
                Log.v("HCAlarmReceiver RequestCode", ""+reqCode);
                in.putExtra("REQUEST CODE", intent.getIntExtra("REQUEST CODE", 0));
                in.putExtra("ALARM_ENABLED", alarmEnabled);
                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(in);
            }//if
            else if(daoAlarmTime.equals(system_hour+":"+system_minute)
                    && daysToRepeatAlarm.contains(today))
            {
                Log.v("HCAlarmReceiver matched", "weekday alarm");
                Intent in = new Intent(Intent.ACTION_MAIN);
                in.setClass(context, SnoozeActivity.class);
                Log.v("HCAlarmReceiver RequestCode", ""+intent.getIntExtra("REQUEST CODE", 0));
                in.putExtra("REQUEST CODE", intent.getIntExtra("REQUEST CODE", 0));
                in.putExtra("ALARM_ENABLED", alarmEnabled);
                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(in);
            }//else if
            else
            {
                Log.v("AlarmReceiver not matched", "no alarm matched");
            }//else
        }//for
    }//else
    if(SnoozeActivity.snoozed)
    {
        Log.v("HCAlarmReceiver matched", "snoozed alarm");
        Intent in = new Intent(Intent.ACTION_MAIN);
        in.setClass(context, SnoozeActivity.class);
        in.putExtra("ALARM_SNOOZED", SnoozeActivity.snoozed);
        in.putExtra("ALARM_ENABLED", alarmEnabled);
        Log.v("HCAlarmReceiver RequestCode", ""+intent.getIntExtra("REQUEST CODE", 0));
        in.putExtra("REQUEST CODE", intent.getIntExtra("REQUEST CODE", 0));
        in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(in);
    }//else
}//onReceive
}//AlarmReceiver

Android Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dzo.HanumanChalisaWithAudioAndAlarm"
android:versionCode="4"
android:versionName="1.3.0"
android:installLocation="auto" >

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-feature android:name="android.hardware.telephony" android:required="false"/>
<supports-screens 
    android:largeScreens="true"
    android:normalScreens="true" 
    android:smallScreens="true"
    android:anyDensity="true" />

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

    <activity 
        android:name=".SplashActivity"
        android:screenOrientation="portrait" 
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity 
        android:name=".StartManualActivity" 
        android:screenOrientation="portrait"/>
    <activity 
        android:name=".English" 
        android:screenOrientation="portrait"/>
    <activity 
        android:name=".About" 
        android:screenOrientation="portrait" />
    <activity 
        android:name=".HanuAlarm" 
        android:screenOrientation="portrait" />
    <activity 
        android:name=".SnoozeActivity" 
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Translucent"/>
    <activity 
        android:name=".MultipleAlarmActivity" 
        android:screenOrientation="portrait"/>
    <activity 
        android:name=".SetAlarmActivity"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
    <activity 
        android:name=".AlarmTimeDetail"
        android:screenOrientation="portrait"/>
    <receiver
        android:name=".receiver.AlarmReceiver"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="com.dzo.HanumanChalisaWithAudioAndAlarm.RECEIVEALARM"/>
        </intent-filter>
    </receiver>
    <receiver 
        android:name=".receiver.BootCompleteReceiver"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <service 
        android:enabled="true"
        android:name=".ChalisaService"></service>
</application>

1 个答案:

答案 0 :(得分:0)

您可能需要在重新启动时再次设置计时器...可能是您的虚拟应用程序,您没有使用任务杀手进行测试或重新启动...

注册重启事件并再次设置计时器。