Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 207
Elad Benda Avatar asked Oct 21 '22 02:10

Elad Benda


1 Answers

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

like image 125
Maxim Shoustin Avatar answered Nov 01 '22 09:11

Maxim Shoustin