Add this in your code
Intent serviceIntent = new Intent(this, ServiceName.class);
startService(serviceIntent);
Dont forget to add service tag in AndroidManifest.xml file
<service android:name="com.example.ServiceName"></service>
From the Android official documentation:
Caution: A service runs in the same process as the application in
which it is declared and in the main thread of that application, by
default. So, if your service performs intensive or blocking operations
while the user interacts with an activity from the same application,
the service will slow down activity performance. To avoid impacting
application performance, you should start a new thread inside the
service.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…