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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:24:30+00:00 2026-06-08T01:24:30+00:00

I have created a Service in android which can send data to the activity

  • 0

I have created a Service in android which can send data to the activity on a regular interval.

so far i have create an activity which binds with the service which i have created.
but the problem my activity gets freez when i am sending message back to the activity which is previously bind. i have create this example based on MessengerService example from API Demo samples.

following is a code written in a service class and inside that i have create a IncomingHandler

case MSG_SET_VALUE:

mValue = msg.arg1;

for (int i=mClients.size()-1; i>=0; i--) {

    final Messenger local = mClients.get(i);
    final int index = i;
    counterThread = new Thread(new Runnable(){
    @Override
    public void run() {
        // TODO Auto-generated method stub
        int j=0;                

        while(true){

            try {
                Thread.sleep(6000);
                local.send(Message.obtain(null,
                    MSG_SET_VALUE, j++, 0));
                Log.d("Thread " , j + "");
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                 mClients.remove(index);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           

        }

    }




    });
counterThread.run();



}
break;

so when i send a message from activity using following code , above case will get triggered.

         msg = Message.obtain(null,
                 MyService.MSG_SET_VALUE, this.hashCode(), 0);
         mService.send(msg);

and i am handling the incoming message in a activity

 class IncomingHandler extends Handler {
         @Override
         public void handleMessage(Message msg) {
             switch (msg.what) {
                 case MyService.MSG_SET_VALUE:

                     System.out.println("DATA" + msg.arg1);
                    //   mCallbackText.setText("Received from service: " + msg.arg1);

                     break;
           }
       }
    }

so i want to know tht is this the right way to get data periodically from Service or am i missing something here

  • 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-08T01:24:33+00:00Added an answer on June 8, 2026 at 1:24 am
    counterThread.run();
    

    This is a bad method. You will call start() to run thread properly :

    counterThread.start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a web service which is saving some data into to db.
I have created one android app in which I am starting a service by
In an android service I have created thread(s) for doing some background task. I
I have created a WCF service for uploading images , which accepts System.IO.Stream as
I have created a web service which takes a username and password as parameters
Sorry this is my first time to create/use a web service I have created
I have a service running in my Android application which contains a HashMap that
I have created a Push Notifications for Android using GCM. The Service is facing
So far I have created a PHP client, and a VB.NET client which both
I have created one service in my grails application. in that service 25 methods

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.