Android AlarmManager 和 BroadcastReceiver 运行在后台 Service

发布于 2024-12-01 19:25:38 字数 1266 浏览 1 评论 0原文

我目前在集成 AlarmManager 和 BroadcastReceiver 时遇到问题。

在我的应用程序中,我正在运行一个后台服务,无论该应用程序是否正在运行,该服务都会运行。我想我让这部分工作得很好。后台服务保留一个根据用户位置而变化的数组。但是,每天早上 8:00,我希望应用程序将数组变量重置为默认值。在网上查了很多资料后,似乎可以通过 AlarmManager (每上午 8:00 启动任务)并使用 BroadcastReceiver (接收警报并执行任务)。

所以基本上代码是这样的:

public class BackgroundService extends Service {

    private ArrayList thisArray;

    private BroadcastReceiver thisReceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent) {
            setArrayToDefault();
        }

    }

    @Override
    public void onCreate(){

        super.onCreate();
        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.MINUTE, 2); //example

        this.registerReceiver(thisReceiver, new IntentFilter("BackgroundService"));

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), ONE_MINUTE, "what to put here?"); //example, repeat every minute

    }

    private void setArrayToDefault(){
        //here, the array will be changed back to default values
    }
}

我的主要问题是如何设置 AlarmManager 在每次设置时调用 thisReceiver 。有什么想法吗?我的做法正确吗?

I'm currently having a problem integrating AlarmManager and BroadcastReceiver.

Im my app, I'm running a background service that runs regardless that app is running or not. I think I get this part working fine. The background service keeps an array that changes based on user's location. However, at 8:00am everyday, I want the app to reset the array variable to default. After much looking around online, it seems like the way to do this is via AlarmManager (to initiate the task every 8:00am) and using BroadcastReceiver (to receive the alarm, and perform the task).

So basically the code goes something like this:

public class BackgroundService extends Service {

    private ArrayList thisArray;

    private BroadcastReceiver thisReceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent) {
            setArrayToDefault();
        }

    }

    @Override
    public void onCreate(){

        super.onCreate();
        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.MINUTE, 2); //example

        this.registerReceiver(thisReceiver, new IntentFilter("BackgroundService"));

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), ONE_MINUTE, "what to put here?"); //example, repeat every minute

    }

    private void setArrayToDefault(){
        //here, the array will be changed back to default values
    }
}

My main issue is on how to set the AlarmManager to call thisReceiver everytime it's set. Any idea? Is my approach correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

黑凤梨 2024-12-08 19:25:38

我是我的应用程序,我正在运行一个后台服务,无论该应用程序是否正在运行,该服务都会运行。

请不要。这就是为什么用户使用任务杀手和“设置”中“管理服务”屏幕的“强制停止”来攻击我们。

我的主要问题是如何设置 AlarmManager 在每次设置时调用 thisReceiver。有什么想法吗?

您没有注册 BroadcastReceiver,因此 AlarmManager 将无法联系它。

请重新设计您的服务,使其不必一直运行。无论如何,Android 将会因为老化而终止你的服务,所以如果你想要一个可靠且稳定的应用程序,你无论如何都需要重新设计。

Im my app, I'm running a background service that runs regardless that app is running or not.

Please don't. This is why users attack us with task killers and the Force Stop from the Manage Services screen in Settings.

My main issue is on how to set the AlarmManager to call thisReceiver everytime it's set. Any idea?

You are not registering the BroadcastReceiver, so AlarmManager will not be able to contact it.

Please please please please please please please redesign your service such that it does not have to run all of the time. Android will be killing off your service due to old age, anyway, so if you want a reliable and stable app, you need to do this redesign, anyway.

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