Permalink
Browse files

[Mobile] Fixed crash on boot. This is related to the Widget fix below.

[Widget] Fixed crash of Widget on boot due to restricted Android 8.0+ background services. Widget now runs its task as a foreground Service.
  • Loading branch information...
thecosmicfrog committed Jun 18, 2018
1 parent 32e14f5 commit 9e7c84adccc220146e52828e4df8e64e1db55201
@@ -26,6 +26,7 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import org.thecosmicfrog.luasataglance.service.WidgetListenerService;
@@ -47,8 +48,12 @@ public void onReceive(Context context, Intent intent) {
Intent intentStartService = new Intent(context, WidgetListenerService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
context.startService(intentStartService);
Log.i(LOG_TAG, "Device booted. Attempting to start WidgetListenerService.");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intentStartService);
} else {
context.startService(intentStartService);
}
}
}
@@ -21,14 +21,18 @@
package org.thecosmicfrog.luasataglance.service;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;
@@ -102,8 +106,17 @@
public WidgetListenerService() {
}
@Override
public void onCreate() {
super.onCreate();
startForeground(1, buildForegroundNotification().build());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(1, buildForegroundNotification().build());
final int STOP_FORECAST_TIMEOUT_MILLIS = 20000;
if (isNetworkAvailable(getApplicationContext())) {
@@ -657,4 +670,40 @@ private void updateStopForecast(Context context, RemoteViews views, StopForecast
);
}
}
/**
* Build a Notification to use when running WidgetListenerService in the foreground.
* @return Notification with details on what is being executed.
*/
private NotificationCompat.Builder buildForegroundNotification() {
/*
* Create a NotificationManager.
*/
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/* Android Oreo and above require a NotificationChannel to be created. */
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel =
new NotificationChannel(
"widgetGetStopForecast",
"Widget get stop forecast",
NotificationManager.IMPORTANCE_HIGH
);
/* Configure notification channel. */
notificationChannel.setDescription("Widget get stop forecast");
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(getApplicationContext(), "widgetGetStopForecast")
.setOngoing(true)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.widget_retrieving_stop_forecast))
.setSmallIcon(R.drawable.laag_logo_notification);
return notificationBuilder;
}
}
@@ -27,6 +27,7 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.util.Log;
@@ -218,7 +219,11 @@ private static void startWidgetListenerService(@NonNull Context context, int[] a
);
/* Start the WidgetListenerService. */
context.startService(intentWidgetListenerService);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intentWidgetListenerService);
} else {
context.startService(intentWidgetListenerService);
}
}
@@ -182,6 +182,7 @@
-->
<string name="widget_left_arrow">\u25C0</string>
<string name="widget_right_arrow">\u25B6</string>
<string name="widget_retrieving_stop_forecast">Retrieving stop forecast for widget.</string>
<!--
Stop arrays

0 comments on commit 9e7c84a

Please sign in to comment.