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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:19:54+00:00 2026-06-04T01:19:54+00:00

I am starting a service from activity.The problem here is the service gets started

  • 0

I am starting a service from activity.The problem here is the service gets started started but the activity is not getting displayed.

@Override
protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
           startService(new Intent(this, ServerActivity1.class));

        }

In the service I am opening a socket via a simple function like this by using a timer.The service gets started as I am able to see in logs but the view(R.layout.main) never gets displayed and after some time the force close pop is displayed.

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

@Override
public void onCreate() {
    Toast.makeText(this, "sasa", Toast.LENGTH_SHORT).show();
    timer.scheduleAtFixedRate( new TimerTask() {
        public void run() {
            read();

        } 
    }, 0,50000);
    Log.i("NoServer","Started1");   
    read();

}

@Override
public void onDestroy() {

}

@Override
public void onStart(Intent intent, int startid) {
     Log.i("Home","Listening on IP: " + SERVERIP+"\n");
}


public  void  read()
{
    SERVERIP = getLocalIpAddress();
   Log.i("Home","Listening on IP: " + SERVERIP+"\n");
               if (SERVERIP != null) {
                Log.i("Home","Listening on IP: " + SERVERIP+"\n");
                }
                try {
                    serverSocket = new         ServerSocket(SERVERPORT);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                   while (true) {
                      Socket client;
                      Log.i("Home","Listening on IP: " + SERVERIP+"\n");
                    try {
                            client = serverSocket.accept();
                            Log.i("Home","Listening on IP: " + SERVERIP+"\n");
                           BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                            while ((line = in.readLine()) != null) {                                                            
                            serverSocket.close();
                            read();
                        }

                       } catch (Exception e) {

                           e.printStackTrace();
                       }
                   }
          } 
  • 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-04T01:19:55+00:00Added an answer on June 4, 2026 at 1:19 am

    Its because your UI thread (main) is being shared by service unless you define your service in a separate process in manifest. If you start your service in activity’s onResume method, till then your service would be visible but still may cause ANR depending on the time (max 5 secs) it takes to complete requests in service.

    Its better to put all the socket stuff (or any expensive calls) of your service in a separate thread. In that case, your app will not hang or crash due to ANR.

    You should use ThreadHandler and Handler to execute Messages and/or Runnables in a separate thread inside Service.

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

Sidebar

Related Questions

I'm starting a service from my activity and then call finish(). The problem is
I'm trying to start an activity from a service running in background but its
I'm developing a web application for a new service, starting from Firefox 3.5. The
I am getting a timeout error while starting my Windows service. I am tring
I'm starting to design a wcf service bus that is small now but will
I'm starting a service in my application using startService. I do not want to
I am starting an activity from one by passing some data in the intent.
what is the best way to implements a service from starting with alarm manager
Possible Duplicate: Android: How can I get the current foreground activity (from a service)?
I've been trying to bind a service that was started on boot from an

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.