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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:13:10+00:00 2026-06-18T11:13:10+00:00

I have an activity in which I start a service, for example I staty

  • 0

I have an activity in which I start a service, for example I staty MyService as:

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

Inside my service I create a thread and run it. Here is part of my code:

public class MyService extends Service {
...
@Override
public void onStart(Intent intent, int startid) {
    Thread mythread= new Thread() {
        @Override
        public void run() {
            while(true)
            {
              ...
            }
        }
    };
    mythread.start();
}

}

Now instead of while(true) I want to use while(a), where a is a parameter that is passed from my activity to this service. Please note that my activity is a different class than my service. How can this be done? Please show specific example with some codes.

  • 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-18T11:13:11+00:00Added an answer on June 18, 2026 at 11:13 am

    You can get access to your service by binding to it. Edit your service class so that it returns an IBinder onBind()

    public class MyService extends Service {
    
        private static final String TAG = MyService.class.getSimpleName();
        private final IBinder binder = new ServiceBinder();
        private boolean a;
    
        @Override
        public IBinder onBind( Intent intent ) {
    
            return binder;
        }
    
        @Override
        public int onStartCommand( Intent intent, int flags, int startId ) {
    
            return super.onStartCommand( intent, flags, startId );
        }
    
        @Override
        public void onCreate() {
    
            super.onCreate();
        }
    
        public class ServiceBinder extends Binder {
    
            public MyService getService() {
    
                return MyService.this;
            }
        }
    
        public void setA(boolean a) {
    
            this.a = a;
        }
    }
    

    Now in your activity you need to handle binding and unbinding to your service. In this example, the service sticks around whether you are bound or not. If this is not the functionality you want, you can just not call startService(...):

    public class MyActivity extends Activity {
    
        //...
        private MyService myService;
        private boolean bound;
    
        @Override    
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            Intent intent = new Intent( this, MyService.class );
            startService( intent );
            doBindService();
        }
    
        private final ServiceConnection serviceConnection = new ServiceConnection() {
    
            public void onServiceConnected( ComponentName className, IBinder service ) {
    
                myService = ( (MyService.ServiceBinder) service ).getService();
                bound = true;
            }
    
            public void onServiceDisconnected( ComponentName className ) {
    
                myService = null;
                bound = false;
            }
        };
    
        void doBindService() {
    
            boolean bound = bindService( new Intent( this, MyService.class ), serviceConnection, Context.BIND_AUTO_CREATE );
            if ( bound ) {
    
                Log.d( TAG, "Successfully bound to service" );
            }
            else {
    
                Log.d( TAG, "Failed to bind service" );
            }
        }
    
        void doUnbindService() {
    
            unbindService( serviceConnection );
        }
    }
    

    Now you have a reference to your bound service in your activity and you can just call myService.setA(true) to set your parameter.

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

Sidebar

Related Questions

I have main activity which call other object, for example GPS class. This GPS
I have Activity and Service which plays music. onCreate of activity I startService and
I have an Android service which I start from the main activity. The service
I have a phonegap activity which I want to start via a button press:
I'm struggling to decide which class to use, Activity or Service. My application is
I have my remote service which tries to run an activity from a different
I have Application that contains main Activity, Service and several Threads run on this
In my app, I have an Activity from which I want to start a
I have an activity that is a service which plays audio using a media
Here is a situation, I have an android activity which starts a service (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.