-1

I'm running a services page continuously where I don't need the use of activity or the app icon to be displayed in my device

Apart from the main activity I have a java class which extends services and I have commented 'setContentView' in the main class were not to display the layout but when I run the app ill get a blank page and yes my services also run

Main Activity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      //  setContentView(R.layout.activity_main);
        Intent serviceIntent = new Intent(this, MyServices.class);
        startService(serviceIntent);
    }
}

I expect my app not to be displayed or an icon to be displayed in my device but just to run my services.

Thanks in advance, please help me out with this.

Shalu T D
  • 3,921
  • 2
  • 26
  • 37
Vineela
  • 1
  • 1
  • 6

1 Answers1

2

If your app targets API level 26 or higher, the system imposes restrictions on running background services when the app itself isn't in the foreground. In most cases like this, your app should use a scheduled job instead.

Background Service Limitations: While an app is idle, there are limits to its use of background services. This does not apply to foreground services, which are more noticeable to the user.

Use Foreground service, but it will display notification bar, to make service alive.

Rishabh Jain
  • 145
  • 8
  • I'm just going to leave thess relevant links here as well, in case people want to do some further reading on background services in android : https://developer.android.com/guide/components/services , https://developer.android.com/topic/performance/background-optimization – Nikos Hidalgo Aug 22 '19 at 10:50
  • In case of using Foreground service my API level should be below 26 right? – Vineela Aug 22 '19 at 10:54