我创建了一个向用户阅读书籍的应用程序。
用户需要能够继续阅读与之前的应用会话中相同的位置。
如果可以随时发生进程终止,我该怎么做呢?
你可以像这样在你的应用程序中创建一个服务类,并在你的后端创建一个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));