开发报警应用程序
我想开发一个警报应用程序。 该应用程序应该像这样工作:
- 启动它的
- 活动显示
- 我可以设置闹钟的
- 时间我可以在闹钟时间到来时关闭应用程序
- ,它启动一个活动(即使设备被锁定)
我已经尝试适应这个示例https://github.com/commonsguy/cwac-wakeful 但我无法启动活动闹钟时间到了。
我使用此代码来设置警报(为了测试,我已在活动的 onCreate
方法上插入了此代码):
Intent intent = new Intent(this, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, cal.getTimeInMillis(),
pendingIntent);
这是 OnAlarmReceiver 类:
public class OnAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(ClockActivity.LOG_TAG, "OnAlarmReceiver::onReceive");
WakefulIntentService.sendWakefulWork(context, AlarmService.class);
}
}
这是服务类:
public class AlarmService extends WakefulIntentService {
public AlarmService(String name) {
super(name);
}
@Override
protected void doWakefulWork(Intent intent) {
Log.i(ClockActivity.LOG_TAG, "AlarmService::doWakefulWork");
Intent newIntent = new Intent(getApplicationContext(), ClockActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.setAction(ClockActivity.ALARM_ACTION);
getApplicationContext().startActivity(newIntent);
}
}
这是 Manifest 的部分,其中设置服务和接收器:
<receiver android:name=".OnAlarmReceiver"></receiver>
<service android:name=".AlarmService"></service>
doWakefulWork 方法永远不会被调用!
I'd like develop an Alarm Application.
The application should work like this:
- launch it
- the activity show me the time
- I can set the alarm
- I can close the application
- when the alarm time comes , it starts an activity (even if the device is locked)
I have tried to adapt this sample https://github.com/commonsguy/cwac-wakeful but I cannot launch an activity when the alarm time comes.
I use this code to setup the alarm (for test I have inserted this code on an onCreate
method of activity):
Intent intent = new Intent(this, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, cal.getTimeInMillis(),
pendingIntent);
this is the OnAlarmReceiver class:
public class OnAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(ClockActivity.LOG_TAG, "OnAlarmReceiver::onReceive");
WakefulIntentService.sendWakefulWork(context, AlarmService.class);
}
}
this is the service class:
public class AlarmService extends WakefulIntentService {
public AlarmService(String name) {
super(name);
}
@Override
protected void doWakefulWork(Intent intent) {
Log.i(ClockActivity.LOG_TAG, "AlarmService::doWakefulWork");
Intent newIntent = new Intent(getApplicationContext(), ClockActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.setAction(ClockActivity.ALARM_ACTION);
getApplicationContext().startActivity(newIntent);
}
}
this is the portion of Manifest where are setup the service and the receiver:
<receiver android:name=".OnAlarmReceiver"></receiver>
<service android:name=".AlarmService"></service>
the doWakefulWork method is never called!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我制作了这个应用程序:
AlarmActivity.java
MyBroadcastReceiver.java
I have made this application:
AlarmActivity.java
MyBroadcastReceiver.java
检查一下:http://blog.nelsondev.net/?p=124
使用“ Alarmmanager”
check this out : http://blog.nelsondev.net/?p=124
using"alarmmanager"
我遇到了类似的问题,我解决了从清单中删除接收器并在我的代码中设置将其注册到 registerReceiver 的问题。
I had a similar problem and I resolved removing the receiver from manifest and set in my code registering him with registerReceiver.
您可能会遇到实例化异常吗?
您的服务中应该有一个公共的无参数构造函数:
Do you get an instantiation exception maybe ?
You should have a public no arg constructor in your service :