Search code examples
androidforeground-service

Android Foreground Services


I have a service which is called with

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    getActivity().startForegroundService(new Intent(getActivity(), 
Background.class));
} else {
    getActivity().startService(new Intent(getActivity(), Background.class));
}

and the service it's self being

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this,"Creating Notification",Toast.LENGTH_SHORT).show();

    //
    initChannels(this);

    Intent notificationIntent = new Intent(this, MainActivity.class);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    Notification notification = new NotificationCompat.Builder(this, "default")
            .setContentTitle("Zeep!?")
            .setTicker("Zeep!?")
            .setContentText("We're currently working in the background")
            .setSmallIcon(R.mipmap.zeep_icon_b)
            .setOngoing(true)
            .setPriority(Notification.PRIORITY_MIN)
            .setContentIntent(pendingIntent)
            .build();

    startForeground(1337, notification);
    //

    return START_NOT_STICKY;
}

but whenever I start the app and the close the app, it crashes and causes the phone to soft reboot, i'm so confused by it all, Thanks


Solution

  • My onStartCommand() looks like this:

         @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Tells the system to not try to recreate the service after it has been killed.
        return START_NOT_STICKY;
    }
    

    I take care of the notification stuff in onCreate() instead. Also, you need to call startForeground() immediately after calling startForegroundService():

         @Override
    public void onCreate() {
        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
        // Android O requires a Notification Channel.
        if (Build.VERSION.SDK_INT >= 26) {
            CharSequence name = getString(R.string.app_name);
            // Create the channel for the notification
            @SuppressLint("WrongConstant")
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_LOW);
            // Set the Notification Channel for the Notification Manager.
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(mChannel);
            }
    
            //Since MainActivity binds with the service and calls onCreate, we can actually call startForegroundService from within the service itself.
            startForegroundService(new Intent(ForegroundService.this, ForegroundService.class));
            //We only need to call this for SDK 26+, since startForeground always has to be called after startForegroundService.
            startForeground(NOTIFICATION_ID, getNotification());
        }
        else {
            //Since MainActivity binds with the service and calls onCreate, we can actually call startService from within the service itself.
            startService(new Intent(ForegroundService.this, ForegroundService.class));
        }
    

    Not saying this is the solution, but it works for me.