【问题标题】:How to make the application run in the background, as soon as the device starts/restarts一旦设备启动/重启,如何使应用程序在后台运行
【发布时间】:2021-02-02 11:58:20
【问题描述】:

所以,我有一个闹钟应用程序,只要设备在闹钟时间之前一直在线,它就可以正常工作。

问题是,假设我的电池没电了,或者我只想重新启动设备...警报不会按时触发。

我尝试了类似的方法:

public class BootCompletedReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Utils.set_alarms(context);
        }
    }
}

在我的 BroadcastReceiver... 但似乎不起作用。哦,它也在 AndroidManifest 中注册了...:

    <receiver
        android:name=".receivers.AlarmReceiver" // The receiver which starts the alarm activity.
        android:enabled="true" />
    <receiver
        android:name=".receivers.BootCompletedReceiver" // The receiver which is supposed to set the alarms after the device is online again.
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    

但不起作用...我们将不胜感激。

【问题讨论】:

    标签: android broadcastreceiver alarmmanager


    【解决方案1】:

    您可能需要将接收启动完成的权限添加到您的清单中。见Manifest Permission: RECEIVE_BOOT_COMPLETED

    【讨论】:

    • 我是否需要添加一些内容来询问用户是否可以收到 RECEIVE_BOOT_COMPLETED 操作?
    • 好吧,这被认为是一个危险的安装权限(不是运行时),所以用户会在安装时被警告并且可以选择不安装,但在运行时该权限被认为是安装授予的(如果我记得正确)。
    【解决方案2】:

    permissionactions下方添加

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <receiver
        android:name=".receivers.BootCompletedReceiver" // The receiver which is supposed to set the alarms after the device is online again.
        android:enabled="true">
        <intent-filter>
             <!-- used for restart or reboot -->
             <action android:name="android.intent.action.QUICKBOOT_POWERON" />
             <!-- Used for cold boot -->
             <action android:name="android.intent.action.BOOT_COMPLETED" />//cold start
        </intent-filter>
    </receiver>
    

    【讨论】:

    • 是的,“”已经在那里了......我尝试了 QUICKBOOT_POWERON 的东西,但似乎没有用然而......还有其他建议吗?提前致谢。
    猜你喜欢
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 2020-12-04
    • 2017-03-07
    • 2019-05-19
    • 1970-01-01
    相关资源
    最近更新 更多