0

Let me start by saying I've been searching for a long time, found a lot of similar questions (on SO) but I can't find anything to solve this yet:

I have a Service (jobcrawler) that is started by calling startservice(). Within this service, I am starting a long-running thread, which at some point is calling a class (webservice) whose init looks like this:

public webservice(Context context) {
    this.context = context;
    this.db = new DatabaseHandler(this.context);
    this.access_token = db.getAuthKey();
}

After some network calls, the class(webservice) receives data in a method called recieveData(). Inside recieveData I am attempting to bind to the service as follows:

        if (!isBound) {
            // not bound yet, then bind to the service.
            Intent intent = new Intent(this, jobcrawler.class);
            bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
        }

Now, I'm getting nullpointerexemption on the line where I call bindservice. Note, that I'm not actually attempting to do anything with the service yet. I'm just trying to bind to it. any help would be appreciated... if I had hair I'd be pulling it out! lol

Here's some additional code that I think is relevant. myConnection:

private ServiceConnection myConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className,IBinder service) {
        Log.e("webservice", "service is connected");
        MyLocalBinder binder = (MyLocalBinder) service;
        myService = binder.getService();
        isBound = true;
    }

    public void onServiceDisconnected(ComponentName arg0) {
        Log.e("webservice", "service is disconnected");
        isBound = false;
    }

};

binder from service called MyLocalBinder:

public class MyLocalBinder extends Binder {
    public jobcrawler getService() {   
            Log.e("Job Crawler", "returning self");
        return jobcrawler.this;
    }
}

service's onbind method:

private final IBinder myBinder = new MyLocalBinder();

@Override
public IBinder onBind(Intent arg0) {
  Log.d("JobCrawler Service", "Service is bound");
    return myBinder;
}

oh and this is where I load the class from the thread inside the service, just in case I should be using a different context or something:

         private webservice ws= new webservice(getBaseContext());
TT--
  • 2,956
  • 1
  • 27
  • 46
Chris
  • 125
  • 1
  • 3
  • 12
  • Not 100% sure, but I think your MyLocalBinder is defined incorrectly. You should create an AIDL file that defines your binder. JobCrawler also needs to be parcelable and ideally defined in AIDL as well. – Bishnu Apr 14 '13 at 00:35
  • So, I can't bind to a service this way? I'm not sure what AIDL is. I was following this tutorial (http://www.techotopia.com/index.php/Android_Local_Bound_Services_%E2%80%93_A_Worked_Example) when I created the binding code. – Chris Apr 14 '13 at 00:52
  • Ok, so from what I can find AIDL is for services being accessed outside of the app context. but my service is only attempting to communicate with a class within the same app, so I shouldn't need AIDL, right? – Chris Apr 14 '13 at 02:22
  • If you are doing this in the same process, then why are you using a bound service? There are infinitely many easier, more java like ways to do this. – Bishnu Apr 15 '13 at 00:50
  • because as far as I could tell, the only way to actually send information back to a service is to bind to it. if there are other options I'm totally open to them. any suggestions? – Chris Apr 15 '13 at 20:57
  • Service wraps singleton and you grab singleton elsewhere. Done. – Bishnu Apr 15 '13 at 23:50
  • can you give me an example of that? – Chris Apr 16 '13 at 02:24
  • Possible duplicate of [onServiceConnected never called after bindService method](https://stackoverflow.com/questions/2486692/onserviceconnected-never-called-after-bindservice-method) – TT-- Jul 17 '19 at 16:16

1 Answers1

0

I know it's a bit late, but I ran upon the same problem and maybe some googlers will be happy :)

So for me the following worked: Call the bindService method in reference to your context:

context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
ya man
  • 443
  • 1
  • 10
  • 15