Home > OS >  Foreground service gets stopped after 1 minute (Android 12)
Foreground service gets stopped after 1 minute (Android 12)

Time:08-31

I'm trying to make a service that works 24/7 and it's been working until I updated to Android 12. I've checked the new foreground restrictions but still dont understand why it isn't working since I'm starting it from an activity. I'd apreciate if someone could help.

ACTIVITY

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class TestActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            startForegroundService(new Intent(TestActivity.this, TestService.class));
    }
}

SERVICE

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.media.MediaBrowserCompat;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.media.MediaBrowserServiceCompat;

import java.util.List;

public class TestService extends MediaBrowserServiceCompat {
    @Override
    public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) { return null; }

    @Override
    public void onl oadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) { result.sendResult(null); }

    @Override
    public int onStartCommand(Intent ii, int flags, int startId) {
        //CREATE NOTI CHANNEL
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    "test", "Test Notifications",
                    NotificationManager.IMPORTANCE_HIGH);
            ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
        }
        //CREATE NOTI
        Notification notification = new NotificationCompat.Builder(this, "test")
                .setSmallIcon(R.drawable.icon)
                .setContentTitle("title")
                .setContentText("test")
                .setChannelId("test")
                .setOngoing(true)
                .setSilent(true)
                .build();
        //START FOREGROUND
        startForeground(101, notification);
        return START_STICKY;
    }
}

Thanks in advanced.

UPDATE: I tried creating a new app with just these lines of code and it works fine so it must be something else making the service close after a minute.

CodePudding user response:

Even though you are correct in wanting a foreground Service here, it seems as if you want it to stay alive whenever you app in the background, or not running at all, as you don't intend your app to be running 24/7, do you?

Since it seems as if you're trying to use a MediaBrowserServiceCompat in your example, I would suggest you do so using a companion instance of a MediaSessionCompat in order to have the media session control the lifecycle of your Service on it's own.

Implementing a media Service is not the most straightforward task in Android; they're still trying to fix some of its prevailing issues with the latest release of Android 13. Therefore, I would also suggest you follow this official Android guide if you're attempting to create an audio app with a media foreground Service.

CodePudding user response:

Turns out samsung put my app on deep sleep which caused it to close itself if not used for a minute. Also thanks to the people who took their time to try and help.

  • Related