其他分享
首页 > 其他分享> > android-使用警报管理器启动服务

android-使用警报管理器启动服务

作者:互联网

我有一些想要每天凌晨3:00执行的代码.我已经阅读了Service Class Documentation,看来我可以使用AlarmManager触发一个意图(我想是活动还是服务,我想?),然后在该意图中创建消息并将其发布到Android通知区域中.

Calendar threeAM = Calendar.getInstance();
threeAM.set(Calendar.HOUR_OF_DAY,2);
threeAM.set(Calendar.MINUTE,0);
threeAM.set(Calendar.SECOND,0);
threeAM.set(Calendar.MILLISECOND,0);
AlarmManager alarmManager =
         (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, myNotifier.class);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, threeAM.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, PendingIntent.getService(context, 1, i , 0));
Log.i("Service TEST", "Alarm set?" );

它可以毫无问题地运行代码,但是没有迹象表明已设置警报,并且活动也不会启动.我正在使用一种我知道有效的活动.我尝试将其包装在try / catch中,logcat中没有任何内容…

任何帮助,将不胜感激!!

标签:alarmmanager,android
来源: https://codeday.me/bug/20191208/2093395.html