每日通知随Alarmanager随机提供



目前我试图在我的应用程序中设置每日通知,一切正常,但每日通知不起作用——它是随机产生的。

MainActivity我如何设置Alarmanager:

Intent i = new Intent(this, NotificationDbReceiver.class);
    Intent start = new Intent(this, StartService.class);
    PendingIntent sender = PendingIntent.getBroadcast(this, 0, i, 0);
    Calendar calendar = Calendar.getInstance();
            Calendar now = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 10);
            calendar.set(Calendar.MINUTE, 00);
            calendar.set(Calendar.SECOND, 00);
            if (calendar.before(now)) {
                calendar.add(Calendar.DATE, 1);
            }
            AlarmManager am = (AlarmManager) this
                    .getSystemService(ALARM_SERVICE);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                am.set(AlarmManager.RTC_WAKEUP,
                        AlarmManager.INTERVAL_DAY, sender);
            } else if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                am.setExact(AlarmManager.RTC_WAKEUP,
                        AlarmManager.INTERVAL_DAY, sender);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
                        AlarmManager.INTERVAL_DAY, sender);
            }

那是我的广播接收器:

@Override
public void onReceive(Context context, Intent intent) {
    Intent service1 = new Intent(context, StartService.class);
    context.startService(service1);
}

这是我的服务:

public class StartService extends Service {
private final int ID = 0;
private Context mContext;

@Override
public void onCreate() {
    super.onCreate();
    mContext = getBaseContext();
    Log.i("onCreate", "I'm in");
    createNotification();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
private void createNotification() {
    Log.i("createNotification", "I'm in");
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
    mBuilder.setSmallIcon(R.drawable.ic_stat_notification)
            .setColor(ContextCompat.getColor(mContext, R.color.primary))
            .setContentTitle("Blablabla")
            .setContentText("Blablabla")
            .setStyle(new NotificationCompat.BigTextStyle().bigText("Blablabla"))
            .setSound(soundUri)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setPriority(Notification.PRIORITY_HIGH)
            .setAutoCancel(true);
    Intent mIntent = new Intent(mContext, MainActivity.class);
    PendingIntent mPendingIntent = PendingIntent.getActivity(mContext, 0, mIntent, 0);
    mBuilder.setContentIntent(mPendingIntent);
    Notification mNotification = mBuilder.build();
    mNotification.flags = Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
    NotificationManagerCompat.from(mContext).notify(ID, mNotification);
}  
}

我希望有人能帮助我。我已经在stackoverflow上读到了一些问题,但这些问题的答案对我没有帮助。

可能是您多次初始化AlarmManager(例如,每当Activity启动时)。尝试只设置一次并进行检查。

最新更新