WorkManager 仅在打开应用程序时执行定期排队的任务。

3
最初的回答: 我的应用程序需要每天发送两次通知。为测试目的,我已经将此时间缩短到1小时。当然,这必须在后台/关闭应用程序时执行,因此我已经尝试了AlarmManager,但没有成功。因此,我转而使用WorkManager。有人建议我使用periodicWork来完成我的任务,但问题在于: 只有当应用程序打开时,WorkManager才会执行所有定期工作。 另一个奇怪的事情是:如果我让应用程序保持3个小时不动,当我打开应用程序时,我将收到远远超过三个通知。
我知道WorkManager没有执行,因为每当调用doWork()时,我都会实例化一个Date对象,并且该对象的时间戳将被打印到通知中。这个打印的时间将始终显示为我打开应用程序的时间,这意味着当我打开应用程序时,所有排队的工作请求都被一次性执行了。
下面是设置闹钟所使用的内容。请注意,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变量来检查worker触发的次数,并添加了一个Date对象来检查触发的时间。这些信息将在通知中显示。翻译成中文为:这是实际的 ReminderWorker 类,我添加了一个 SharedPreference 变量来检查 worker 触发的次数,并添加了一个 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文件。
原始答案:Original Answer
<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>

没有抛出任何错误,只是没有得到预期的结果。我需要每小时左右显示一个通知,但这似乎并没有发生。有没有什么方法可以解决这个问题?

最初的回答:

1个回答

0
你从任务管理器中移除了应用程序吗?你在使用哪个设备进行测试?有一些设备会强制关闭应用程序,当你再次打开应用程序时,WorkManager任务将被重新安排。 这个答案可能会帮助你理解发生了什么 - https://dev59.com/zVUL5IYBdhLWcg3wM1vy#52605503

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接