0

I'm developing an application for Android. And I have some troubles with services in Android. I want to make service running in background and playing audio stream. But unfortunately service restarts when task swipes out from activity manager. It's very inconvenient for me, I want that service continue to work, without restarts.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rkoptev.backgroundservice">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".TweetViewActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".TweetCollectorService"
            android:process=":remote">
            <intent-filter>
                <action android:name="com.rkoptev.backgroundservice.TweetCollectorService"/>
            </intent-filter>
        </service>
    </application>

</manifest>

TweetCollectorService.java

public class TweetCollectorService extends Service {

    private static final String TAG = TweetCollectorService.class.getSimpleName();

    private Timer timer;

    private TimerTask updateTask = new TimerTask() {
        @Override
        public void run() {
            Log.i(TAG, "Timer task doing work");
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "Service creating");

        timer = new Timer("TweetCollectorTimer");
        timer.schedule(updateTask, 1000);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "Service destroying");

        timer.cancel();
        timer = null;
    }
}

TweetViewActivity.java

public class TweetViewActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(TweetCollectorService.class.getName()));
    }
}

Is there a way to prevent service restart? Thank you!

Ruslan
  • 122
  • 1
  • 14

2 Answers2

0

Try adding this to the bottom of your service's onCreate method:

Notification note = new Notification.Builder(this)
    .setContentTitle("Notification title")
    .setContentText("Notification message")
    .setSmallIcon(R.mipmap.ic_launcher)
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)).build();
startForeground(1, note);

This will tell the Android system that you don't want the service to be killed.

You can also try returning Service.START_STICKY from the service's onStartCommand() method, as described here: https://stackoverflow.com/a/24427569/2441655

Community
  • 1
  • 1
Venryx
  • 15,624
  • 10
  • 70
  • 96
  • startForeground will always shows the app on notification tray..! for me this is not preferred, any workaround to hide it? – GvSharma Jul 10 '18 at 05:07
  • There is no workaround that doesn't use an icon in the notification tray, as far as I know. You can use services, but there are some limitations to them if I remember correctly. (haven't done android dev for quite some time) – Venryx Jul 17 '18 at 04:21
-1

Swiping out is equivalent to causing Force Stop on the app. Since the service is part of the app only, it would also be stopped.

As an alternative, you can do:

noHistory="true"

in your <activity> tag

Ankur Aggarwal
  • 2,210
  • 2
  • 34
  • 39
  • Swiping out is not equivalent to calling Force Stop on the app. Services can continue to run even after swiping; I know because that's what my current app does. Not only is the process preserved, but the data (static variables, etc) are as well. Probably, something just is different with his setup that makes it not persist. (I wrote an answer on how to maybe solve it) – Venryx Mar 03 '17 at 23:44