使用 AlarmManager 和 BroadCast 接收器在特定时间不显示通知

Notification not showing up at specific time using AlarmManager and BroadCast receiver

这是我呼叫广播接收者的地方class

private void createAlarmNotification() {

    // TODO Auto-generated method stub
        Intent myIntent = new Intent(getApplication() , AlarmSet.class);     
       AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
       PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplication(), 0, myIntent, 0);
       Calendar calendar1 = Calendar.getInstance();

       Calendar calendar = Calendar.getInstance();

       calendar.set(2015, 3, 20, 23, 55, 00);
       Alarm a= new Alarm();
       a.SetAlarm(MainActivity.this, calendar.getTimeInMillis(), 60*1000);


}

这是我的广播接收器class

public class Alarm extends BroadcastReceiver {

private final long GMT6 = AlarmManager.INTERVAL_HOUR * 6;
private final long DAY = AlarmManager.INTERVAL_DAY;

private ActivityManager activityManager;
private Context context;


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

    this.context = context;
    PowerManager pm = (PowerManager) context
            .getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

    showNotification();

    wl.release();
}


public void SetAlarm(Context context, long userHour, long userMinute) {
    this.context = context;
    AlarmManager am = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, Alarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setExact(AlarmManager.RTC_WAKEUP, userHour, pi);

}



public void showNotification() {

    Uri soundUri = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pIntent = PendingIntent
            .getActivity(context, 0, intent, 0);

    Notification mNotification = new Notification.Builder(context)
            .setContentTitle("")
            .setContentText("")
            .setSmallIcon(R.drawable.ic_launcher).setContentIntent(pIntent)
            .setSound(soundUri)
            .addAction(R.drawable.ic_launcher, "View", pIntent)
            .addAction(0, "Remind", pIntent).build();

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    mNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, mNotification);
}


}

}

这是清单代码

<receiver android:name="com.example.notification.Alarm"
    android:enabled="true"
    android:exported="true" ></receiver>

但是广播接收器 class 不显示任何类型的通知。 获得解决问题的适当建议将很有帮助。

在本次通话中

calendar.set(2015, 3, 20, 23, 55, 00);

您将日历的月份设置为四月,而不是三月,这就是它未被触发的原因。如果你想将闹钟设置为60秒后,你可以这样写:

    Calendar calendar = Calendar.getInstance();
    a.SetAlarm(MainActivity.this, calendar.getTimeInMillis(), 60*1000);