我看到了一个关于如何绑定服务并从活动中调用其方法的示例。
http://developer.android.com/reference/android/app/Service.html#LocalServiceSample
但我想绑定一个服务并从非活动类中调用它的方法。
我该怎么做?
因为我没有以下方法的实现:
bindService,unbindService
我看到了一个关于如何绑定服务并从活动中调用其方法的示例。
http://developer.android.com/reference/android/app/Service.html#LocalServiceSample
但我想绑定一个服务并从非活动类中调用它的方法。
我该怎么做?
因为我没有以下方法的实现:
bindService,unbindService
Activity
中获取/传递实例相同的方式,只需获取/传递content
活动的实例。MyActivity
类和OtherClass
类。OtherClass
中运行。 public class OtherClass {
Context mContext;
public void init(Context context){
mContext = context;
}
...
mContext.startService(new Intent(mContext, SomeService.class));
[编辑]
针对您的情况:
Intent intent = new Intent(mContext, LocalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
bindService
问题是否可以解决?我真的需要操作mContext成员吗? - Elad Benda
android.content.ContextWrapper
类,在其中实现bindService
、unbindService
和startService
方法,然后可以从你的扩展类中使用它们。 - Eido95