需要长时间运行的应用不会永远保持在前台运行,用户很可能在计时的同时需要微信聊天,或者刷视频。对于秒表应用来讲就是被切换到后台。这时我们希望:

  1. 无论秒表应用处于前台还是后台,计时动作都可以继续执行。

  2. 切换到后台时秒表音频暂停播放,切回前台时继续播放音频。

以下是演示视频:

46 前后台

处理前后台切换通知

通过重载MainAbilitySlice类的onForeground和onBackground方法可以接受应用的前、后台切换通知:

@Overridepublic void onForeground(Intent intent){    HiLog.info(LABEL, "MainAbilitySlice.onForeground");    super.onForeground(intent);    stopwatch.onForeground(intent);}
@Overridepublic void onBackground(){    HiLog.info(LABEL, "MainAbilitySlice.onBackground");    super.onBackground();    stopwatch.onBackground();}

AnalogStopWatch中处理前后台切换的代码如下:​​​​​​​

public void onForeground(Intent intent){    resumeSound();}
public void onBackground(){    pauseSound();}public void pauseSound(){    if(playStatus == PlayStatus.PLAYING){        soundPlayer.pause(taskId);        playStatus = PlayStatus.PSUSE;    }}
public void resumeSound(){    if(playStatus == PlayStatus.PSUSE){        soundPlayer.resume(taskId);        playStatus = PlayStatus.PLAYING;    }}

切换到后台时暂停播放;切换到前台时继续播放。

计时过程中的退出应用处理

如果计时过程中用户进行退出操作,我们希望应用不会应用真正退出,而是切换到后台继续计时。代码如下:​​​​​​​

@Overridepublic void onStop(){    HiLog.info(LABEL, "onStop");    saveStatus();    super.onStop();
}private void saveStatus(){    HiLog.info(LABEL, "MainAbilitySlice.saveStatus");    DatabaseHelper databaseHelper = new DatabaseHelper(this); // context入参类型为ohos.app.Context。    Preferences preferences = databaseHelper.getPreferences("StopWatch");    preferences.putBoolean("running", stopwatch.isRunning());    preferences.putLong("start_time", stopwatch.getStartTime());    preferences.putLong("milliseconds", stopwatch.getMilliseconds());    preferences.putString("lap_times", lap_time.getText());    preferences.putInt("record_count", record_count);}

这里没有使用后台Service技术,只是将必要的信息保存起来,当系统再次启动时恢复这些信息:​​​​​​​

@Overridepublic void onStart(Intent intent) {    //HiLog.warn(LABEL, "Failed to visit %{private}s, reason:%{public}d.", url, errno);    HiLog.info(LABEL, "onStart");    super.onStart(intent);    super.setUIContent(ResourceTable.Layout_ability_main);    //...    loadStatus();}private void loadStatus(){    HiLog.info(LABEL, "MainAbilitySlice.loadStatus");    DatabaseHelper databaseHelper = new DatabaseHelper(this); // context入参类型为ohos.app.Context。    Preferences preferences = databaseHelper.getPreferences("StopWatch");    stopwatch.setStartTime(preferences.getLong("start_time", 0));    stopwatch.setMilliseconds(preferences.getLong("milliseconds", 0));    lap_time.setText(preferences.getString("lap_times", ""));    record_count = preferences.getInt("record_count", 0);    if(preferences.getBoolean("running",false)){        stopwatch.start();    }    updateButton();}

参考代码

完整代码可以从以下链接下载:

https://github/xueweiguo/Harmony/tree/master/StopWatch

参考资料

https://developer.harmonyos/cn/docs/documentation/doc-guides/ability-page-lifecycle-0000000000029840

作者著作介绍

《实战Python设计模式》是作者去年3月份出版的技术书籍,该书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。


觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

更多推荐

自学鸿蒙应用开发(46)- 处理应用前后台切换