Android:将参数从活动传递给服务 [英] Android: pass parameter to Service from Activity

查看:33
本文介绍了Android:将参数从活动传递给服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过这种方式绑定到服务:

I'm binding to Service by this way:

活动类:

ListenLocationService mService;
@Override
public void onCreate(Bundle savedInstanceState) {
        ...
        Intent intent = new Intent(this, ListenLocationService.class);
        intent.putExtra("From", "Main");
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        ...
}

private ServiceConnection mConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className,
                IBinder service) {
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();         
        }

        public void onServiceDisconnected(ComponentName arg0) {
        }
    };

这是我的服务onBind方法:

@Override
public IBinder onBind(Intent intent) {
    Bundle extras = intent.getExtras(); 
    if(extras == null)
        Log.d("Service","null");
    else
    {
        Log.d("Service","not null");
        String from = (String) extras.get("From");
        if(from.equalsIgnoreCase("Main"))
            StartListenLocation();
    }
    return mBinder;
}

所以我在 LogCat 中有null" - 尽管我在 bindService

So I have "null" in LogCat - bundle is null in spite of I made intent.putExtra before bindService

总的来说,服务工作正常.但是我只需要从应用程序的主要活动中调用 StartListenLocation(); (我决定通过发送一个标志来做到这一点).

In general Service works fine. However I need to call StartListenLocation(); only from main activity of application (I decide to do this by sending a flag).

我如何向服务发送数据?或者也许有另一种方法来检查启动了哪个活动 onBind?

How can I send a data to service?Or maybe there's another way to check what activity launched onBind?

推荐答案

1 创建一个接口,声明您要从 Activity 调用的所有方法签名:

1 Create a interface that declare all method signature that you want to call from a Activity:

public interface ILocationService {
  public void StartListenLocation(Location location);
}

2 让你的 binder 实现 ILocaionService 并定义实际的方法体:

2 Make your binder implements ILocaionService and define the actual method body:

public class MyBinder extends Binder implements ILocationService {
  ... ...

  public void StartListenLocation(Location location) {
    // implement your method properly
  }

  ... ...
}

3 在绑定到服务的活动中,通过接口引用您的绑定器:

3 In activity that bind to the service, reference your binder by the interface:

... ...

ILocationService mService; // communication is handled via Binder not the actual service class.

private ServiceConnection mConnection = new ServiceConnection() {

  public void onServiceConnected(ComponentName className, IBinder service) {
    mService = (ILocationService) service;     
  }

  ... ...
};

... ...

// At some point if you need call service method with parameter:
Location location = new Location();
mService.StartListenLocation(location);

所有通信(即对您的服务的方法调用)都应该通过在 ServiceConnection.onServiceConnected() 中初始化和返回的 binder 类来处理和执行,而不是实际的服务类(binder.getService() 是不必要的).这就是设计用于 API 中的绑定服务通信的方式.

All communications (i.e. method call to your Service) should be handled and performed via the binder class initialize and return in ServiceConnection.onServiceConnected(), not the actual service class (binder.getService() is unnecessary). This is how bind service communication designed to work in the API.

注意 bindService() 是一个异步调用.在您调用 bindService() 之后和在 ServiceConnection.onServiceConnected() 回调被系统参与之前会有一个延迟.所以最好在 ServiceConnection.onServiceConnected() 方法中初始化 mService 之后立即执行 service 方法.

Note that bindService() is an asynchronous call. There will be a lag after you call bindService() and before ServiceConnection.onServiceConnected() callback get involved by system. So the best place to perform service method is immediately after mService get initialized in ServiceConnection.onServiceConnected() method.

希望这会有所帮助.

这篇关于Android:将参数从活动传递给服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆