【问题标题】:which way is better in android , JobScheduler or use timer?在 android , JobScheduler 或使用计时器中哪种方式更好?
【发布时间】:2019-10-03 08:47:59
【问题描述】:

我正在开发一个聊天应用程序,为了实时获取新消息,我们使用前台服务。 (由于某些情况我们不能使用 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());
        }
    }
}}

【问题讨论】:

    标签: android service battery


    【解决方案1】:

    如果您不支持低于 SDK 14 的版本,您可以使用 workmanager。否则,请参阅此 guide 以了解所有选项。

    一些关于电池管理的额外资源:Doze and standbypower management restrictionsAnalyzing power usageExcessive wake-upsExcessive network usage in background

    希望这对您有所帮助。我不得不说,让您的应用每分钟 ping 一次后端似乎有点多。除非您的用户在收到消息时立即收到消息至关重要,否则最好将其在后台至少缩短到 5 或 10 分钟。

    【讨论】:

      【解决方案2】:

      如果您的情况比 JobScheduler 更好,您也可以使用 WorkManager。

      WorkManager API 可以轻松安排可延迟的异步任务,即使应用退出或设备重新启动,这些任务也会运行。

      查看此官方文档以获取更多信息:: https://developer.android.com/topic/libraries/architecture/workmanager/

      也可以阅读这篇文章https://medium.com/androiddevelopers/introducing-workmanager-2083bcfc4712

      【讨论】:

        猜你喜欢
        • 2011-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多