簡體   English   中英

如何從android中的非Activity類調用服務方法

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

我看到了關於如何綁定服務並從Activity調用其方法的示例。

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

但我想綁定一個服務並從非Activity類調用它的方法。

我怎樣才能做到這一點?

因為我沒有實現以下方法:

bindService, unbindService

通過相同的方式從Activity獲取/傳遞content Activity的實例

假設您有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);

請參閱此處的文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM