Android: creating a service that starts when the application stops

How to create a service that continues to work when your application is stopped? I create the service in a separate process in the manifest file:

<service android:name="com.example.myservice" android:process=":remoteservice" /> 

I also return START_STICKY on the onStartCommand service:

 @Override public in onStartCommand(Intent intent, int flags, int startId){ return Service.START_STICKY; } 
+4
source share
2 answers

I would not use the android: process attribute - this actually starts your service in a separate process and makes it difficult to perform such actions. You do not have to worry about your service dying, when your application dies, the service will continue to work (this is a service point). You also do not want the binding service, because it will start and die when the bindings are performed. It is said:

 <service android:enabled="true" android:exported="true" android:name=".MyService" android:label="@string/my_service_label" android:description="@string/my_service_description" <intent-filter> <action android:name="com.package.name.START_SERVICE" /> </intent-filter> </service> 

You can define your own action for the intention to start the service (cannot be started by the system - there must be an event). I like to set “enabled” to true so that the system can create an instance of my service and “export”, so that other applications can send intentions and interact with my service. Read more about it here .

You can also create a long service that uses bindings, if you do, just add and configure for the binder, for example:

 <action android:name="com.package.name.IRemoteConnection" /> 

Now, to start the service from your activity:

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent serviceIntent = new Intent("com.package.name.START_SERVICE"); this.startService(serviceIntent); // You can finish your activity here or keep going... } } 

Now for the service:

 public class MyService extends Service { @Override public IBinder onBind(Intent intent) { // This is triggered by the binder action intent. // You can return your binder here if you want... return null; } @Override public void onCreate() { // Called when your service is first created. } @Override public in onStartCommand(Intent intent, int flags, int startId) { // This is triggered by the service action start intent return super.onStartCommand(intent, flags, startId); } } 

Your service should now work. To help with longevity, there is some kind of trick. Make it work as a front-end service (you do it in the service) - this will force you to create a notification icon, which is located in the status bar. Also keep the HEAP light on to make your app less likely to be killed by Android.

In addition, the service is killed when you kill DVM, so if you are going to Settings-> Applications and stop the application, you kill DVM and thus kill the service. Just because you see that your application is running in the settings does not mean that the activity is running. Activities and services have different life cycles, but can share DVM. Just keep in mind that you don’t need to kill your activity, if you don’t need to, just let Android handle it.

Hope this helps.

+4
source

Start the service using the following code:

 startService(new Intent(this, YourServiceClass.class)); 

Run it in your Activitie onStop () or anywhere!

-8
source

Source: https://habr.com/ru/post/1401856/


All Articles