how to call a service method from a non-Activity class in android

android, android-intent, java, service

Solution

By the same way like from `Activity` just take/pass instance of `content` Activity

Lets say you have `MyActivity` class and `OtherClass` class

so you run in `OtherClass`

 public class OtherClass {

    Context mContext;

    public void init(Context context){
      mContext = context;
    }
    ...

mContext.startService(new Intent(mContext, SomeService.class)); 

[EDIT]

In your case:

Intent intent = new Intent(mContext, LocalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

See documentation here

Problem

I saw this sample on how to bind a service and call its methods from an Activity. http://developer.android.com/reference/android/app/Service.html#LocalServiceSample But I want to bind a service and call its methods from a non-Activity class. how can I do this? as i don't have implementation of the following methods: `bindService, unbindService`

Original source

Related problems