如何从android中的非Activity类调用服务方法

Ela*_*nda 5 java service android android-intent

我看到了有关如何绑定服务并从活动调用其方法的示例。

http://developer.android.com/reference/android/app/Service.html#LocalServiceSample

但是我想绑定一个服务并从一个非 Activity 类调用它的方法。

我怎样才能做到这一点?

因为我没有实现以下方法:

bindService, unbindService

Max*_*tin 4

通过与仅获取/传递ActivityActivity实例相同的方式content

假设你有MyActivity班级和OtherClass班级

所以你跑进去OtherClass

 public class OtherClass {

    Context mContext;

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

mContext.startService(new Intent(mContext, SomeService.class)); 
Run Code Online (Sandbox Code Playgroud)

[编辑]

在你的情况下:

Intent intent = new Intent(mContext, LocalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
Run Code Online (Sandbox Code Playgroud)

请参阅此处的文档