Create a background service  |  Android Developers

Create a background service | Android Developers

Create a background service The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface’s responsiveness.

Services overview | Android Developers

Background A background service performs an operation that isn’t directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service.

Creating a Background Service | Android Developers

Because android:exported is set to ”false”, the service is only available to this app. –> The attribute android:name specifies the class name of the IntentService .

Send work requests to the background service | Android

Create and send a work request to a JobIntentService. To create a work request and send it to a JobIntentService, create an Intent and enqueue it to be executed by calling enqueueWork(). Optionally you can add data to the intent (in the form of intent extras) for the JobIntentService to process.

Creating a Background Service | Android Developers

The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface’s responsiveness. Also, an IntentService isn…

Creating Background Service in Android – Stack Overflow

It will not keep running forever — Android will eventually get rid of the service, because too many developers write services that try to be ”always running in the background”. And. of course, the user can kill the service whenever the user wants to.

Here is a semi-different way to keep the service going forever. There is ways to kill it in code if you’d wish Background Service: package com.ex.ample;

import android.app.Service;
import android.content.*;
import android.os.*;
import android.widget.Toast;

public class BackgroundService extends Service {

public Context context = this;
public Handler handler = null;
public static Runnable runnable = null;

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
Toast.makeText(this, ”Service created!”, Toast.LENGTH_LONG).show();

handler = new Handler();
runnable = new Runnable() {
public void run() {
Toast.makeText(context, ”Service is still running”, Toast.LENGTH_LONG).show();
handler.postDelayed(runnable, 10000);
}
};

handler.postDelayed(runnable, 15000);
}

@Override
public void onDestroy() {
/* IF YOU WANT THIS SERVICE KILLED WITH THE APP THEN UNCOMMENT THE FOLLOWING LINE */
//handler.removeCallbacks(runnable);
Toast.makeText(this, ”Service stopped”, Toast.LENGTH_LONG).show();
}

@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, ”Service started by user.”, Toast.LENGTH_LONG).show();
}
}
Here is how you start it from your main activity or wherever you wish: startService(new Intent(this, BackgroundService.class));
onDestroy() will get called when the application gets closed or killed but the runnable just starts it right back up. You need to remove the handler callbacks as well. I hope this helps someone out. The reason why some people do this is because of corporate applications where in some instances the users/employees must not be able to stop certain things 🙂 http://i.imgur.com/1vCnYJW.png6
But if am kill the current activity the service also is killing. I need this service always running in the background. What I need to do? If by ”kill the current activity” you mean that you are using a task killer, or Force Stop from within the Settings app, your service will be stopped. There is nothing you can do about that. The user has indicated they do not want your app to run any more; please respect the user’s wishes. If by ”kill the current activity” you mean you pressed BACK or HOME or something, then the service should keep running, at least for a little while, unless you call stopService() . It will not keep running forever — Android will eventually get rid of the service, because too many developers write services that try to be ”always running in the background”. And. of course, the user can kill the service whenever the user wants to. A service should only be ”running” when it is actively delivering value to the user . This usually means the service should not be ”always running in the background”. Instead, use AlarmManager and an IntentService to do work on a periodic basis.5you can create background service and call that by AlarmManager 1- you have to create a BroadcastReceiver class for calling by
AlarmManager public class AlarmReceiver extends BroadcastReceiver

{
/**

* Triggered by the Alarm periodically (starts the service to run task)

* @param context

* @param intent

*/

@Override

public void onReceive(Context context, Intent intent)

{

Intent i = new Intent(context, AlmasService.class);

i.putExtra(”foo”, ”AlarmReceiver”);

context.startService(i);

}

}
2-you have to create a IntentService class for calling by
AlarmReceiver public class AlmasService extends IntentService

{

public Context context=null;

// Must create a default constructor
public AlmasService() {

// Used to name the worker thread, important only for debugging.
super(”test-service”);

}

@Override

public void onCreate() {

super.onCreate(); // if you override onCreate(), make sure to call super().

}

@Override
protected void onHandleIntent(Intent intent) {

context=this;
try

{

Thread.sleep(5000);

}

catch (InterruptedException e)

{

e.printStackTrace();

}

String val = intent.getStringExtra(”foo”);

// Do the task here
Log.i(”MyTestService”, val);

}

}
3- you have to add AlarmReceiver as receiver and AlmasService as service on manifest

4-now you can start service and call AlarmManager on MainActivity public class MainActivity extends AppCompatActivity
{
public static final int REQUEST_CODE = (int) new Date().getTime();

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scheduleAlarm();
}

public void scheduleAlarm()
{
// Construct an intent that will execute the AlarmReceiver
Intent intent = new Intent(getApplicationContext(), AlmasAlarmReceiver.class);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(
this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every every half hour from this point onwards
long firstMillis = System.currentTimeMillis(); // alarm is set right away
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
// First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
// Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY
alarm.setRepeating(AlarmManager.RTC_WAKEUP, firstMillis, (long) (1000 * 60), pIntent);

}
}4
Try to start the service in separate thread, so that when you will destroy your activity the service will not be affected. It will run without any interruption. Also, in the service return Service.START_STICKY from onStartCommand(intent, flags, startId) to make sure that the service is re-created if it is killed by the system (Android OS).3override this method: public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}1

java – How to create an always running background service
Android application as a service without activity

See more results

Report work status | Android Developers

This guide shows you how to report the status of a work request run in a background service to the component that sent the request. This allows you, Follow Android Developers on Google+. YouTube Check out Android Developers on YouTube. More Android. Android Enterprise Security

Creating a never ending background service in Android

Creating a never ending background service in Android is simple but not obvious. Android has two ways of working: foreground (the app that you use and manipulate) and background (the services that you do not see but that provide data to the apps).

Service | Android Developers

Note that because Service itself is so simple, you can make your interaction with it as simple or complicated as you want: from treating it as a local Java object that you make direct method calls on (as illustrated by Local Service Sample), to providing a full remoteable interface using AIDL.