Home » Android » Start service in Android

Start service in Android

Posted by: admin March 11, 2020 Leave a comment


I want to call a service when a certain activity starts. So, here’s the Service class:

public class UpdaterServiceManager extends Service {

    private final int UPDATE_INTERVAL = 60 * 1000;
    private Timer timer = new Timer();
    private static final int NOTIFICATION_EX = 1;
    private NotificationManager notificationManager;

    public UpdaterServiceManager() {}

    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;

    public void onCreate() {
        // Code to execute when the service is first created

    public void onDestroy() {
        if (timer != null) {

    public int onStartCommand(Intent intent, int flags, int startid) {
        notificationManager = (NotificationManager) 
        int icon = android.R.drawable.stat_notify_sync;
        CharSequence tickerText = "Hello";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);
        Context context = getApplicationContext();
        CharSequence contentTitle = "My notification";
        CharSequence contentText = "Hello World!";
        Intent notificationIntent = new Intent(this, Main.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText,
        notificationManager.notify(NOTIFICATION_EX, notification);
        Toast.makeText(this, "Started!", Toast.LENGTH_LONG);
        timer.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                // Check if there are updates here and notify if true
        }, 0, UPDATE_INTERVAL);
        return START_STICKY;

    private void stopService() {
        if (timer != null) timer.cancel();

And here is how I call it:

Intent serviceIntent = new Intent();

The problem is that nothing happens. The above code block is called at the end of the activity’s onCreate. I already debugged and no exception is thrown.

Any idea?

How to&Answers:

Probably you don’t have the service in your manifest, or it does not have an <intent-filter> that matches your action. Examining LogCat (via adb logcat, DDMS, or the DDMS perspective in Eclipse) should turn up some warnings that may help.

More likely, you should start the service via:

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


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

Just writing this line was not sufficient for me. Service still did not work. Everything had worked only after registering service at manifest

    android:label="@string/app_name" >


        android:label="My Service" >


Java code for start service:

Start service from Activity:

startService(new Intent(MyActivity.this, MyService.class));

Start service from Fragment:

getActivity().startService(new Intent(getActivity(), MyService.class));


import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {

    private static String TAG = "MyService";
    private Handler handler;
    private Runnable runnable;
    private final int runTime = 5000;

    public void onCreate() {
        Log.i(TAG, "onCreate");

        handler = new Handler();
        runnable = new Runnable() {
            public void run() {

                handler.postDelayed(runnable, runTime);

    public IBinder onBind(Intent intent) {
        return null;

    public void onDestroy() {
        if (handler != null) {

    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;

    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.i(TAG, "onStart");


Define this Service into Project’s Manifest File:

Add below tag in Manifest file:

<service android:enabled="true" android:name="com.my.packagename.MyService" />



I like to make it more dynamic

Class<?> serviceMonitor = MyService.class; 

private void startMyService() { context.startService(new Intent(context, serviceMonitor)); }
private void stopMyService()  { context.stopService(new Intent(context, serviceMonitor));  }

do not forget the Manifest

<service android:enabled="true" android:name=".MyService.class" />


Intent serviceIntent = new Intent(this,YourActivity.class);


add service in manifist

<service android:enabled="true" android:name="YourActivity.class" />

for running service on oreo and greater devices use for ground service and show notification to user

or use geofencing service for location update in background