I'm having a nightmare trying to create a simple background service on android that runs permanently.
This service will be doing some background tasks like pool users social media and show notification so it requires only one user interaction (login) after that should be able to run forever until the end of the days....
but it is not happening
this is my manifest:
<service
android:name=".service.MyService"
android:description="@string/serviceDescription"
android:directBootAware="false"
android:enabled="true"
android:exported="false"
android:icon="@drawable/ic_logo"
android:label="@string/serviceLabel"/>
I start service on Application onCreate()
if (MyService.getInstance() == null) {
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
service:
public void onCreate() {
super.onCreate();
this.workDone = 0;
this.workerMap = new HashMap<User.UserData, Worker>(3);
this.listeners = new HashMap<Worker, Worker.WorkerListener>(3);
this.usernames = new HashMap<APIFacade, List<String>>(3);
this.threadPool = Executors.newFixedThreadPool(3);
INSTANCE = this;
}
ABSOLUTELLY NOWHERE IN MY CODE I CALL STOPSERVICE NOR STOPSELF
so why i'm getting nullpointerexception in some random situations?
someone even told me to do the bellow code to force android to "restart" service
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
can someone please help me... i'm getting really stressed with this problem
my app has over 1000 daily users and around 10% of them "find a way" to make nullpointerexception when using the service
This example demonstrate about how to Create Background Service in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.
onDestroy : This method is invoked when the background service is destroyed. You can release related resources in it such as close database connection, write data to file, etc. stopSelf : If you want to stop and exit the running service in your source code, you can call this method. 3. How To Start Stop Android Background Service In Activity.
# Create an intent object, pass the activity instance and the service class to the constructure. # Call startService method to start the background service. # Create an intent object, pass the activity instance and the service class to the constructure.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 – Right click on the project, Select New >> Service >> Service and add the following to MyServices.java
A lot of the internals of background services has changed in Oreo (or was it Nougat?). I'd get familiar with the Android documentation:
https://developer.android.com/about/versions/oreo/background
and
https://developer.android.com/training/best-background
You might want to consider the JobScheduler
framework.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With