Hi, I'm doing an app for android and I wanted to know how to create a service that does not die when the app is closed, that keeps working, because in it I see constantly if I have to send a notification to the user.
Hi, I'm doing an app for android and I wanted to know how to create a service that does not die when the app is closed, that keeps working, because in it I see constantly if I have to send a notification to the user.
You must create a class that extends from service
:
public class miservicio extends service
{
public miservicio() {
super();
}
@Override
public void Oncreate{
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
//lo que quieras que haga tu servicio
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
And you must start it in MainActivity
:
Intent intent = new Intent(this, miservicio.class);
startService(intent);
Go to the Android Studio toolbar and select File > New > Service > Service (Intent Service) . This will create a new IntentService
based on a prefabricated template and will automatically add it to AndroidManifest.xml .
Intent Services isolate heavy processes in a thread different from the main one, so as not to obstruct the UI if it is active.
Unlike Service
, your actions go in the controller onHandleIntent()
:
public class WorkingIntentService extends IntentService {
// TODO: Rename actions, choose action names that describe tasks that this
// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
public static final String ACTION_FOO = "com.herprogramacion.abtesting.action.FOO";
public static final String ACTION_BAZ = "com.herprogramacion.abtesting.action.BAZ";
// TODO: Rename parameters
public static final String EXTRA_PARAM1 = "com.herprogramacion.abtesting.extra.PARAM1";
public static final String EXTRA_PARAM2 = "com.herprogramacion.abtesting.extra.PARAM2";
public WorkingIntentService() {
super("WorkingIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_FOO.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionFoo(param1, param2);
} else if (ACTION_BAZ.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionBaz(param1, param2);
}
}
}
/**
* Handle action Foo in the provided background thread with the provided
* parameters.
*/
private void handleActionFoo(String param1, String param2) {
// TODO: Handle action Foo
throw new UnsupportedOperationException("Not yet implemented");
}
/**
* Handle action Baz in the provided background thread with the provided
* parameters.
*/
private void handleActionBaz(String param1, String param2) {
// TODO: Handle action Baz
throw new UnsupportedOperationException("Not yet implemented");
}
}
As you can see, you can determine what type of action the service will take with the actions of the Intent.
And generate handleAction*()
methods to separate the elections.
I hope this helps you.
If you want to supplement I leave this guide: Tutorial To Create A Service On Android
you have to develop the application to run in the background. This is done using the IntentService.
The IntentService class provides a simple structure for executing a single-thread operation running in the background. This allows you to handle long-term operations without affecting the responsiveness of the user interface. In addition, an IntentService is not affected by most UI events, so it continues to function under circumstances that would close an AsyncTask.
To create an IntentService component for your application, you have to define a class that extends IntentService, and within it, define a method that overrides onHandleIntent (). For example:
public class RSSPullService extends IntentService {
@Override
protected void onHandleIntent(Intent workIntent) {
// Gets data from the incoming Intent
String dataString = workIntent.getDataString();
...
// Do work here, based on the contents of dataString
...
}
}
Define the IntentService in the Manifest An IntentService also needs an entry in its manifest application. Provide this entry as an element that is a child of the element:
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
...
<!--
Because android:exported is set to "false",
the service is only available to this app.
-->
<service
android:name=".RSSPullService"
android:exported="false"/>
...
<application/>
The android: name attribute specifies the class name of the IntentService.
Note that the element does not contain an intention filter. The activity that sends the work requests for the service uses an explicit intention, so no filter is needed. This also means that only the components of the same application or in other applications with the same user ID can access the service.
Now that you have the basic IntentService class, you can send work requests to it with intent objects.
You can look at this link where they explain and teach how to use it.