【问题标题】:Save last reading position保存上次阅读位置
【发布时间】:2019-04-18 19:32:38
【问题描述】:

我创建了一个向用户阅读书籍的应用。

用户需要能够继续阅读 与他在上一次应用会话中的位置相同。

如果随时可能发生进程终止,我该怎么做?

【问题讨论】:

标签: android lifecycle


【解决方案1】:

您可以像这样在您的应用程序中创建一个服务类,并使用您的后端创建一个 api:-

服务类 OnClearFromRecentService.class:-

public class OnClearFromRecentService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_NOT_STICKY;

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {

        callApi();
        stopSelf();
    }
    public void callApi(){

     //set your api here
    }
}

并在您的清单中设置此服务类:-

<service
            android:name=".halper.OnClearFromRecentService"
            android:stopWithTask="false" />

并在您的视频播放时启动此服务:-

OnClearFromRecentService onClearFromRecentService = new OnClearFromRecentService();

或在您需要停止服务的地方停止服务,如下所示:-

onClearFromRecentService.onTaskRemoved(new Intent(LandingScreen.this, OnClearFromRecentService.class));

【讨论】:

  • 如果您有任何疑问,请发表评论,如果解决了您的问题,请感谢答案
  • 谢谢哥们!试过这段代码真的很有帮助......@Sandeep Malik
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-04
  • 2015-12-18
  • 1970-01-01
  • 2011-10-07
相关资源
最近更新 更多