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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:11:59+00:00 2026-05-28T02:11:59+00:00

I have an application with many activities, and behind the application I want a

  • 0

I have an application with many activities, and “behind” the application I want a server socket always running, listening and serving data on request.

So I’ve created a service, but maybe I’m not understanding how it works because I’m having some problems. Let’s see my service:

public class MyService extends Service {
    private static final String TAG = "MyService";  
    @Override   
    public IBinder onBind(Intent intent) {  
        return null;    
    }       
    @Override   
    public void onCreate() {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        Log.d("STATUS", "onCreate service");    

        try {


            // create socket
            ServerSocket servsock;
            servsock = new ServerSocket(50000);
            Log.v("STATUS","SERVER SOCKET CREATED at="+50000);


while (true) {
                Log.v("status","Waiting..."); 

                Socket sc = servsock.accept();
                Log.v("status","Accepted connection : " + sc);

                ObjectOutputStream os =  new ObjectOutputStream(sc.getOutputStream());
                ObjectInputStream is = new ObjectInputStream(sc.getInputStream());
                .........
                sc.close();

            }

.............

In my app the first screen appearing it’s a login screen, and I want the service to start imediatelly after the login (on creation of the “logged in” activity)

public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainActivity);

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

Problem is, when the activity starts I get a Force Close above a black screen (interesting because the startService is after the setContentView) – although I can see through the Logcat that the service (and the server socket) was executed.

The activity manager gives me the following information:

01-10 17:55:43.706: E/ActivityManager(2490): ANR in
com.sapo.android.obesidade 01-10 17:55:43.706:
E/ActivityManager(2490): Reason: Executing service
com.sapo.android.obesidade/.MyService 01-10 17:55:43.706:
E/ActivityManager(2490): Load: 1.24 / 1.13 / 1.1 01-10 17:55:43.706:
E/ActivityManager(2490): CPU usage from 43658ms to 10ms ago: 01-10
17:55:43.706: E/ActivityManager(2490): system_server: 6% = 3% user +
3% kernel / faults: 7590 minor 01-10 17:55:43.706:
E/ActivityManager(2490): .app.twlauncher: 2% = 2% user + 0% kernel /
faults: 812 minor 01-10 17:55:43.706: E/ActivityManager(2490):
mmcqd: 0% = 0% user + 0% kernel

So the problem is undoubtedly (it works if removed) on the while(true) – something that was already working in an AsyncTask in an Activity (without using Services).

What can I do ?

  • 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-05-28T02:12:00+00:00Added an answer on May 28, 2026 at 2:12 am

    Your problem (as with most ANRs) is that you’re hogging the main thread. A service by default runs on the main (UI) thread. Since you have a while(true), it’s constantly blocking. You should launch a separate thread from your service.

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

Sidebar

Related Questions

I have an application with many views. I want only a couple of the
I have always wondered about this. So many application setups have a zip file
In my web application we have many users.I want to set permission for each
I have an android application with many activities. I render next activity from the
In my application I have many classes and activities. Droid is a class which
i have many application running on my emulator but ps -ef does not show
I have a application with many dialogs and created a function to open a
I have an application that depends on connection It has many Activities.I need to
I have an application with lots of activities. Many of these activities can start
I have an application which has many icons (Buttons). I want to put a

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.