Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8551661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:14:38+00:00 2026-06-11T14:14:38+00:00

My service extends IntentService and when it is started with startService , onHandleIntent gets

  • 0

My service extends IntentService and when it is started with startService, onHandleIntent gets called. However, when the service is started with bindService (I do need binding), onHandleIntent does not get called.

Should onHandleIntent be called when IntentService is started with bindService? Is startService the only way IntentService should be started?

The documentation for IntentService says the following:

Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

Currently I solve my problem by calling startService right after bindService but I find it ugly. I would like to know is there a way to make it work with just one call.

Code snippets follow, it might be that I am missing something obvious.

ExampleService.java

public class ExampleService extends IntentService {

private class IncomingHandler extends Handler {

    @Override
    public void handleMessage(Message message) {

        if (message.replyTo != null) {
            outMessenger = message.replyTo;
        }
    }
}

private Messenger messenger = new Messenger(new IncomingHandler()); 
private Messenger outMessenger = null;


public ExampleService() {
    super("ExampleService");
}

@Override
public IBinder onBind(Intent intent) {
    return messenger.getBinder();
}

@Override
protected void onHandleIntent(Intent arg0) {

    System.out.println("Service started");

    for (int i = 0; i < 5; i++) {

        SystemClock.sleep(5000);

        if (outMessenger != null) {
            try {
                outMessenger.send(Message.obtain());
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
}

Service Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.service"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="3"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".ExampleService">
        <intent-filter>
            <action android:name="com.example.service.ExampleService" />
        </intent-filter>
    </service>
</application>
</manifest>

MainActivity.java (caller)

public class MainActivity extends Activity implements ServiceConnection {


private class IncomingHandler extends Handler {

    @Override
    public void handleMessage(Message msg) {

        Toast.makeText(getApplicationContext(), "Message received", Toast.LENGTH_SHORT).show();
        System.out.println("Message received!");
        super.handleMessage(msg);
    }
}

private Messenger messenger = new Messenger(new IncomingHandler());

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

    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent("com.example.service.ExampleService");
            bindService(intent, MainActivity.this, Context.BIND_AUTO_CREATE);
            //startService(intent);
        }
    });

}

@Override
public void onServiceConnected(ComponentName name, IBinder binder) {

    Message message = Message.obtain();
    message.replyTo = messenger;

    try {
        new Messenger(binder).send(message);
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void onServiceDisconnected(ComponentName name) {
    // TODO Auto-generated method stub
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:14:39+00:00Added an answer on June 11, 2026 at 2:14 pm

    Should onHandleIntent be called when IntentService is started with bindService?

    No.

    Is startService the only way IntentService should be started?

    IMHO, yes. IMHO, IntentService is not designed for the binding pattern.

    In your case, you can:

    • Pass a Messenger from the activity in an Intent extra in the command sent by startService(), or
    • Use LocalBroadcastManager, or
    • Use Otto, or
    • Use an ordered broadcast, if the IntentService might continue past the activity’s life and you want to, say, display a Notification when the work gets done in that case,
    • Etc.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I have some class myService extends Service() being called by a startService
colleagues, how can i debug an activity that extends the service class? Its not
This is my service file: public class SMSCounterService extends Service { /** Called when
It does not get registration id from GCM server the app does not give
I have an IntentService that queues up web service calls that need to be
I need help with this situation: I have activity, what starts IntentService. Service do
I implemented Service for listening user location: public class ListenLocationService extends Service { private
I implemented Android service for listening user location: public class ListenLocationService extends Service {
I have this code that describes a service: public class navigation_web extends Service {
I've created a class that extends Application. This class starts a service when the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.