我应该如何使用 Alarmmanager 每天和中午 12 点运行服务
我试图每天晚上 7 点打开一项新服务,该服务会在启动时通知我,但我无法解决这个问题,也不明白为什么。任何人都可以帮助我,谢谢 这是代码
这是我在 DaysCounterActivity 类中编写的代码
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 05);
calendar.set(Calendar.SECOND, 00);
Intent intent = new Intent(this, MyReciever.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pintent);
这是 MyReviever 类 onRevcieve 方法
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.saaram.MyService");
context.startService(serviceIntent);
}
I am trying to open a new service daily at 7 pm that would notify me on startup but i cant resolve this problem and cant understand why. can anyone help me out thankx Here is the code
Here is the code i wrote in the DaysCounterActivity class
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 05);
calendar.set(Calendar.SECOND, 00);
Intent intent = new Intent(this, MyReciever.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pintent);
and here is MyReviever class onRevcieve method
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.saaram.MyService");
context.startService(serviceIntent);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是
pintent
正在尝试运行服务,但MyReceiver
是广播接收器。如果您将MyReceiver
更改为服务,它就会起作用。在你的清单中你只需这样声明:
Your problem is that
pintent
is trying to run a Service, butMyReceiver
is a Broadcast Receiver. It would work if you changedMyReceiver
to a Service.In your manifest you would just declare it like so: