PeriodicWorkRequest 失败 redmi,vivo,oppo
Posted
技术标签:
【中文标题】PeriodicWorkRequest 失败 redmi,vivo,oppo【英文标题】:PeriodicWorkRequest Failed redmi,vivo,oppo 【发布时间】:2022-01-19 08:31:50 【问题描述】:在设置 PeriodicWorkRequest 时,通知不会每隔 15 分钟发送一次。通知会在 15 分钟,有时在 16 分钟,有时在 20 分钟,有时在 30 分钟,或者有时不显示通知。 我想在选定的时间段(例如上午 8 点到晚上 8 点)收到通知时每 15 次播放一次声音。 我已实施 PeriodicWorkRequest 但仍有一些设备未按时发出通知。
SchedulerActivity
public class SchedulerActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scheduler);
schedulePeriodicWork();
void schedulePeriodicWork()
Constraints constraints= new Constraints.Builder().setRequiresBatteryNotLow(false).build();
PeriodicWorkRequest unused = this.periodicWorkRequest = (PeriodicWorkRequest) newPeriodicWorkRequest.Builde((Class<? extends ListenableWorker>) MyWorker.class, 15, TimeUnit.MINUTE).setConstraints(constraints).build();
WorkManager.getInstance().enqueueUniquePeriodicWork("My Work", ExistingPeriodicWorkPolicy.REPLACE, this.periodicWorkRequest);
工人阶级
public class MyWorker extends Worker
public static MediaPlayer mediaPlayer;
Context con;
public MyWorker(@NonNull Context context, @NonNull WorkerParameters workerParams)
super(context, workerParams);
this.con=context;
@RequiresApi(api = Build.VERSION_CODES.Q)
@NonNull
@Override
public Result doWork()
int startHour=AlramUtility.getFromTimeHours(getApplicationContext()); // 8
int startMinute=AlramUtility.getFromTimeMinute(getApplicationContext()); //00
int endHour=AlramUtility.getToTimeHours(getApplicationContext()); //20
int endMinute=AlramUtility.getToTimeMinute(getApplicationContext()); //00
Calendar calendar= Calendar.getInstance();
Calendar startCalendar= Calendar.getInstance();
startCalendar.set(Calendar.HOUR_OF_DAY,startHour);
startCalendar.set(Calendar.MINUTE,startMinute);
Calendar endCalendar= Calendar.getInstance();
endCalendar.set(Calendar.HOUR_OF_DAY,endHour);
endCalendar.set(Calendar.MINUTE,endMinute);
if (calendar.after(startCalendar) && calendar.before(endCalendar) calendar.equals(startCalendar) && calendar.before(endCalendar)
calendar.after(startCalendar) && calendar.equals(endCalendar))
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 1, new Intent(getApplicationContext(), MainActivity.class).
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
PendingIntent.FLAG_CANCEL_CURRENT);
NewMessageNotification.notify(getApplicationContext(), "Hello", "Hello-", 1, contentIntent);
playAudio();
return Result.success();
private void playAudio()
try
this.mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.notificationtone);
if (!this.mediaPlayer.isPlaying())
this.mediaPlayer.start();
this.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
public void onCompletion(MediaPlayer mp)
MyWorker.this.mediaPlayer.reset();
MyWorker.this.mediaPlayer.release();
MyWorker.this.mediaPlayer = null;
);
catch (Exception e)
e.printStackTrace();
【问题讨论】:
您是否尝试过使用处理程序的 IntentService... 查看来自dontkillmyapp.com的指令 【参考方案1】:由于这些制造商强加的电池优化,它不起作用。
每次用户离开您的应用程序或应用程序进入后台时,手机都会终止与您的应用程序相关的所有服务。
继续寻找它是没有意义的,这是一个已知问题,希望 Google 能够向他们施加压力以恢复良好的用户体验。
【讨论】:
以上是关于PeriodicWorkRequest 失败 redmi,vivo,oppo的主要内容,如果未能解决你的问题,请参考以下文章
在android oreo中设备重启后PeriodicWorkRequest不起作用
如何使用Workmanager / PeriodicWorkRequest保持时间间隔之间的数据
NSUrlSession 代理请求失败,错误代码为 310 (kCFErrorHTTPSProxyConnectionFailure)