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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:57:21+00:00 2026-05-26T17:57:21+00:00

I am trying to implement a very simple service example. The user inputs the

  • 0

I am trying to implement a very simple service example.
The user inputs the value through EditText and clicks on Calculate Button. The Calculate button triggers a service which performs some calculations and sends the result back to another EditText box.
If I use a simple service without binding, the result gets displayed before the calculation is performed, so I want to use a bound service. But in my case, the control just stops at onBind call and onStart does not get executed. The control does flow to onCreate though. Could anyone help me to find where I am going wrong?

public class SimpleService extends Service {
    private final IBinder mBinder = new LocalBinder();


    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("Service: OnBind");
         return mBinder;
    }

    public class LocalBinder extends Binder {

        SimpleService getService() {
            System.out.println("Service: in Local binder");

            return SimpleService.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println(" Service:In on create...");
        Toast.makeText(this,"Service created ...",         Toast.LENGTH_LONG).show()
    }

    @Override
    public void onDestroy() {
       super.onDestroy();
       System.out.println(" Service:in on destroy...");

       Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
    }

@Override
public void onStart(Intent intent, int startid) {
  System.out.println("Service:in onstart command ...");
  super.onStart(intent, startid);
  int res;
  String LOG_TAG =  "";
  int input2 = intent.getIntExtra("input", -1);
  int mode = intent.getIntExtra("mode", -1);
  String aString = Integer.toString(mode);
  Log.v(LOG_TAG, aString);
  if(mode == 1) {
         res = cal_F(input2);
    } else {
         res = cal_C(input2);
    }

  intent.putExtra("result", res);
     }

    }

#

public class ClassExamplesServiceActivity extends Activity implements  OnClickListener{

@Override
public void onClick(View v) {

    input = Integer.parseInt(input1.getText().toString());
    if(v.getId() == R.id.radio_fib)
        rd_button = 0;
    else if(v.getId() == R.id.radio_fact)
        rd_button = 1;
    else if (v.getId() == R.id.button1){

        intent = new Intent(this, SimpleService.class);
        intent.putExtra("input", input);
        intent.putExtra("mode", rd_button);
        doBindService();
        System.out.println("in class activity "+System.currentTimeMillis());

    }       

    else if(v.getId() == R.id.stop)
    {
        stopService(intent);
    }     
}

private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder service) {
        System.out.println("\n in service connection");
        mBoundService = ((SimpleService.LocalBinder)service).getService();  
    }



public void onServiceDisconnected(ComponentName className) {
      System.out.println("\n in service disconnected");
      mBoundService = null;
  }
};

void doBindService() {
    System.out.println("in do bind service");

    boolean isConnected = bindService(new Intent(ClassExamplesServiceActivity.this, SimpleService.class), mConnection, Context.BIND_AUTO_CREATE);
    intent.putExtra("input", input);
    intent.putExtra("mode", rd_button);
    System.out.println("\n isconnected = "+ isConnected);
    mIsBound = true;
}
void doUnbindService() {
    if (mIsBound) {
        res = intent.getIntExtra("result", -1);
        result.setText(Integer.toString(res));// Set the result in the EditText
        // Detach our existing connection.
        unbindService(mConnection);
        mIsBound = false;
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    doUnbindService();
}
}

#

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".ClassExamplesServiceActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".SimpleService"></service>
</application>
  • 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-26T17:57:21+00:00Added an answer on May 26, 2026 at 5:57 pm

    You need to call Context.startService() in order to use onStart():
    http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)

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

Sidebar

Related Questions

I am trying to implement some very simple Data Binding in my User Control
I'm trying to implement a very, very simple accessibility test for Swing so I
I am very interested in at least trying to implement NAT break through for
This is a very basic example of what I am trying to implement in
I'm trying to implement a very simple footer notification element to slide up for
I'm trying to implement a very simple XML schema constraint. The idref attribute on
I'm trying to implement my custom security attribute. It's very simple for now [Serializable]
I am trying to implement something seemingly very simple, and I have been beating
I'm trying to implement a very simple strategy class in Objective-C that allows for
Grrrrrr, trying to implement a very simple tree. It doesn't need to be bidirectional

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.