AlarmManager和Notification无法正常工作

时间:2013-11-09 13:45:40

标签: android alarmmanager android-notifications

我已经阅读了有关此主题的几个问题,但我仍然无法弄明白。

我需要每天同时显示通知。我正在使用AlarmManager。它看起来写得很好,但实际上我不会收到任何警报。 SyncAlarmReceiver从不调用onReceive()。

以下是设置闹钟的代码:

private static final int ORA_NOTIFICA = 14;   //Hour for the notification
private static final int MINUTO_NOTIFICA = 35;  //Minutes for the notification
private static final int ID_NOTIFICA_SYNC = 33;  //Notification's ID

public static void setSyncAlarm(Context ctx) {
    Calendar calendar = Calendar.getInstance();

    calendar.set(Calendar.HOUR_OF_DAY, ORA_NOTIFICA);
    calendar.set(Calendar.MINUTE, MINUTO_NOTIFICA);
    AlarmManager am = (AlarmManager) ctx
            .getSystemService(Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, getPendingIntentSync(ctx));
    Log.d("NOTIFICA SYNC", "Alarm set at " + ORA_NOTIFICA + ":"
            + MINUTO_NOTIFICA);
}


public static PendingIntent getPendingIntentSync(Context ctx) {
    return PendingIntent.getBroadcast(ctx, 0, new Intent(ctx,
            SyncAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
}

这是SyncAlarmReceiver:

public class SyncAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.d("NOTIFICA SYNC", "Alert Ricevuto");
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

    NotificationUtils.createNotifSync(context);

    wl.release();
}

}

最后是createNotifSync:

public static void creaNotificaSync(Context ctx) {
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            ctx).setSmallIcon(R.drawable.action_bar_icon)
            .setContentTitle(ctx.getString(R.string.sync_notif_titolo))
            .setContentText(ctx.getString(R.string.sync_notif_testo))
            .setAutoCancel(true);

    Intent resultIntent = new Intent(ctx, ActTestJson.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
    stackBuilder.addParentStack(ActTestJson.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) ctx
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(ID_NOTIFICA_SYNC, mBuilder.build());
}

此外,Manifest的片段:

 <receiver
        android:name="com.mypackage.SyncAlarmReceiver"
        android:enabled="true" >
    </receiver>
编辑:这就是我在dumbsys警报上使用findstr发现的内容

adb shell dumpsys alarm | findstr /C:com.mypackage

RTC_WAKEUP #33: Alarm{42bd9470 type 0 com.mypackage} operation=PendingIntent{429d3238: PendingIntentRecord42fe80a0 com.mypackage startService}}

ELAPSED #12: Alarm{42b4fcc8 type 3 com.mypackage} operation=PendingIntent{42914e20: PendingIntentRecord{42a2a198 com.mypackage broadcastIntent}}

com.contram.unicam.controllori +15ms running, 6 wakeups: +15ms 6 wakes 6 alarms: cmp={com.mypackage/com.mypackage.utils.SyncAlarmReceiver}

RTC_WAKEUP和ELAPSED在这里,因为我尝试使用这两种方法(并没有真正理解差异)。

编辑: 我试过阅读用于设置时间的日历,这是它的toString():

java.util.GregorianCalendar[time=1386194671008,areFieldsSet=true,lenient=true,
zone=Europe/Rome,firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2013,MONTH=11,
WEEK_OF_YEAR=49,WEEK_OF_MONTH=2,DAY_OF_MONTH=4,DAY_OF_YEAR=338,DAY_OF_WEEK=4,
DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=4,SECOND=31,MILLISECOND=8,
ZONE_OFFSET=3600000,DST_OFFSET=0]

使用在线毫秒转换器,时间参数转换为周四08 08 2409 06:45:10 GMT + 0200(欧洲夏令时间)。

我应该用什么来设定时间?

1 个答案:

答案 0 :(得分:0)

问题在于ELAPSED_TIME:

am.setRepeating(AlarmManager.ELAPSED_REALTIME, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, getPendingIntentSync(ctx));

更改为RTC,它有效。