【问题标题】:How to run a service every day at noon, and on every boot如何在每天中午和每次启动时运行服务
【发布时间】:2011-10-21 06:17:17
【问题描述】:

在我的应用程序中,我有一个 SQLite 数据库,其中包含一个以毫秒为单位的日期行表。我希望每天IF从我的数据库中存储的最后一个日期值过去 30 天后显示一个通知。服务似乎是完成这项检查的好方法。

我遇到了 Commonsware 的 WakefulIntentService 并认为这可能是答案,但我真的不知道我应该如何实现它。在演示中,它会在启动完成后 5 分钟后启动服务,这很好,但我需要添加什么才能让它在每个中午启动。 (...但仅显示一个通知/天,而不是同时显示一个通知,如启动和定期每日检查)

我知道这可以使用 AlarmManager 解决,但真的不知道如何。所以,我需要的帮助是给我一些示例/关键点,以便在每次启动和/或每天不运行应用程序时启动服务。

谢谢

【问题讨论】:

    标签: android service broadcastreceiver


    【解决方案1】:

    Android 警报管理器就是您的答案。将它与广播接收器一起使用,该接收器还可以在电话唤醒时重置警报。

    现在有代码示例: 在方法中设置警报:

    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.setAction("packagename.ACTION");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
            AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarm.cancel(pendingIntent);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    

    您的间隔接收器:

    public class AlarmReceiver extends BroadcastReceiver {
    private final String SOMEACTION = "packagename.ACTION"; //packagename is com.whatever.www
    @Override
    public void onReceive(Context context, Intent intent) {
        Time now = new Time();
        now.setToNow();
        String time = FileHandler.timeFormat(now);
    
        String action = intent.getAction();
        if(SOMEACTION.equals(action)) {
            // here you call a service etc.
        }
    

    手机关机时重置闹钟的接收器。

    public class AlarmSetter extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // get preferences
            SharedPreferences preferences = context.getSharedPreferences("name_of_your_pref", 0);
            Map<String, ?> scheduleData = preferences.getAll();
    
            // set the schedule time
            if(scheduleData.containsKey("fromHour") && scheduleData.containsKey("toHour")) {
                int fromHour = (Integer) scheduleData.get("fromHour");
                int fromMinute = (Integer) scheduleData.get("fromMinute");
    
                int toHour = (Integer) scheduleData.get("toHour");
                int toMinute = (Integer) scheduleData.get("toMinute");
    
                //Do some action
            }
        }
    
    }
    

    manifest很重要,这个是在application下添加的:

            <receiver android:name="AlarmReceiver">
            <intent-filter>
                <action android:name="packagename.ACTION"/>
                <action android:name="packagename.ACTION2"/>
            </intent-filter>
        </receiver>
    
        <receiver android:name="AlarmSetter" >
            <intent-filter>
                <action
                    android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    

    此外,为了使其正常工作,您需要在清单中添加接收引导广播的权限,并使用以下行:

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    

    如果有任何错误请告诉我们,希望这能解决问题。

    编辑(添加警报器示例):

    public class AlarmSetter extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // Do your stuff
        }
    }
    

    【讨论】:

    • 对不起,我的问题措辞有点糟糕,但我已经知道警报管理器,但不完全知道如何使用它来解决问题......也不知道如何在唤醒时重置它。所以我认为我需要启动与 commonsware WakeFulIntentService 演示(github.com/commonsguy/cwac-wakeful)中相同的服务,但来自不同的接收器......?如果没有运行应用,如何每天和/或每次启动时运行服务。
    • 了解警报管理器非常简单。或谷歌它:) 如果你仍然卡住,今晚可以给一些代码
    • 已经卡了好几天了 :) 所以是的,代码示例将不胜感激。
    • 嗨,我不知道为什么这对我来说很难理解...但是在您的示例中,在 AlarmReceiver 类上,您正在检查 ACTION.equals(action)... 是否应该它是“if (action.equals(SOMACTION)”吗?如果是真的,我应该将我的服务称为“Intent i = new Intent(context, MyService.class); this.startService(i);”吗?...您在清单中声明的​​“sleepScheduler.ACTION2”是什么?另外,您为什么不调用 AlarmSetter 中的服务?
    • 更新了代码并修复了一些错误。抱歉,那里有点快。对于您的问题 1. 是的,1a。是的,你可以这样做,2. 抱歉是一个错误,3. 我的代码用于现在添加的不同的东西 // 在那里做一些动作。你可以在那里做任何你想做的事情。
    【解决方案2】:

    这个答案似乎很老了。 现在,我完全建议人们查看 Google 提供的 SyncAdapter 框架。 它是为这些东西定制的。 这是链接:https://developer.android.com/training/sync-adapters/index.html

    【讨论】:

    • 更多人应该意识到这一点。像我这样的。谢谢!
    【解决方案3】:

    在演示中,它会在启动完成后 5 分钟后启动服务,这很好,但我需要添加什么才能让它在每个中午启动。

    更改setRepeating() 调用的初始时间。该示例显示的是从现在开始的一分钟 - 您需要进行计算以确定下一个中午的时间。

    您可以在来自不同示例项目的this OnBootReceiver 中看到此类计算的示例。在这里,我将闹钟设置为每天在用户指定的时间响起。

    【讨论】:

      猜你喜欢
      • 2019-05-12
      • 2023-04-08
      • 1970-01-01
      • 2015-03-20
      • 2011-11-06
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      • 2015-01-09
      相关资源
      最近更新 更多