0

I have an app (activity). I want it to stay running even if screen goes off, but older solution don't work. As i understand the only solution is to make a service. But is it easy to transform an activity to service?The method with wake lock can't be used any more, as it is deprecated.

Arnav M.
  • 2,590
  • 1
  • 27
  • 44
Nikos
  • 17
  • 1
  • 10
  • 2
    I'd use a [ForegroundService](https://developer.android.com/guide/components/services#Foreground) for maximum compatibility with the most recent Android versions. Also the end user might need to disable battery optimization for your application. _"But is easy to transform an activity to service?"_ That depends on your code in the Activity. Probably it isn't too hard. But it needs to done anyway. – Markus Kauppinen Jun 24 '19 at 11:23
  • Use JonScheduler or ForgroundService or JobIntentService – Mukesh Y. Jun 24 '19 at 11:30
  • 1
    Possible duplicate of [How to make an android app to always run in background?](https://stackoverflow.com/questions/34573109/how-to-make-an-android-app-to-always-run-in-background) – mbo Jun 24 '19 at 12:18

1 Answers1

1

Create Foreground Service as below.

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;

public class ForegroundService extends Service {
    public static final String CHANNEL_ID = "ForegroundServiceChannel";

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");
        createNotificationChannel();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Foreground Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_stat_name)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1001, notification);



        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

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

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }
}

Dont forget to declarer the service in manifest.

SimpleCoder
  • 1,665
  • 1
  • 21
  • 34