当应用程序未运行时,如何从后台启动 AlarmActivity?

发布于 2025-01-11 16:28:34 字数 2773 浏览 0 评论 0原文

我正在开发一个应用程序的闹钟组件;我使用 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 中 - 主动使用或重新启动后。

这是我的代码(最后有错误的部分):

AlarmBroadcastReceiver

    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);
        }
    }

AlarmService

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):

AlarmBroadcastReceiver

    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);
        }
    }

AlarmService

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我ぃ本無心為│何有愛 2025-01-18 16:28:34

此崩溃的原因就在异常文本中:

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)

您的 Service 必须通过调用 startForeground() 将自身声明为前台服务代码>handleMessage()方法。你需要添加这一点。

The reason for this crash is right in the exception text:

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)

Your Service must declare itself as a foreground service by calling startForeground() in the handleMessage() method. You need to add that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文