Alarmmanager没有触发警报

时间:2015-11-29 18:47:51

标签: android alarmmanager android-alarms

我正在尝试创建每日闹钟以发送通知,但不知何故通知未来。 我已经阅读了很多关于它的内容,但解决方案都没有帮助。

CreateAlarm功能:

public static void createAlarm (Context ctx, String filename, boolean must_create_alarm)
    {
        // CreateAlarm is called from main activity onCreate method.
        // Hence, using SharedPreference to set alarm only once.
        // When need to explicitly set the alarm. Client pass
        // must_create_alarm boolean to true
        SharedPreferences pref = ctx.getSharedPreferences(filename, Context.MODE_PRIVATE);
        boolean create_alarm = pref.getBoolean("MY_ALARM", true);
        create_alarm |= must_create_alarm;

        if (create_alarm == false) {
            Log.w ("ALARM INFO","ALARM ALREADY CREATED");
                return;
        }

        Log.w ("ALARM INFO","CREATED FRESH ALARAM");

        pref.edit()
            .putBoolean("MY_ALARM", false).commit();

        // Set the alarm to start at exactly 11:00 a.m.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 21);


        AlarmManager alarmMgr = (AlarmManager) ctx.getSystemService(ctx.ALARM_SERVICE);

        Intent _alarmIntent = new Intent(ctx, AppBroadCastReceiver.class);
        _alarmIntent.setAction("android.intent.action.ALARM_KICK_OFF");

        PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 1, _alarmIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Log.w("myApp", "SETTING ALARM");
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent); // Setting notification every DAY

    }

AppBroadCastReceiver类:

public class AppBroadCastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Log.w("myApp", "AppBroadCastReceiver DEVICE REBOTTED");
            GeneralUtility.createAlarm(context, "designersarres.designersarees", true);
        }
        else if (intent.getAction().equals("android.intent.action.ALARM_KICK_OFF")) {
            // Alarm is fired. Send notification
            Log.w("myApp", "AppBroadCastReceiver ALARM");
            GetNotificationMsg msg = new GetNotificationMsg(context);
            msg.sendNotification();
        }
    }
}

的AndroidManifest.xml:

<!-- Register the Broadcast Receiver -->
        <receiver android:name="designersarres.designersarees.AppBroadCastReceiver"
                  android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.ALARM_KICK_OFF"/>
            </intent-filter>
        </receiver>

最近几天我被困在这里。

请让我知道我在这里缺少什么或在这里做错了什么。

如果我需要在此处发布更多代码,请与我们联系。

0 个答案:

没有答案