首页 > 解决方案 > WorkManager 仅在应用打开时执行定期排队的任务

问题描述

我的应用程序要求每天发送两次通知。出于测试目的,我将这个时间缩短到 1 小时。当然,这必须在后台/应用程序关闭时完成,所以我已经尝试过 AlarmManager,但没有奏效。因此,我切换到了 WorkManager。有人建议我使用periodicWork 来完成我的任务,但问题是: WorkManager 仅在应用程序打开时执行所有定期工作 另一个奇怪的事情:如果我将应用程序单独放置3 小时,我将收到超过三个通知我打开应用程序。

我知道 WorkManager 没有执行的事实,因为每当调用 doWork() 时我都实例化了一个日期对象,并且日期对象的时间戳被打印到通知中。此打印时间将始终显示为我打开应用程序的时间,这意味着所有排队的工作请求在我打开应用程序时立即执行。

这是用于设置警报的内容。请注意,cancelAlarm() 不会取消警报,而是重置我用于调试的共享首选项

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enter);
        ToggleButton toggle = findViewById(R.id.toggleButton);
        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    setAlarm();
                } else {
                    cancelAlarm();
                }
            }
        });
    }

    private void setAlarm() {
        Constraints constraints = Constraints.NONE;
        PeriodicWorkRequest testRequest = new PeriodicWorkRequest.Builder(ReminderWorker.class, 1, TimeUnit.HOURS)
                .setConstraints(constraints)
                .build();
        WorkManager.getInstance().enqueueUniquePeriodicWork("ReminderWork", ExistingPeriodicWorkPolicy.KEEP, testRequest);
    }

    private void cancelAlarm() {
        SharedPreferences savedSharedPreferences = getApplicationContext().getSharedPreferences("USER_PREFERENCES", Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = savedSharedPreferences.edit();
        editor.putInt("Test", 0);
        editor.commit();
    }

这是实际的 ReminderWorker 类,我放置了一个 SHaredPreference 变量来检查工作人员被解雇的次数,以及一个 Date 对象来检查被解雇的时间。这些都打印在通知中。

public class ReminderWorker extends Worker {
    int i;
    public final String CHANNEL_ID = "MainChannel";
    public ReminderWorker(@NonNull Context context, @NonNull WorkerParameters params) {
        super(context, params);
    }



    @NonNull
    @Override
    public Result doWork() {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        SharedPreferences savedSharedPreferences = getApplicationContext().getSharedPreferences("USER_PREFERENCES", Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = savedSharedPreferences.edit();
        i = savedSharedPreferences.getInt("Test", 0) + 1;
        editor.putInt("Test", i);
        editor.commit();
        createNotificationChannel();
        buildNotification(cal);
        return Result.success();
    }
    private void createNotificationChannel() {
        String name = "Birthday Notifications";
        String description = "Reminds you when your friends birthday approaches";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        NotificationManager notificationManager = getApplicationContext().getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
    private void buildNotification(Calendar cal) {
        Context context = getApplicationContext();
        Intent openTap = new Intent(context, EnterActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, openTap, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
        builder.setSmallIcon(R.drawable.pix_cake);
        builder.setContentTitle("TestNotification");
        builder.setStyle(new NotificationCompat.BigTextStyle()
                .bigText("TestText" + i + " Time: " + cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE)));
        builder.setPriority(NotificationCompat.PRIORITY_MAX);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(i, builder.build());
        //notificationManager.cancelAll();
    }
}

如果需要,这是我的 AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp">

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.SET_ALARM" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/main_icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/main_icon_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ListOfDaysActivity" />
        <activity android:name=".MainActivity" />
        <activity android:name=".EnterActivity"
            android:launchMode="singleInstance">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

没有抛出错误,只是不是预期的结果。我需要大约每小时显示一次通知,但这根本没有发生。有什么办法吗?

标签: javaandroid

解决方案


您是否从任务管理器中删除该应用程序?您使用哪种设备进行测试?有一些设备会强制关闭应用程序,并且一旦您再次打开应用程序,WorkManager 任务就会重新安排。这个答案可能会帮助您了解发生了什么 - https://stackoverflow.com/a/52605503/1313699


推荐阅读