From 459902aedf22dd22d9120cdb751e7404fd632b30 Mon Sep 17 00:00:00 2001 From: NekoInverter <7741672-NekoInverter@users.noreply.gitlab.com> Date: Mon, 28 Dec 2020 14:22:08 +0800 Subject: [PATCH] Start service properly --- .../ehviewer/shortcuts/ShortcutsActivity.java | 3 ++- .../hippo/ehviewer/ui/CommonOperations.java | 22 +++++-------------- .../ehviewer/ui/scene/DownloadsScene.java | 9 ++++---- app/src/main/res/xml/filepaths.xml | 10 ++++++++- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/shortcuts/ShortcutsActivity.java b/app/src/main/java/com/hippo/ehviewer/shortcuts/ShortcutsActivity.java index f6937651f..8d845543a 100644 --- a/app/src/main/java/com/hippo/ehviewer/shortcuts/ShortcutsActivity.java +++ b/app/src/main/java/com/hippo/ehviewer/shortcuts/ShortcutsActivity.java @@ -20,6 +20,7 @@ import android.content.Intent; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; import com.hippo.ehviewer.download.DownloadService; @@ -37,7 +38,7 @@ public class ShortcutsActivity extends AppCompatActivity { action = intent.getAction(); if (action != null && (action.equals(DownloadService.ACTION_START_ALL) || action.equals(DownloadService.ACTION_STOP_ALL))) { - startService(new Intent(this, DownloadService.class).setAction(action)); + ContextCompat.startForegroundService(this, new Intent(this, DownloadService.class).setAction(action)); } } finish(); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java index 9a7c8659f..fa42be226 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java @@ -19,6 +19,8 @@ package com.hippo.ehviewer.ui; import android.app.Activity; import android.content.Intent; +import androidx.core.content.ContextCompat; + import com.hippo.app.ListCheckBoxDialogBuilder; import com.hippo.ehviewer.EhApplication; import com.hippo.ehviewer.EhDB; @@ -126,14 +128,7 @@ public final class CommonOperations { Intent intent = new Intent(activity, DownloadService.class); intent.setAction(DownloadService.ACTION_START_RANGE); intent.putExtra(DownloadService.KEY_GID_LIST, toStart); - try { - activity.startService(intent); - } catch (Exception e) { - // java.lang.IllegalStateException: - // Not allowed to start service Intent { act=start cmp=com.hippo.ehviewer/.download.DownloadService (has extras) }: app is in background uid null - e.printStackTrace(); - return; - } + ContextCompat.startForegroundService(activity, intent); } if (toAdd.isEmpty()) { @@ -161,14 +156,7 @@ public final class CommonOperations { intent.setAction(DownloadService.ACTION_START); intent.putExtra(DownloadService.KEY_LABEL, label); intent.putExtra(DownloadService.KEY_GALLERY_INFO, gi); - try { - activity.startService(intent); - } catch (Exception e) { - // java.lang.IllegalStateException: - // Not allowed to start service Intent { act=start cmp=com.hippo.ehviewer/.download.DownloadService (has extras) }: app is in background uid null - e.printStackTrace(); - return; - } + ContextCompat.startForegroundService(activity, intent); } // Notify activity.showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT); @@ -198,7 +186,7 @@ public final class CommonOperations { intent.setAction(DownloadService.ACTION_START); intent.putExtra(DownloadService.KEY_LABEL, label1); intent.putExtra(DownloadService.KEY_GALLERY_INFO, gi); - activity.startService(intent); + ContextCompat.startForegroundService(activity, intent); } // Save settings if (builder.isChecked()) { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java index f6a99f83e..406dae3d0 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java @@ -48,6 +48,7 @@ import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.PopupMenu; import androidx.appcompat.widget.Toolbar; +import androidx.core.content.ContextCompat; import androidx.core.view.ViewCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.recyclerview.widget.LinearLayoutManager; @@ -532,7 +533,7 @@ public class DownloadsScene extends ToolbarScene case R.id.action_start_all: { Intent intent = new Intent(activity, DownloadService.class); intent.setAction(DownloadService.ACTION_START_ALL); - activity.startService(intent); + ContextCompat.startForegroundService(activity, intent); return true; } case R.id.action_stop_all: { @@ -571,7 +572,7 @@ public class DownloadsScene extends ToolbarScene Intent intent = new Intent(activity, DownloadService.class); intent.setAction(DownloadService.ACTION_START_RANGE); intent.putExtra(DownloadService.KEY_GID_LIST, gidList); - activity.startService(intent); + ContextCompat.startForegroundService(activity, intent); return true; } } @@ -786,7 +787,7 @@ public class DownloadsScene extends ToolbarScene Intent intent = new Intent(activity, DownloadService.class); intent.setAction(DownloadService.ACTION_START_RANGE); intent.putExtra(DownloadService.KEY_GID_LIST, gidList); - activity.startService(intent); + ContextCompat.startForegroundService(activity, intent); // Cancel check mode recyclerView.outOfCustomChoiceMode(); break; @@ -1326,7 +1327,7 @@ public class DownloadsScene extends ToolbarScene Intent intent = new Intent(activity, DownloadService.class); intent.setAction(DownloadService.ACTION_START); intent.putExtra(DownloadService.KEY_GALLERY_INFO, list.get(index)); - activity.startService(intent); + ContextCompat.startForegroundService(activity, intent); } else if (stop == v) { if (null != mDownloadManager) { mDownloadManager.stopDownload(list.get(index).gid); diff --git a/app/src/main/res/xml/filepaths.xml b/app/src/main/res/xml/filepaths.xml index 3afbf3842..b7cf90b5c 100644 --- a/app/src/main/res/xml/filepaths.xml +++ b/app/src/main/res/xml/filepaths.xml @@ -18,6 +18,14 @@ + path="." /> + +