我正在开发聊天应用程序,为了实时获取新消息,我们使用了前台服务。 (由于某些情况,我们不能使用FCM)为了确保客户端已连接到服务器,我们使用JobScheduler每1分钟将ping发送到服务器。现在我们有电池使用问题。最好在我们的前台服务中像下面的波纹管代码那样使用CountDownTimer:
CountDownTimer countDownTimerPingPeriodic;
public static boolean isPinging;
public void pingPeriodic(boolean fromService) {
if (countDownTimerPingPeriodic != null) {
countDownTimerPingPeriodic.cancel();
countDownTimerPingPeriodic = null;
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
long future = 75000;
countDownTimerPingPeriodic =
new CountDownTimer(60000, 60000) {
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
sendPing(false);
pingPeriodic(false);
}
};
isPinging = true;
countDownTimerPingPeriodic.start();
}
});
}
或者最好使用像波纹管这样的作业服务(现在我们使用波纹管代码并在onStartJob中发送ping):
public class ScheduleConnectionJob extends JobService {
private static final String TAG = "ScheduleConnectionJob";
private int i = 0;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
@Override
public boolean onStartJob(JobParameters params) {
//here I will send a ping to the server
jobFinished(params, true);
Util.scheduleJob(getApplicationContext()); // reschedule the job
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
Util.scheduleJob(getApplicationContext());
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
Util.scheduleJob(getApplicationContext());
}}
并拨打并重复此服务,我们使用下面的代码每1分钟重复一次:
public class Util {
public static final long MinimumSchadulePeriodic = 15 * 60 * 1000 ;
// schedule the start of the service every 10 - 30 seconds
public static void scheduleJob(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ComponentName serviceComponent = new ComponentName(context, ScheduleConnectionJob.class);
JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent);
FileLog.i("Util:",
Thread.currentThread().getStackTrace()[2].getLineNumber() + " " +
"scheduleJob:scheduleJob");
builder.setMinimumLatency(MinimumSchadulePeriodic); // wait at least
builder.setOverrideDeadline(60 * 1000); // maximum delay
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); // require unmetered network
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
if (jobScheduler != null) {
jobScheduler.schedule(builder.build());
}
}
}}
如果您不支持SDK 14以下,则可以使用workmanager。否则,请参见此guide以了解所有选项。
有关电池管理的一些额外资源:Doze and standby,power management restrictions,Analyzing power usage,Excessive wake-ups,Excessive network usage in background
希望这对您有所帮助。我不得不说,让您的应用每分钟对后端执行一次ping操作似乎有点麻烦。除非至关重要的是您的用户在收到消息后即会立即收到消息,否则最好将其至少减少到后台5或10分钟。
如果您的情况更好,也可以使用WorkManager代替JobScheduler。
通过WorkManager API,即使应用程序退出或设备重新启动,也可以轻松地调度预期将要运行的可延迟异步任务。
查看此官方文档以获取更多信息::https://developer.android.com/topic/libraries/architecture/workmanager/
也请阅读本文https://medium.com/androiddevelopers/introducing-workmanager-2083bcfc4712