繁体   English   中英

AlarmManager和通知不起作用

[英]AlarmManager and Notification not working

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

我需要每天同时显示一条通知。 我正在使用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());
}

另外,清单中的代码段:

 <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]

使用在线千禧时间转换器,时间参数转换为格林尼治标准时间+0200(W. Apr Day 2 2 08)(W. Europe Daylight Time)。

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

问题出在ELAPSED_TIME:

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

更改为RTC,它起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM