当应用程序未运行时,如何从后台启动 AlarmActivity?
我正在开发一个应用程序的闹钟组件;我使用 AlarmManager
安排闹钟,并使用静态 BroadcastReceiver
处理触发器,该静态 BroadcastReceiver
运行一个服务,该服务应该通过唤醒来启动 AlarmActivity
设备并显示动画屏幕,通知用户有警报(除了通知之外)。麻烦在于从 AlarmService
启动 AlarmActivity
;我不断收到导致应用程序崩溃的错误
E/.thengnet.medi: [qarth_debug:] get PatchStore::createDisableExceptionQarthFile method fail.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.blogspot.thengnet.medic, PID: 12588
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{9c39969 u0 com.blogspot.thengnet.medic/.services.AlarmService}
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2126)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
,并且丢失了所有计划的警报(直到我重新打开应用程序,因此它可能重复循环),之后通知它构建的通知。
我需要安排可以打开 AlarmActivity
的警报,无论应用程序是打开还是关闭 - 在 bg/fg 中 - 主动使用或重新启动后。
这是我的代码(最后有错误的部分):
private void startAlarmService(Context context, Intent intent) {
Intent alarmIntentService = new Intent(context, AlarmService.class);
// alarmIntentService.putExtra(label, intent.getStringExtra(label));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(alarmIntentService);
} else {
context.startService(alarmIntentService);
}
}
public AlarmService () {
super(AlarmService.class.getName());
}
@Override
protected void onHandleIntent (@Nullable Intent intent) {
NotificationUtil.notifyUserToTakeMeds(getApplicationContext());
Intent intent1 = new Intent(getBaseContext(), AlarmActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent1);
}
}
I'm working on the alarm component of an app; I schedule an alarm using AlarmManager
, and handle the trigger using a static BroadcastReceiver
, which runs a service that is supposed to start the AlarmActivity
by waking device and displaying animated screen notifying the user of an alarm (besides a notification). The hassle is in starting AlarmActivity
from AlarmService
; I keep getting the error
E/.thengnet.medi: [qarth_debug:] get PatchStore::createDisableExceptionQarthFile method fail.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.blogspot.thengnet.medic, PID: 12588
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{9c39969 u0 com.blogspot.thengnet.medic/.services.AlarmService}
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2126)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
which crashes the app, and I lose all scheduled alarms, (until I reopen the app, whence it probably repeats the cycle), after notifying the notification it builds.
I need to schedule alarms that can open AlarmActivity
whether app is open or closed -- in bg/fg -- actively used, or after a reboot.
Here's my code (buggy part at the end):
private void startAlarmService(Context context, Intent intent) {
Intent alarmIntentService = new Intent(context, AlarmService.class);
// alarmIntentService.putExtra(label, intent.getStringExtra(label));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(alarmIntentService);
} else {
context.startService(alarmIntentService);
}
}
public AlarmService () {
super(AlarmService.class.getName());
}
@Override
protected void onHandleIntent (@Nullable Intent intent) {
NotificationUtil.notifyUserToTakeMeds(getApplicationContext());
Intent intent1 = new Intent(getBaseContext(), AlarmActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent1);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此崩溃的原因就在异常文本中:
您的
Service
必须通过调用startForeground()
将自身声明为前台服务代码>handleMessage()方法。你需要添加这一点。The reason for this crash is right in the exception text:
Your
Service
must declare itself as a foreground service by callingstartForeground()
in thehandleMessage()
method. You need to add that.