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 7952153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:46:34+00:00 2026-06-04T02:46:34+00:00

Having read most of the available documentation on Android services on the developer site

  • 0

Having read most of the available documentation on Android services on the developer site and here in stackoverflow, I’m still confused by several aspects of running a service in a separate task. Hopefully someone can put me on the right track.

Let’s say we have trival service framework such as

public class HliService extends Service {

    @Override
    public void onCreate() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // If we get killed, after returning from here, restart
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // We don't provide binding, so return null
        return null;
    }

    @Override
    public void onDestroy() {
    }
}

and in the manifest, I have

<service android:name=".HliService" android:process=":HLI_Comms"/>

so that the service runs in its own thread.

The intent of the service is to provide a background task that will communicate
to a device using a TCP socket and do some other stuff. At the risk of ignoring battery issues etc, basically I’d like it to run forever.

Something like

// Method that communicates using a TCP socket, and needs to send
// information back to the activity and receive messages from activity
// not shown here.
private void dummytask() {

    boolean keepGoing = true;
    while (keepGoing) {
        // do useful stuff in here
        // sets keepGoing false at some point
    }
    stopSelf();
}

What is the best way to initiate this method/task ?

I have looked at code in the developer site that uses a message handler and a looper, which I only partly understand, but it seems very complicated and perhaps more than I require?

I don’t believe I can call this method from either onCreate() or onStartCommand() since then neither would complete when invoked from the system ? Should I start it with a timer or alarm?

I will need to add a message handler to communicate with the the gui activity, but since I’m starting the service in another thread (by virtue of the manifest “process” instruction), do I need to use AIDL instead?

I have also looked at using AysnchTask rather than extending Service, but it seems better suited to running a task and then terminating.

  • 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-04T02:46:35+00:00Added an answer on June 4, 2026 at 2:46 am

    so that the service run in its own thread.

    That puts the service in its own process. This is generally something to be avoided, as it consumes extra RAM and CPU (for IPC). You can create a thread just by creating a Thread or any number of other means, most of which have been in Java for a decade or so.

    At the risk of ignoring battery issues etc, basically I’d like it to run forever.

    It is pretty much impossible for a service to run forever. Users or the OS will get rid of your service eventually.

    What is the best way to initiate this method/task ?

    Call dummytask() from a background thread.

    do I need to use AIDL instead?

    No. Your service can broadcast an Intent, or invoke a PendingIntent supplied by the activity, or send a Message via a Messenger supplied by the activity, etc. The best would be to use the LocalBroadcastManager from the Android Support package, but that will not work across process boundaries, forcing you into more expensive communications options.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having read Apple's numerous documentation on icon guidelines: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/MobileHIG/IconsImages/IconsImages.html#//apple_ref/doc/uid/TP40006556-CH14-SW2 http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/App-RelatedResources/App-RelatedResources.html#//apple_ref/doc/uid/TP40007072-CH6-SW1 http://developer.apple.com/library/ios/#qa/qa1686/_index.html I was wondering
I have read the Django documentation (For reference, https://docs.djangoproject.com/en/1.3/intro/tutorial01/ ), but I'm still having
Having read all the StackOverflow entries regarding Model-View-ViewModel architecture along with most of the
Having read the documentation of Java's String class, it doesn't appear to support popping
I've read through all the documentation, and most of the source of LFE. All
Having read some postings here about this topic, I realize that there are quite
I read the whole documentation about AsyncTasks, Services,... It's pretty clear what a remote
In most documentation you read about ASP.NET MVC, the entire 'Separation of Concerns' is
Having read Fowler's Refactoring for a while, I still often catch myself thinking I
So having read some of the PostSharp documentation presented as an answer to my

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.