Skip to content

Commit 2da2974

Browse files
committedMar 6, 2020
make service always foreground on init
1 parent de31ecd commit 2da2974

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package heyalex.widgethelper
2+
3+
import android.R
4+
import android.annotation.TargetApi
5+
import android.app.Notification
6+
import android.app.NotificationChannel
7+
import android.app.NotificationManager
8+
import android.content.Context
9+
import android.os.Build
10+
11+
class NotificationDelegate {
12+
companion object {
13+
@JvmStatic
14+
@TargetApi(Build.VERSION_CODES.O)
15+
fun getNotification(context: Context): Notification {
16+
val chan = NotificationChannel(
17+
WidgetUpdater.NOTIFICATION_CHANNEL_ID,
18+
WidgetUpdater.channelName,
19+
NotificationManager.IMPORTANCE_NONE
20+
)
21+
val manager =
22+
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
23+
manager.createNotificationChannel(chan)
24+
val notificationBuilder =
25+
Notification.Builder(context, WidgetUpdater.NOTIFICATION_CHANNEL_ID)
26+
notificationBuilder.setOngoing(true)
27+
notificationBuilder.setContentTitle("Update widget")
28+
.setContentText("Wait for finish updating")
29+
.setSmallIcon(R.drawable.stat_notify_sync)
30+
return notificationBuilder.build()
31+
}
32+
}
33+
}

‎widget-helper/src/main/java/heyalex/widgethelper/WidgetUpdateService.java

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public class WidgetUpdateService extends IntentService {
3838

3939
public WidgetUpdateService() {
4040
super("WidgetUpdateService");
41+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
42+
startForeground(NOTIFICATION_ID, NotificationDelegate.getNotification(this));
43+
}
4144
setIntentRedelivery(true);
4245
}
4346

‎widget-helper/src/main/java/heyalex/widgethelper/WidgetUpdater.java

+5-14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import androidx.annotation.Nullable;
1212
import android.widget.RemoteViews;
1313

14+
import org.jetbrains.annotations.NotNull;
15+
1416

1517
/**
1618
* Interface for updating widget
@@ -22,12 +24,12 @@ public abstract class WidgetUpdater {
2224
/**
2325
* Channel Id of default {@link Notification}
2426
*/
25-
private static final String NOTIFICATION_CHANNEL_ID = "widget updates";
27+
static final String NOTIFICATION_CHANNEL_ID = "widget updates";
2628

2729
/**
2830
* Channel Name of default {@link Notification}
2931
*/
30-
private static final String channelName = "Update Widget";
32+
static final String channelName = "Update Widget";
3133

3234
/**
3335
* Update widget by passing {@link RemoteViews} and id into
@@ -53,17 +55,6 @@ public abstract class WidgetUpdater {
5355
*/
5456
@TargetApi(Build.VERSION_CODES.O)
5557
public Notification makeNotification(@NonNull Context context) {
56-
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
57-
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
58-
if (manager != null) {
59-
manager.createNotificationChannel(chan);
60-
}
61-
62-
Notification.Builder notificationBuilder = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID);
63-
notificationBuilder.setOngoing(true);
64-
notificationBuilder.setContentTitle("Update widget")
65-
.setContentText("Wait for finish updating")
66-
.setSmallIcon(android.R.drawable.stat_notify_sync);
67-
return notificationBuilder.build();
58+
return NotificationDelegate.getNotification(context);
6859
}
6960
}

0 commit comments

Comments
 (0)
Please sign in to comment.