Start service properly

This commit is contained in:
NekoInverter committed 2020-12-28 14:22:08 +08:00
1 parent 3c284be1e8
commit 459902aedf
4 files changed
+21 -23

No files matched your search

@@ -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();
@@ -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()) {
@@ -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);
+9 -1
View File
@@ -18,6 +18,14 @@
<external-cache-path
name="temp"
path="/" />
path="." />
<!--
java.lang.IllegalArgumentException:
Failed to find configured root that contains /storage/6230-3338/Android/data/io.github.nekoinverter.ehviewer/cache/1274942-c154d4fd8d-00000023.jpg
App install in sdcard ...?
-->
<root-path
name="root_path"
path="." />
</path>