安卓:从活动传递参数服务参数

2023-09-12 04:23:05 作者:骨zǐ里的傲气

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

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中空 - 丛尽管我做了 intent.putExtra 之前为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创建声明你想从一个活动来调用所有的方法签名的接口:

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让你的粘合剂实现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.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()回调之前涉足由系统。因此,要执行服务方法最好的地方是后立即MSERVICE在ServiceConnection.onServiceConnected得到初始化()方法。

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.

希望这有助于。